-
Notifications
You must be signed in to change notification settings - Fork 2
/
StatusBar.py
159 lines (141 loc) · 4.78 KB
/
StatusBar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
############################################################################
#
# StatusBar.py
# curses window (container) for status bar
#
############################################################################
import curses
import utils
from Keys import Keys
import config
class StatusBar:
def __init__(self, win):
# curses window we're living in
self.win = win
_, self.cols = self.win.getmaxyx()
# text to display
self.text = ''
# search text when searching
self.search_text = ''
# command text when commanding
self.command_text = ''
# attribute for display, default reverse for contrast
self.attr = curses.A_REVERSE
# flag for search in progress
self.searching = False
# flag for command in progress
self.command_mode = False
self.refresh()
def debugger(self, s='', state=False, log=None):
if not log:
log = config.logfile
with open(log, 'a') as f:
print(s, file=f)
if state:
print(f'self.text: {self.text}\n'
+ f'self.search_text: {self.search_text}\n'
+ f'self.command_text: {self.command_text}\n'
+ f'self.searching: {self.searching}\n'
+ f'self.command_mode: {self.command_mode}\n',
file=f)
def refresh(self, text=None):
if text == None: # default
text = self.text
self.win.erase()
curses.curs_set(0) # hide cursor
# pad with spaces to light up whole bar
display = text + ' '*self.cols
self.win.insstr( 0,0, display, self.attr )
self.win.refresh()
def preview_ID(self, ID):
if ID: # may be None
title = utils.get_title(ID)
text = ID.ljust(9) + title
self.refresh(text)
def set(self, text):
self.text = text
self.refresh()
def start_search(self):
self.searching = True
self.text = '/'
self.search_text = ''
self.refresh()
def end_search(self):
self.searching = False
self.text = ''
self.refresh()
def start_command(self):
self.command_mode = True
self.command_text = ''
def end_command(self):
self.command_mode = False
self.text = ''
self.refresh()
def exec_command(self):
self.command_mode = False
self.text = ''
instruction = None # instructions to pass back to zk.py
if 'protograph' in self.command_text:
if 'directed' in self.command_text:
directed = True
self.text = 'protographing (directed) ...'
else:
directed = False
self.text = 'protographing ...'
self.refresh()
try:
utils.protograph(directed)
self.text = ''
except Exception as e:
self.text = 'ERROR: ' + str(e)
else:
instruction = self.command_text
self.refresh()
return instruction
def echo(self, k):
s = ''
# search known keys to see if we have a name for it
known_keys = Keys.__dict__
for key in known_keys.keys():
if known_keys[key] == k:
s = key
# if no name found, just echo the character
if not s:
s = chr(k)
self.text = s
self.refresh()
def insert(self, k):
if k == Keys.BACKSPACE:
# don't backspace '/' when searching or ':' in command mode
if self.searching and len(self.search_text) > 0:
self.text = self.text[:-1]
self.search_text = self.search_text[:-1]
elif self.command_mode and len(self.command_text) > 0:
self.text = self.text[:-1]
self.command_text = self.command_text[:-1]
elif 32 <= k <= 126: # only include "regular" characters
self.text += chr(k)
self.search_text += chr(k)
self.command_text += chr(k)
self.refresh()
def resize(self, screen_rows, screen_cols):
self.cols = screen_cols
self.win = curses.newwin( 1,screen_cols, screen_rows-1,0 )
self.refresh()
def keypress(self, k):
flag, val = None, None
if k == Keys.ESC:
if self.searching:
self.end_search()
elif self.command_mode:
self.end_command()
else:
self.echo(k)
elif self.searching or self.command_mode:
self.insert(k)
else:
self.echo(k)
return flag, val
def error(self, e):
text = 'ERROR: ' + str(e)
self.refresh(text)