import bottombar as bb import readchar import string DEBUG = False class BottomBar(object): FUNC_QUIT = 0 FUNC_INFO = 1 FUNC_BOOL = 2 FUNC_TEXT = 3 # F_KEYS = { 1: '\x1bOP', 2: '\x1bOQ', 3: '\x1bOR', 4: '\x1bOS', 5: '\x1b[15~', 6: '\x1b[17~', 7: '\x1b[18~', 8: '\x1b[19~', 9: '\x1b[20~', 12: '\x1b[24~' } def __init__(self, *args, **kwargs): self.__bb_args__ = {} self.__bb_bar__ = {} self.__bb_data__ = {} self.__key_to_name__ = {} self.__key_to_func__ = {} self.edit_active = None # self.__bb_args__['__info__'] = (args, kwargs) def add_entry(self, name, f_key, function, *args, **kwargs): if len(args) == 0: args = ("", ) # store data for entry if function in [self.FUNC_BOOL]: self.__bb_data__[name] = False elif function in [self.FUNC_TEXT]: self.__bb_data__[name] = "" elif function in [self.FUNC_INFO]: self.__bb_data__[name] = kwargs.pop('infotext') # store function and name for key f_inp = self.F_KEYS.get(f_key) if f_inp is not None: self.__key_to_func__[f_inp] = function self.__key_to_name__[f_inp] = name # add bb arguments for later barentry creation self.__bb_args__[name] = (args, kwargs) def get_entry(self, name, default=None): return self.__bb_data__.get(name, default) def edit(self, data): if data in string.ascii_letters or data in string.digits or data in string.punctuation: self.__bb_data__[self.edit_active] += data elif data == "\x7f": self.__bb_data__[self.edit_active] = self.__bb_data__[self.edit_active][:-1] elif data == "\n": self.edit_active = None elif DEBUG: print("unused keystroke in edit method:", repr(data)) def run(self): for name in self.__bb_args__: args, kwargs = self.__bb_args__.get(name, (None, None)) self.__bb_bar__[name] = bb.add(*args, **kwargs) while True: # Update bar content for name in self.__bb_data__: data = self.__bb_data__[name] hotkey = [k for k, v in self.__key_to_name__.items() if v == name][0] func = self.__key_to_func__[hotkey] if func in [self.FUNC_BOOL, self.FUNC_TEXT]: if type(data) is type(True): data = "on" if data else "off" if name != self.edit_active: if data == "": data = "-" else: data += "\u2588" self.__bb_bar__[name].text = data # keystroke action data = readchar.readkey() if self.edit_active is not None: self.edit(data) else: if data in self.__key_to_func__: name = self.__key_to_name__.get(data) if self.__key_to_func__[data] is self.FUNC_QUIT: break elif self.__key_to_func__[data] is self.FUNC_INFO: print(self.__bb_data__[name]) elif self.__key_to_func__[data] is self.FUNC_BOOL: self.__bb_data__[name] = not self.__bb_data__[name] elif self.__key_to_func__[data] is self.FUNC_TEXT: self.edit_active = name elif DEBUG: print("unused keystroke in run method:", repr(data))