some extended functionality
This commit is contained in:
parent
e47647d7e9
commit
ff0434370a
25
__init__.py
25
__init__.py
@ -2,11 +2,13 @@ import bottombar as bb
|
||||
import readchar
|
||||
import string
|
||||
|
||||
DEBUG = False
|
||||
|
||||
class BottomBar(object):
|
||||
FUNC_QUIT = 0
|
||||
FUNC_BOOL = 1
|
||||
FUNC_TEXT = 2
|
||||
FUNC_INFO = 1
|
||||
FUNC_BOOL = 2
|
||||
FUNC_TEXT = 3
|
||||
#
|
||||
F_KEYS = {
|
||||
1: '\x1bOP',
|
||||
@ -34,18 +36,20 @@ class BottomBar(object):
|
||||
def add_entry(self, name, f_key, function, *args, **kwargs):
|
||||
if len(args) == 0:
|
||||
args = ("", )
|
||||
# add bb arguments for later barentry creation
|
||||
self.__bb_args__[name] = (args, kwargs)
|
||||
# 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)
|
||||
@ -53,8 +57,12 @@ class BottomBar(object):
|
||||
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__:
|
||||
@ -64,13 +72,16 @@ class BottomBar(object):
|
||||
# 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 += "#"
|
||||
data += "\u2588"
|
||||
self.__bb_bar__[name].text = data
|
||||
# keystroke action
|
||||
data = readchar.readkey()
|
||||
@ -81,7 +92,11 @@ class BottomBar(object):
|
||||
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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user