A simple replacement for i3status that is both extensible and customizable.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vdstatus 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python3
  2. import argparse
  3. import importlib
  4. import json
  5. import os
  6. import sys
  7. import time
  8. import yaml
  9. DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.yaml')
  10. DEFAULTS = {
  11. 'output_format': 'term',
  12. 'output_freq': 1,
  13. 'plugins': [{'name': 'date'}]
  14. }
  15. def parse_arguments():
  16. desc = ('A simple i3status replacement, '
  17. 'and more. Warning: WIP, may be broken.')
  18. parser = argparse.ArgumentParser(description=desc)
  19. parser.add_argument('-c', '--conf', default=DEFAULT_CONFIG,
  20. help='configuration file')
  21. return parser.parse_args()
  22. class PluginRunner:
  23. def __init__(self, config_file=DEFAULT_CONFIG):
  24. self.conf = dict()
  25. self.conf.update(DEFAULTS)
  26. with open(config_file) as config_data:
  27. self.conf.update(yaml.safe_load(config_data))
  28. self.plugins_loaded = list()
  29. self.format_output = self.format_term
  30. for plugin in self.conf['plugins']:
  31. mod = importlib.import_module('.' + plugin['name'], 'plugins')
  32. thread_object = mod.PluginThread(plugin)
  33. self.plugins_loaded.append(thread_object)
  34. def start(self):
  35. if self.conf['output_format'] == 'i3':
  36. print('{"version":1}\n[', flush=True)
  37. self.format_output = self.format_i3wm
  38. for plugin in self.plugins_loaded:
  39. plugin.start()
  40. def query(self):
  41. outputs = list()
  42. for plugin in self.plugins_loaded:
  43. if \
  44. not plugin.conf['hide_ok'] or \
  45. not plugin.hide:
  46. outputs.append(plugin.status)
  47. print(self.format_output(outputs), flush=True)
  48. def run(self):
  49. while True:
  50. try:
  51. self.query()
  52. time.sleep(self.conf['output_freq'])
  53. except (KeyboardInterrupt, SystemExit):
  54. sys.exit()
  55. @staticmethod
  56. def format_i3wm(inputs):
  57. return json.dumps(inputs, ensure_ascii=False) + ','
  58. @staticmethod
  59. def format_term(inputs):
  60. return_info = list()
  61. for item in inputs:
  62. return_info.append(item['full_text'])
  63. return ' \033[1m|\033[0m '.join(return_info)
  64. def main():
  65. args = parse_arguments()
  66. plugin_runner = PluginRunner(args.conf)
  67. plugin_runner.start()
  68. time.sleep(0.1)
  69. plugin_runner.run()
  70. if __name__ == '__main__':
  71. main()