Wrapper for bottombar
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import bottombar as bb
  2. import os
  3. import readchar
  4. import string
  5. DEBUG = False
  6. class BottomBar(object):
  7. FUNC_QUIT = 0
  8. FUNC_INFO = 1
  9. FUNC_BOOL = 2
  10. FUNC_TEXT = 3
  11. #
  12. F_KEYS = {
  13. 1: '\x1bOP',
  14. 2: '\x1bOQ',
  15. 3: '\x1bOR',
  16. 4: '\x1bOS',
  17. 5: '\x1b[15~',
  18. 6: '\x1b[17~',
  19. 7: '\x1b[18~',
  20. 8: '\x1b[19~',
  21. 9: '\x1b[20~',
  22. 12: '\x1b[24~'
  23. }
  24. def __init__(self, *args, **kwargs):
  25. self.__bb_args__ = {}
  26. self.__bb_bar__ = {}
  27. self.__bb_data__ = {}
  28. self.__key_to_name__ = {}
  29. self.__key_to_func__ = {}
  30. self.edit_active = None
  31. #
  32. self.__bb_args__['__info__'] = (args, kwargs)
  33. def add_entry(self, name, f_key, function, *args, **kwargs):
  34. try:
  35. default = kwargs.pop('default')
  36. except KeyError:
  37. default = None
  38. if len(args) == 0:
  39. args = ("", )
  40. # store data for entry
  41. if function in [self.FUNC_BOOL]:
  42. self.__bb_data__[name] = default or False
  43. elif function in [self.FUNC_TEXT]:
  44. self.__bb_data__[name] = default or ""
  45. elif function in [self.FUNC_INFO]:
  46. self.__bb_data__[name] = kwargs.pop('infotext')
  47. # store function and name for key
  48. f_inp = self.F_KEYS.get(f_key)
  49. if f_inp is not None:
  50. self.__key_to_func__[f_inp] = function
  51. self.__key_to_name__[f_inp] = name
  52. # add bb arguments for later barentry creation
  53. self.__bb_args__[name] = (args, kwargs)
  54. def get_entry(self, name, default=None):
  55. return self.__bb_data__.get(name, default)
  56. def edit(self, data):
  57. if data in string.ascii_letters or data in string.digits or data in string.punctuation:
  58. self.__bb_data__[self.edit_active] += data
  59. elif data == "\x7f":
  60. self.__bb_data__[self.edit_active] = self.__bb_data__[self.edit_active][:-1]
  61. elif data == "\n":
  62. self.edit_active = None
  63. elif DEBUG:
  64. print("unused keystroke in edit method:", repr(data))
  65. def run(self):
  66. for name in self.__bb_args__:
  67. args, kwargs = self.__bb_args__.get(name, (None, None))
  68. self.__bb_bar__[name] = bb.add(*args, **kwargs)
  69. while True:
  70. # Update bar content
  71. for name in self.__bb_data__:
  72. data = self.__bb_data__[name]
  73. hotkey = [k for k, v in self.__key_to_name__.items() if v == name][0]
  74. func = self.__key_to_func__[hotkey]
  75. if func in [self.FUNC_BOOL, self.FUNC_TEXT]:
  76. if type(data) is type(True):
  77. data = "on" if data else "off"
  78. if name != self.edit_active:
  79. if data == "":
  80. data = "-"
  81. else:
  82. data += "\u2588"
  83. self.__bb_bar__[name].text = data
  84. # keystroke action
  85. data = readchar.readkey()
  86. if self.edit_active is not None:
  87. self.edit(data)
  88. else:
  89. if data in self.__key_to_func__:
  90. name = self.__key_to_name__.get(data)
  91. if self.__key_to_func__[data] is self.FUNC_QUIT:
  92. break
  93. elif self.__key_to_func__[data] is self.FUNC_INFO:
  94. print(self.__bb_data__[name])
  95. elif self.__key_to_func__[data] is self.FUNC_BOOL:
  96. self.__bb_data__[name] = not self.__bb_data__[name]
  97. elif self.__key_to_func__[data] is self.FUNC_TEXT:
  98. self.edit_active = name
  99. elif DEBUG:
  100. print("unused keystroke in run method:", repr(data))
  101. else:
  102. if data in ['c', ]:
  103. os.system('clear')
  104. elif data in ['q', ]:
  105. break