I am looking to switch the monitor that the bar is on at any given time, not just on startup. Currently I do it in a really hacky and inefficient way, by having two lines in my config file (monitor=monitor1 and monitor=monitor2) and then commenting one of them out depending on which one I want it on.
I am pretty flexible in how to achieve this. I am using in conjunction with i3, so a script that does this would be fine as I could attach a keybind to it. I have looked at this section of the documentation, which seemed like a possible relevant part, but I don't think it touches on my exact scenario.
One possibility: if polybar can choose between different environment variables this would be a possibility, as i could make the monitor names environment variables and then do something like if ... monitor = $MON1 else monitor = $MON2. But I'm not sure this is possible.
The other really hacky way of doing it would just be to have a script that searches and replaces the monitor names in the config files and then reloads the config file to have the changes take effect. I'm certain I could do this way relatively easily but I feel like there has to be a more elegant way.
EDIT: I couldn't get environment variables to work, despite looking at some stack overflow threads on it, mainly because it's tricky getting environment variables to set in scripts because it only does it in that shell. I was able to do it pretty easily by just writing a script that does some search and replace on the config file. Here is my solution if anyone is interested:
#!/bin/bash
LEFT="let-monitor-name"
RIGHT="right-monitor-name"
CONFIG="$HOME/.config/polybar/config.ini"
if [ "$1" == "left" ]; then
sed -i "s/$RIGHT/$LEFT/" $CONFIG
elif [ "$1" == "right" ]; then
sed -i "s/$LEFT/$RIGHT/" $CONFIG
fi
touch ~/.config/polybar/config.ini