-
Notifications
You must be signed in to change notification settings - Fork 0
/
briar-0.1.1.py
311 lines (270 loc) · 11.4 KB
/
briar-0.1.1.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import os
import requests
import json
import curses
from curses import wrapper
from curses.textpad import Textbox, rectangle
from pathlib import Path
import sys, traceback
home = str(Path.home())
rel_path = ".briar/auth_token"
abs_file_path = os.path.join(home, rel_path)
apiURL = "http://127.0.0.1:7000"
auth = {"Authorization" : "Bearer " + open(abs_file_path, "r").read()}
def contacts():
url_format = apiURL + "/v1/contacts"
response = requests.get(url_format, headers = auth)
json_data = response.json()
contactList = []
for contact in json_data:
contactList.append(contact["author"]["name"])
return contactList
def messages(contactId):
url_format = apiURL + "/v1/messages/" + str(contactId)
response = requests.get(url_format, headers = auth )
json_data = response.json()
return json_data
def message(message):
#varibles for the other's person messages
color, x = 6, 1
#change variables if is our own messages
if message["local"] == True:
color, x = 5, 11
return color, x, message["text"]
def send(stdscr, contactId, contactName):
draw_window(stdscr, "Briar Linux Client", "Type your message and press Ctrl+G to send")
# Centering calculations
height, width = stdscr.getmaxyx()
z = 18
x, y = int((width // 2) - (z // 2) - z % 2), 3
stdscr.addstr(y-1,x,"New message for: " + contactName, curses.color_pair(2))
ncols, nlines = 40, 5
uly, ulx = 5, 2
editwin = curses.newwin(nlines, ncols, uly, ulx)
rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = Textbox(editwin)
# Let the user edit until Ctrl-G is struck.
box.edit()
# Get resulting contents
message = box.gather()
if message != "":
url_format = apiURL + "/v1/messages/" + contactId
response = requests.post(url_format, headers = auth, json = {"text":message} )
stdscr.addstr(20,10, "Message sent")
else:
stdscr.addstr(20,10, "Message empty", curses.color_pair(4) )
stdscr.refresh()
stdscr.getch()
messages(stdscr, contactId, contactName)
class CursedMenu(object):
'''A class which abstracts the horrors of building a curses-based menu system'''
def __init__(self):
'''Initialization'''
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
self.screen.keypad(1)
# Highlighted and Normal line definitions
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
self.highlighted = curses.color_pair(1)
self.normal = curses.A_NORMAL
def show(self, options, title="Title", subtitle="Subtitle"):
'''Draws a menu with the given parameters'''
self.set_options(options)
self.title = title.upper()
self.subtitle = subtitle.upper()
self.selected = 0
self.draw_menu()
def set_options(self, options):
'''Validates that the last option is "Exit"'''
if options[-1] is not 'Exit':
options.append('Exit')
self.options = options
def set_submenu(self, submenu):
'''Validates that the last option is "Exit"'''
if submenu[-1] is not 'Exit':
submenu.append('Exit')
self.submenu = submenu
def draw_dict(self):
cfg_dict = {}
cfg_dict['InstanceName'] = "InstanceName"
cfg_dict['Environment'] = "Enviroment"
cfg_dict['InstanceType'] = "InstanceType"
cfg_dict['SystemOwner'] = "SystemOwner"
cfg_dict['LifeCycle'] = "LifeCycle"
cfg_dict['DeptName'] = "DeptName"
cfg_dict['Org'] = "Org"
self.screen.addstr(8, 35, " "*43, curses.A_BOLD)
self.screen.addstr(10, 35," "*43, curses.A_BOLD)
self.screen.addstr(12, 35," "*43, curses.A_BOLD)
self.screen.addstr(14, 35," "*43, curses.A_BOLD)
self.screen.addstr(16, 35," "*43, curses.A_BOLD)
self.screen.addstr(18, 35," "*43, curses.A_BOLD)
self.screen.addstr(20, 35," "*43, curses.A_BOLD)
self.screen.addstr(8, 35, cfg_dict['InstanceName'], curses.A_STANDOUT)
self.screen.addstr(10, 35,cfg_dict['Environment'], curses.A_STANDOUT)
self.screen.addstr(12, 35,cfg_dict['InstanceType'], curses.A_STANDOUT)
self.screen.addstr(14, 35,cfg_dict['SystemOwner'], curses.A_STANDOUT)
self.screen.addstr(16, 35,cfg_dict['LifeCycle'], curses.A_STANDOUT)
self.screen.addstr(18, 35,cfg_dict['DeptName'], curses.A_STANDOUT)
self.screen.addstr(20, 35,cfg_dict['Org'], curses.A_STANDOUT)
self.screen.refresh()
def draw_menu(self):
'''Actually draws the menu and handles branching'''
request = ""
try:
while request is not "Exit":
self.draw()
request = self.get_user_input()
self.handle_request(request)
self.__exit__()
# Also calls __exit__, but adds traceback after
except Exception as exception:
self.__exit__()
traceback.print_exc()
def draw(self):
'''Draw the menu and lines'''
self.screen.border(0)
self.screen.subwin(7, 15, 3, 1).box()
self.screen.addstr(1,30, self.title, curses.A_STANDOUT|curses.A_BOLD) # Title for this menu
self.screen.hline(2, 1, curses.ACS_HLINE, 78)
self.screen.addstr(3,2, self.subtitle, curses.A_BOLD) #Subtitle for this menu
# Display all the menu items, showing the 'pos' item highlighted
y = 4
for index in range(len(self.options)):
menu_name = len(self.options[index])
textstyle = self.normal
if index == self.selected:
textstyle = self.highlighted
self.screen.addstr(y, 2, "%d.%s" % (index+1, self.options[index]), textstyle)
y += 1
#self.draw_dict()
self.screen.refresh()
def get_user_input(self):
'''Gets the user's input and acts appropriately'''
user_in = self.screen.getch() # Gets user input
'''Enter and Exit Keys are special cases'''
if user_in == 10:
return self.options[self.selected]
if user_in == 27:
return self.options[-1]
if user_in == (curses.KEY_END, ord('!')):
return self.options[-1]
# This is a number; check to see if we can set it
if user_in >= ord('1') and user_in <= ord(str(min(9,len(self.options)+1))):
self.selected = user_in - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
return
# Increment or Decrement
if user_in == curses.KEY_UP: # left arrow
self.selected -=1
if user_in == curses.KEY_DOWN: # right arrow
self.selected +=1
self.selected = self.selected % len(self.options)
return
def handle_request(self, request):
'''This is where you do things with the request'''
if request is "Contacts":
self.draw_submenu(request, contacts(), 8, 20, 3, 16)
elif request is "Groups":
self.org_func()
elif request is "Forums":
self.org_func()
elif request is "Blogs":
self.org_func()
if request is None: return
def draw_submenu(self, subtitle, submenu, lines, cols, h, w):
'''Actually draws the submenu and handles branching'''
c = None
self.option = 0
self.set_submenu(submenu)
height = len(self.submenu)
while c != 10:
self.s = curses.newwin(height+lines, cols, h, w)
self.s.keypad(1)
self.s.box()
self.s.addstr(0,1, subtitle.upper(), curses.A_BOLD) #Subtitle for this menu
for index in range(len(self.submenu)):
textstyle = self.normal
if index == self.option:
textstyle = self.highlighted
self.s.addstr(index+1,1, "%d-%s" % (index+1, self.submenu[index]), textstyle)
self.s.refresh()
c = self.s.getch() # Gets user input
# This is a number; check to see if we can set it
if c >= ord('1') and c <= ord(str(len(self.submenu)+1)):
self.option = c - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
# Increment or Decrement
elif c == curses.KEY_DOWN: # down arrow
if self.option < len(self.submenu):
self.option += 1
else: self.option = 0
elif c == curses.KEY_UP: # up arrow
if self.option > 0:
self.option -= 1
else: self.option = len(self.submenu)
if c == 10:
d = self.submenu[self.option] #TO-DO enviar id a la consulta de chats y mostrar venta
return self.draw_messages(self.submenu[self.option],messages(self.option+1),self.option+1)
def draw_messages(self, subtitle, messages, contactId):
'''Actually draws the submenu and handles branching'''
c = None
curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_CYAN)
curses.init_pair(6, curses.COLOR_WHITE, curses.COLOR_BLUE)
self.option = 0
height = 24
cols = 43
h = 3
w = 36
while c != 10:
self.s = curses.newwin(height, cols, h, w)
self.s.keypad(1)
self.s.box()
self.s.addstr(0,1, subtitle.upper(), curses.A_BOLD) #Subtitle for this menu
#ACA VIENEN LOS MENSAJES
y = 1
index = 1
if len(messages) > 22:
startline = len(messages) - 22
for msg in messages:
if index > startline:
color, x, text = message(msg)
self.s.attron(curses.color_pair(color))
self.s.addstr(y,x, text)
self.s.attroff(curses.color_pair(color))
y += 1
index += 1
#if y > 22:
# break
'''Dibujar el cuadro de texto para nuevos mensajes
send(contactId)
'''
#HASTA ACA
self.s.refresh()
c = self.s.getch() # Gets user input
# This is a number; check to see if we can set it
if c >= ord('1') and c <= ord(str(len(self.submenu)+1)):
self.option = c - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
# Increment or Decrement
elif c == curses.KEY_DOWN: # down arrow
if self.option < len(self.submenu):
self.option += 1
else: self.option = 0
elif c == curses.KEY_UP: # up arrow
if self.option > 0:
self.option -= 1
else: self.option = len(self.submenu)
if c == 10:
d = self.submenu[self.option] #TO-DO enviar id a la consulta de chats y mostrar ventana de chats con textbox para escribir mensaje
d = str(self.option+1)
self.s.addstr(6,1, d)
self.s.refresh()
self.s.getch()
return self.draw_submenu(self.submenu[self.option],messages(self.option+1, self.submenu[self.option]),8,20,3,16)
def __exit__(self):
curses.endwin()
os.system('clear')
'''demo'''
cm = CursedMenu()
cm.show(['Contacts','Groups','Forums','Blogs'], title='Briar Linux Client 0.1.1v', subtitle='Main Menu')