Sfoglia il codice sorgente

some extended functionality

master
Dirk Alders 1 anno fa
parent
commit
ff0434370a
1 ha cambiato i file con 27 aggiunte e 12 eliminazioni
  1. 27
    12
      __init__.py

+ 27
- 12
__init__.py Vedi File

@@ -2,11 +2,13 @@ import bottombar as bb
2 2
 import readchar
3 3
 import string
4 4
 
5
+DEBUG = False
5 6
 
6 7
 class BottomBar(object):
7 8
     FUNC_QUIT = 0
8
-    FUNC_BOOL = 1
9
-    FUNC_TEXT = 2
9
+    FUNC_INFO = 1
10
+    FUNC_BOOL = 2
11
+    FUNC_TEXT = 3
10 12
     #
11 13
     F_KEYS = {
12 14
         1: '\x1bOP',
@@ -34,18 +36,20 @@ class BottomBar(object):
34 36
     def add_entry(self, name, f_key, function, *args, **kwargs):
35 37
         if len(args) == 0:
36 38
             args = ("", )
37
-        # add bb arguments for later barentry creation
38
-        self.__bb_args__[name] = (args, kwargs)
39 39
         # store data for entry
40 40
         if function in [self.FUNC_BOOL]:
41 41
             self.__bb_data__[name] = False
42 42
         elif function in [self.FUNC_TEXT]:
43 43
             self.__bb_data__[name] = ""
44
+        elif function in [self.FUNC_INFO]:
45
+            self.__bb_data__[name] = kwargs.pop('infotext')
44 46
         # store function and name for key
45 47
         f_inp = self.F_KEYS.get(f_key)
46 48
         if f_inp is not None:
47 49
             self.__key_to_func__[f_inp] = function
48 50
             self.__key_to_name__[f_inp] = name
51
+        # add bb arguments for later barentry creation
52
+        self.__bb_args__[name] = (args, kwargs)
49 53
 
50 54
     def get_entry(self, name, default=None):
51 55
         return self.__bb_data__.get(name, default)
@@ -53,8 +57,12 @@ class BottomBar(object):
53 57
     def edit(self, data):
54 58
         if data in string.ascii_letters or data in string.digits or data in string.punctuation:
55 59
             self.__bb_data__[self.edit_active] += data
60
+        elif data == "\x7f":
61
+            self.__bb_data__[self.edit_active] = self.__bb_data__[self.edit_active][:-1]
56 62
         elif data == "\n":
57 63
             self.edit_active = None
64
+        elif DEBUG:
65
+            print("unused keystroke in edit method:", repr(data))
58 66
 
59 67
     def run(self):
60 68
         for name in self.__bb_args__:
@@ -64,14 +72,17 @@ class BottomBar(object):
64 72
             # Update bar content
65 73
             for name in self.__bb_data__:
66 74
                 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
+                hotkey = [k for k, v in self.__key_to_name__.items() if v == name][0]
76
+                func = self.__key_to_func__[hotkey]
77
+                if func in [self.FUNC_BOOL, self.FUNC_TEXT]:
78
+                    if type(data) is type(True):
79
+                        data = "on" if data else "off"
80
+                    if name != self.edit_active:
81
+                        if data == "":
82
+                            data = "-"
83
+                    else:
84
+                        data += "\u2588"
85
+                    self.__bb_bar__[name].text = data
75 86
             # keystroke action
76 87
             data = readchar.readkey()
77 88
             if self.edit_active is not None:
@@ -81,7 +92,11 @@ class BottomBar(object):
81 92
                     name = self.__key_to_name__.get(data)
82 93
                     if self.__key_to_func__[data] is self.FUNC_QUIT:
83 94
                         break
95
+                    elif self.__key_to_func__[data] is self.FUNC_INFO:
96
+                        print(self.__bb_data__[name])
84 97
                     elif self.__key_to_func__[data] is self.FUNC_BOOL:
85 98
                         self.__bb_data__[name] = not self.__bb_data__[name]
86 99
                     elif self.__key_to_func__[data] is self.FUNC_TEXT:
87 100
                         self.edit_active = name
101
+                    elif DEBUG:
102
+                        print("unused keystroke in run method:", repr(data))

Loading…
Annulla
Salva