Browse Source

some extended functionality

master
Dirk Alders 1 year ago
parent
commit
ff0434370a
1 changed files with 27 additions and 12 deletions
  1. 27
    12
      __init__.py

+ 27
- 12
__init__.py View File

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

Loading…
Cancel
Save