-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_linux.py
199 lines (159 loc) · 5.43 KB
/
main_linux.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
#! /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
#######################################################################
# Classes
#######################################################################
#######################################################################
# Functions
#######################################################################
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
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(search_linum, 5, prompt + str_1 + str_2)
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:
str_1 = str_1[:-1]
# Enter
elif ch == 10:
open_file(res[selected][0])
stdscr.clear()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
# Excape
elif ch == 27:
break
# Delete
elif ch == curses.KEY_DC:
str_2 = str_2[1:]
# Left arrow key
elif ch == curses.KEY_LEFT:
if len(str_1) > 0:
str_2 = str_1[-1] + str_2
str_1 = str_1[:-1]
# Right arrow key
elif ch == curses.KEY_RIGHT:
if len(str_2) > 0:
str_1 = str_1 + str_2[0]
str_2 = str_2[1:]
# Down arrow key
elif ch == curses.KEY_DOWN:
if selected < 9 and selected < len(res) - 1:
selected += 1
continue
# Up arrow key
elif ch == curses.KEY_UP:
if selected > 0:
selected -= 1
continue
# Any other character
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 default_main():
while True:
intext = input()
if intext == "quit()":
quit()
docs = query.query(intext)
for doc in docs[:10]:
print(doc[0])
if __name__ == "__main__":
init.init()
if sys.platform == "linux" or sys.platform == "linux2":
wrapper(display_init)
elif sys.platform == "win32":
default_main()