Wrapper for bottombar
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.

__init__.py 3.6KB

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