diff --git a/README.md b/README.md index 0adfdf4..8509fca 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ # easyconf-lemonbar -A python script to easily configure lemonbar. Object-oriented. +A python script to easily configure lemonbar. + +### Signals +You may update individual modules by sending a real-time signal to the +easyconf-lemonbar process. First, in the module configuration, specify which +signal to listen for, e.g. + +```yaml +# Display a random number, and only update when we recieve the RTMIN+1 signal. +- name: "random" + command: "python -c 'from random import randrange; print(randrange(1000))'" + refresh: 0 + signal: 1 +``` + +In the example above, the refresh is `0`. This means that the module *will never +refresh on its own*; it will wait for the RTMIN+1 signal. You may also configure +your module to both refresh on a time interval AND listen for a signal. + +Unfortunately, to send the signal, you cant just `pkill easyconf-lemonbar`. You +have to read the PID from ECL's pidfile. I would recommend creating an alias in +your shell as shown below. + +```sh +$ alias ecl_kill="cat /run/user/${UID}/easyconf-lemonbar.pid | xargs kill" +$ ecl_kill -RTMIN+1 +``` + +This will refresh the module that is listening for signal `1`. To kill ecl +completely, just run `ecl_kill` (after setting the above alias). diff --git a/data/example_config.yml b/data/example_config.yml index 6f9a7a8..f5695af 100644 --- a/data/example_config.yml +++ b/data/example_config.yml @@ -62,8 +62,7 @@ bar: # Prefix to be printed before the text of the command. prefix: "DATE: " # Linux real-time signal that, when received, will refresh the module - # instantly. For example, if you set this to `1`, run - # `pkill -RTMIN+1 easyconf-lemonbar`. + # instantly. See docs for more info. signal: 0 # Formatting options. These will only affect the text and padding of this # module. diff --git a/easyconf-lemonbar/main.py b/easyconf-lemonbar/main.py index cb1d88c..2a00bbe 100644 --- a/easyconf-lemonbar/main.py +++ b/easyconf-lemonbar/main.py @@ -1,8 +1,14 @@ +from signal import SIGTERM, signal +from pidfile import delete_pidfile, pidfile_name, create_pidfile from SignalRouter import SignalRouter from Bar import Bar from Module import Module from parse_config_file import get_bar_config_and_module_config_list +def sigterm_handler(signal, frame): + delete_pidfile(pidfile_name()) + exit() + def main(): bar_config, module_config_list = get_bar_config_and_module_config_list("./data/testing_config.yml") padding = bar_config["padding"] @@ -20,7 +26,9 @@ def main(): if module.signal != 0: signal_router.register_signal_thread(module.signal, module.ident) + create_pidfile(pidfile_name()) + signal(SIGTERM, sigterm_handler) if __name__ == "__main__": main() diff --git a/easyconf-lemonbar/pidfile.py b/easyconf-lemonbar/pidfile.py new file mode 100644 index 0000000..f74be0c --- /dev/null +++ b/easyconf-lemonbar/pidfile.py @@ -0,0 +1,14 @@ +from os import chmod, getpid, getuid, remove + +def pidfile_name(): + filepath = "/run/user/" + str(getuid()) + "/easyconf-lemonbar.pid" + return filepath + + +def create_pidfile(filepath): + with open(filepath, "w") as file: + file.write(str(getpid())) + chmod(filepath, 0o644) + +def delete_pidfile(filepath): + remove(filepath)