-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
211 lines (185 loc) · 6.25 KB
/
interface.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
import curses
from swe import Backend
from database import Database
def reset_screen(stdscr):
stdscr.move(2, 0)
stdscr.clrtobot()
def gety(stdscr):
return stdscr.getyx()[0]
def getx(stdscr):
return stdscr.getyx()[1]
def display_wrap(stdscr, s):
for char in s:
max_x = stdscr.getmaxyx()[1]
if getx(stdscr) == max_x:
stdscr.move(gety(stdscr)+1, 0)
stdscr.addch(char)
def display_job_details(stdscr, job):
title = job.get('title')
company = job.get('company')
location = job.get('location')
description = job.get('description')
link = job.get('link')
reset_screen(stdscr)
stdscr.addstr('Title: ')
display_wrap(stdscr, title)
stdscr.addstr(gety(stdscr) + 2, 0, 'Company: ')
display_wrap(stdscr, company)
stdscr.addstr(gety(stdscr) + 2, 0, 'Location: ')
display_wrap(stdscr, location)
stdscr.addstr(gety(stdscr) + 2, 0, 'Description: ')
display_wrap(stdscr, description)
stdscr.addstr(gety(stdscr) + 2, 0, 'Link: ')
display_wrap(stdscr, link)
def display_ellipsis(stdscr, s, limit):
for char in s:
if getx(stdscr) == limit - 1:
stdscr.addch('\u2026')
return
stdscr.addch(char)
def display_table(stdscr, table):
stdscr.move(2, 2)
stdscr.addstr('ID', curses.A_UNDERLINE)
stdscr.move(2, 15)
stdscr.addstr('Title', curses.A_UNDERLINE)
stdscr.move(2, 50)
stdscr.addstr('Company', curses.A_UNDERLINE)
for i in range(len(table)):
id = str(table[i].get('id'))
stdscr.move(3 + i, 2)
display_ellipsis(stdscr, id, 15)
title = table[i].get('title')
stdscr.move(3 + i, 15)
display_ellipsis(stdscr, title, 50)
company = table[i].get('company')
stdscr.move(3 + i, 50)
display_ellipsis(stdscr, company, stdscr.getmaxyx()[1])
def get_country(stdscr):
countries_dict = {
"1": "Austria",
"2": "Brazil",
"3": "Canada",
"4": "France",
"5": "Germany",
"6": "India",
"7": "Italy",
"8": "Netherlands",
"9": "New Zealand",
"10": "Poland",
"11": "Russia",
"12": "Singapore",
"13": "South Africa",
"14": "United Kingdom",
"15": "United States",
}
stdscr.addstr('Enter country ID:\n')
for id, country in countries_dict.items():
stdscr.addstr(id + ": " + country + "\n")
id = stdscr.getstr().decode(stdscr.encoding)
stdscr.addstr('\n')
while id not in countries_dict.keys():
stdscr.move(gety(stdscr)-2, 0)
stdscr.clrtobot()
stdscr.addstr('Please enter a valid input\n')
stdscr.addstr(id+'\n')
id = stdscr.getstr().decode(stdscr.encoding)
return id
def get_valid_input(stdscr, accepted_inputs):
input = stdscr.getkey()
while input not in accepted_inputs:
stdscr.addstr('Please enter a valid input\r')
input = stdscr.getkey()
return input
def main(stdscr):
database = Database()
while True:
stdscr.clear()
stdscr.addstr('You are in the main menu.\n\n'
'Press s to search jobs.\n'
'Press v to view saved jobs.\n'
'Press q to quit.\n')
input = get_valid_input(stdscr, ['s', 'v', 'q'])
if input == 's':
search(stdscr, database)
elif input == 'v':
saved_jobs(stdscr, database)
elif input == 'q':
return
def search(stdscr, database):
stdscr.clear()
stdscr.addstr('You are in search.')
curses.echo()
reset_screen(stdscr)
# stdscr.addstr(2,0,'Enter a keyword to search: ')
# keyword = stdscr.getstr().decode(stdscr.encoding)
country = get_country(stdscr)
reset_screen(stdscr)
stdscr.addstr('Enter a location: ')
location = stdscr.getstr().decode(stdscr.encoding)
curses.noecho()
backend = Backend(country, location)
while True:
job = backend.next_job()
display_job_details(stdscr, job)
y = stdscr.getyx()[0]
stdscr.addstr(y+2, 0, 'Press n to go to the next job.\n'
'Press s to save this job.\n'
'Press m to return to the main menu.\n')
input = get_valid_input(stdscr, ['n', 's', 'm'])
if input == 's':
database.save_job(job)
elif input == 'm':
return
def saved_jobs(stdscr, database):
stdscr.clear()
stdscr.addstr('You are in your saved jobs.')
saved = database.get_saved_jobs()
page = 0
while True:
reset_screen(stdscr)
start = 5 * page
end = start + 5
if end >= len(saved):
end = len(saved)
display_table(stdscr, saved[start:end])
y = stdscr.getyx()[0]
stdscr.move(y+2, 0)
prompt = 'Press v to view a job.\n' \
'Press d to delete a job.\n'
accepted_inputs = ['v', 'd', 'm']
if end < len(saved):
prompt += 'Press n to go to the next page.\n'
accepted_inputs.append('n')
if start > 0:
prompt += 'Press p to go to the previous page.\n'
accepted_inputs.append('p')
prompt += 'Press m to return to the main menu.\n'
stdscr.addstr(prompt)
input = get_valid_input(stdscr, accepted_inputs)
if input == 'v':
curses.echo()
stdscr.addstr('Enter the id of the job you\'d like to view: ')
id = stdscr.getstr().decode(stdscr.encoding)
curses.noecho()
job = database.get_job(id)
if job is None:
continue
display_job_details(stdscr, job)
y = stdscr.getyx()[0]
stdscr.addstr(y+2, 0, 'Press r to return.\n')
get_valid_input(stdscr, ['r'])
elif input == 'd':
curses.echo()
stdscr.addstr('Enter the id of the job you\'d like to remove: ')
id = stdscr.getstr().decode(stdscr.encoding)
curses.noecho()
database.delete_job(id)
saved = database.get_saved_jobs()
elif input == 'n':
page += 1
elif input == 'p':
page -= 1
elif input == 'm':
return
if __name__ == '__main__':
curses.wrapper(main)