-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
262 lines (216 loc) · 7.35 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#! /use/bin/python3
#######################################################################
# THE DISPLAY MODULE
#######################################################################
# This module sets up the GUI, using Python bindings for GTK3
#######################################################################
# External modules we use
#######################################################################
import curses
from curses import wrapper
import os
import sys
#######################################################################
# Modules of our own project
#######################################################################
# The initialization module
import init
# The query module
import query
# Global variables
import global_vars
# VOice search
import voice_recog
#######################################################################
# Classes
#######################################################################
#######################################################################
# Functions
#######################################################################
def exec_command(command):
if command[0] == ':':
command = command[1:]
if command == 'v':
text = voice_recog.recog()
return query.query(text)
elif command == 'q':
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
quit()
def open_file(file_name):
abs_name = global_vars._DOCS_PATH + file_name
if os.path.exists(abs_name) and os.path.isfile(abs_name):
if sys.platform == "win32":
os.system(abs_name)
elif sys.platform == "linux" or sys.platform == "linux2":
os.system('%s %s' % (os.getenv('EDITOR'), abs_name))
def display_init(stdscr):
# Start
stdscr.clear()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
header = ["***********************************************************************",
global_vars._NAME,
"AUTHORS : " + ', '.join(global_vars._AUTHORS),
"***********************************************************************"]
instructions = ["INSTRUCTIONS :",
"1) Type to search",
"2) Use the arrow keys to select the required file",
"3) DO NO resize the window",
"4) Press Enter when a file is selected to open it.",
"5) Press Esc to exit."]
directory = "DIRECTORY : {}".format(global_vars._DOCS_PATH)
prompt = "Search: "
str_1 = ""
str_2 = ""
stdscr.move(5, 5)
query.query("Hello")
search_linum = 14
display_linum = 16
res = []
selected = 0
command_mode = False
command_str_1 = ""
command_str_2 = ""
while (True):
y, x = stdscr.getmaxyx()
if x < 80 and y < 30:
curses.resizeterm(30, 80)
elif x < 80:
curses.resizeterm(y, 80)
elif y < 30:
curses.resizeterm(30, x)
stdscr.refresh()
stdscr.clear()
stdscr.addstr(0, 5, header[0])
stdscr.addstr(1, 5, header[1])
stdscr.addstr(2, 5, header[2])
stdscr.addstr(3, 5, header[3])
stdscr.addstr(5, 5, instructions[0])
stdscr.addstr(6, 5, instructions[1])
stdscr.addstr(7, 5, instructions[2])
stdscr.addstr(8, 5, instructions[3])
stdscr.addstr(9, 5, instructions[4])
stdscr.addstr(10, 5, instructions[5])
stdscr.addstr(12, 5, directory)
linum = display_linum
# Display the documents. Highlight the one selected
if len(res) > 0:
for (doc, score) in res[0:selected]:
stdscr.addstr(linum, 7, doc)
linum += 1
stdscr.addstr(linum, 7, res[selected][0], curses.A_REVERSE)
linum += 1
for (doc, score) in res[selected + 1:10]:
stdscr.addstr(linum, 7, doc)
linum += 1
stdscr.addstr(y - 1, 5, command_str_1 + command_str_2)
stdscr.addstr(search_linum, 5, prompt + str_1 + str_2)
if command_mode:
stdscr.move(y - 1, 5 + len(command_str_1))
else:
stdscr.move(search_linum, 5 + len(prompt) + len(str_1))
stdscr.refresh()
ch = stdscr.getch()
# Deal with resize first
if ch == curses.KEY_RESIZE:
pass
# Backspace
elif ch == 127:
if command_mode:
command_str_1 = command_str_1[:-1]
else:
str_1 = str_1[:-1]
# Enter
elif ch == 10:
if command_mode:
command_mode = False
res = exec_command(command_str_1 + command_str_2)
command_str_1 = ""
command_str_2 = ""
else:
open_file(res[selected][0])
stdscr.clear()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
selected = 0
continue
# Excape
elif ch == 27:
if command_mode:
command_str_1 = ""
command_str_2 = ""
command_mode = False
continue
else:
break
# Delete
elif ch == curses.KEY_DC:
if command_mode:
command_str_2 = command_str_2[1:]
else:
str_2 = str_2[1:]
# Left arrow key
elif ch == curses.KEY_LEFT:
if command_mode:
if len(command_str_1) > 0:
command_str_2 = command_str_1[-1] + command_str_2
command_str_1 = command_str_1[:-1]
else:
if len(str_1) > 0:
str_2 = str_1[-1] + str_2
str_1 = str_1[:-1]
continue
# Right arrow key
elif ch == curses.KEY_RIGHT:
if command_mode:
if len(command_str_2) > 0:
command_str_1 = command_str_1 + command_str_2[0]
command_str_2 = command_str_2[1:]
else:
if len(str_2) > 0:
str_1 = str_1 + str_2[0]
str_2 = str_2[1:]
continue
# Down arrow key
elif ch == curses.KEY_DOWN:
if not command_mode and selected < 9 and selected < len(res) - 1:
selected += 1
continue
# Up arrow key
elif ch == curses.KEY_UP:
if not command_mode and selected > 0:
selected -= 1
continue
# Colon for command mode
elif chr(ch) == ':':
if not command_mode and len(str_1 + str_2) == 0:
command_mode = True
command_str_1 = ':'
command_str_2 = ''
elif not command_mode:
str_1 += chr(ch)
else:
command_str_1 += chr(ch)
# Any other character
else:
if command_mode:
command_str_1 += chr(ch)
else:
str_1 += chr(ch)
selected = 0
res = query.query(str_1 + str_2)
# End
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
def main():
init.init()
wrapper(display_init)
if __name__ == "__main__":
main()