initial botombar wrapper implementation
This commit is contained in:
parent
8e95519390
commit
e47647d7e9
87
__init__.py
Normal file
87
__init__.py
Normal file
@ -0,0 +1,87 @@
|
||||
import bottombar as bb
|
||||
import readchar
|
||||
import string
|
||||
|
||||
|
||||
class BottomBar(object):
|
||||
FUNC_QUIT = 0
|
||||
FUNC_BOOL = 1
|
||||
FUNC_TEXT = 2
|
||||
#
|
||||
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 = ("", )
|
||||
# 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] = ""
|
||||
# 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
|
||||
|
||||
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 == "\n":
|
||||
self.edit_active = None
|
||||
|
||||
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]
|
||||
if type(data) is type(True):
|
||||
data = "on" if data else "off"
|
||||
if name != self.edit_active:
|
||||
if data == "":
|
||||
data = "-"
|
||||
else:
|
||||
data += "#"
|
||||
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_BOOL:
|
||||
self.__bb_data__[name] = not self.__bb_data__[name]
|
||||
elif self.__key_to_func__[data] is self.FUNC_TEXT:
|
||||
self.edit_active = name
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
bottombar
|
Loading…
x
Reference in New Issue
Block a user