Wrapper for bottombar
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

__init__.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import bottombar as bb
  2. import readchar
  3. import string
  4. class BottomBar(object):
  5. FUNC_QUIT = 0
  6. FUNC_BOOL = 1
  7. FUNC_TEXT = 2
  8. #
  9. F_KEYS = {
  10. 1: '\x1bOP',
  11. 2: '\x1bOQ',
  12. 3: '\x1bOR',
  13. 4: '\x1bOS',
  14. 5: '\x1b[15~',
  15. 6: '\x1b[17~',
  16. 7: '\x1b[18~',
  17. 8: '\x1b[19~',
  18. 9: '\x1b[20~',
  19. 12: '\x1b[24~'
  20. }
  21. def __init__(self, *args, **kwargs):
  22. self.__bb_args__ = {}
  23. self.__bb_bar__ = {}
  24. self.__bb_data__ = {}
  25. self.__key_to_name__ = {}
  26. self.__key_to_func__ = {}
  27. self.edit_active = None
  28. #
  29. self.__bb_args__['__info__'] = (args, kwargs)
  30. def add_entry(self, name, f_key, function, *args, **kwargs):
  31. if len(args) == 0:
  32. args = ("", )
  33. # add bb arguments for later barentry creation
  34. self.__bb_args__[name] = (args, kwargs)
  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. # store function and name for key
  41. f_inp = self.F_KEYS.get(f_key)
  42. if f_inp is not None:
  43. self.__key_to_func__[f_inp] = function
  44. self.__key_to_name__[f_inp] = name
  45. def get_entry(self, name, default=None):
  46. return self.__bb_data__.get(name, default)
  47. def edit(self, data):
  48. if data in string.ascii_letters or data in string.digits or data in string.punctuation:
  49. self.__bb_data__[self.edit_active] += data
  50. elif data == "\n":
  51. self.edit_active = None
  52. def run(self):
  53. for name in self.__bb_args__:
  54. args, kwargs = self.__bb_args__.get(name, (None, None))
  55. self.__bb_bar__[name] = bb.add(*args, **kwargs)
  56. while True:
  57. # Update bar content
  58. for name in self.__bb_data__:
  59. data = self.__bb_data__[name]
  60. if type(data) is type(True):
  61. data = "on" if data else "off"
  62. if name != self.edit_active:
  63. if data == "":
  64. data = "-"
  65. else:
  66. data += "#"
  67. self.__bb_bar__[name].text = data
  68. # keystroke action
  69. data = readchar.readkey()
  70. if self.edit_active is not None:
  71. self.edit(data)
  72. else:
  73. if data in self.__key_to_func__:
  74. name = self.__key_to_name__.get(data)
  75. if self.__key_to_func__[data] is self.FUNC_QUIT:
  76. break
  77. elif self.__key_to_func__[data] is self.FUNC_BOOL:
  78. self.__bb_data__[name] = not self.__bb_data__[name]
  79. elif self.__key_to_func__[data] is self.FUNC_TEXT:
  80. self.edit_active = name