Explorar el Código

initial botombar wrapper implementation

master
Dirk Alders hace 1 año
padre
commit
e47647d7e9
Se han modificado 2 ficheros con 88 adiciones y 0 borrados
  1. 87
    0
      __init__.py
  2. 1
    0
      requirements.txt

+ 87
- 0
__init__.py Ver fichero

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

+ 1
- 0
requirements.txt Ver fichero

@@ -0,0 +1 @@
1
+bottombar

Loading…
Cancelar
Guardar