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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 'full_text' in plugin.status and (
  45. not plugin.conf['hide_ok'] or not plugin.hide
  46. ):
  47. outputs.append(plugin.status)
  48. print(self.format_output(outputs), flush=True)
  49. def run(self):
  50. while True:
  51. try:
  52. self.query()
  53. time.sleep(self.conf['output_freq'])
  54. except (KeyboardInterrupt, SystemExit):
  55. sys.exit()
  56. @staticmethod
  57. def format_i3wm(inputs):
  58. return json.dumps(inputs, ensure_ascii=False) + ','
  59. @staticmethod
  60. def format_term(inputs):
  61. return_info = list()
  62. for item in inputs:
  63. return_info.append(item['full_text'])
  64. return ' \033[1m|\033[0m '.join(return_info)
  65. def main():
  66. args = parse_arguments()
  67. plugin_runner = PluginRunner(args.conf)
  68. plugin_runner.start()
  69. time.sleep(0.1)
  70. plugin_runner.run()
  71. if __name__ == '__main__':
  72. main()