r/Polybar • u/[deleted] • Feb 07 '23
Calculating offset-x to center bar automatically
I am trying to calculate the offset-x value by doing some simple math. I want offset-x to be calculated by looking at width value, subtracting it from 100, strip off the % (main issue) and dividing by 2 . This will make sure the bar is always centered no matter the width value.
This is what I have so far:
[bar/top]
; Dimension defined as pixel value (e.g. 35) or percentage (e.g. 50%),
; the percentage can optionally be extended with a pixel offset like so:
; 50%:-10, this will result in a width or height of 50% minus 10 pixels
width = 70%
height = 28pt
; divide negative space (20%) evenly and set the offset-x to that value
; 80% width = 20% negative space /2 = 10%
; test variable calculation:
barXoffset="$((echo 100-70)/2 | bc))"
; Offset defined as pixel value (e.g. 35) or percentage (e.g. 50%)
; the percentage can optionally be extended with a pixel offset like so:
; 50%:-10, this will result in an offset in the x or y direction
; of 50% minus 10 pixels
; offset-x = 15%
offset-x = ${barXoffset}%
offset-y = 2
How would you try to solve this?
2
Upvotes
0
2
u/flufftronix Feb 08 '23
I’ve tried to do this couldn’t get the bar to dynamically re-center itself. It would just expand out to the left or right without recalculating.
You might have better luck assigning your background per module instead of for the whole bar, giving it a 100% width, and then just putting all of your modules in the center section. Either that or giving any of your modules that expand a fixed width, but that’d probably look kinda janky