Advertisement
Guest User

Untitled

a guest
Oct 13th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.36 KB | None | 0 0
  1.  
  2. import curses
  3. import os
  4. from curses import wrapper
  5.  
  6.  
  7.  
  8. class EndConsole:
  9.  
  10. def __init__(self):
  11. self.generate_dims()
  12. self.create_records()
  13.  
  14. self.set_record(ab(5, 17))
  15. self.init_params()
  16. self.init_screen()
  17. self.create_colors()
  18. self.last_list_record = None
  19.  
  20. def start(self):
  21. self.start_loop()
  22.  
  23. def set_record(self, record):
  24. self.record = record
  25. self.cursor = (record[0], record[1])
  26. self.other_recs = []
  27.  
  28. def create_records(self, e_span=(-200,200), n_span=(-100,100), x_lim=(50)):
  29. self.record_dic = self.create_records_inner(e_span=e_span, n_span=n_span, x_lim=x_lim)
  30.  
  31. def create_records_inner(self, e_span=(-200,200), n_span=(-100,100), x_lim=(50)):
  32. dic = {}
  33. e_w = e_span[1] - e_span[0]
  34. for e in range(*e_span):
  35. if e % (e_w/10) == 0:
  36. print('.\n')
  37. for n in range(*n_span):
  38. for x in range(-x_lim, x_lim):
  39. x_test = 2*x + e%2
  40. if in_enx(e,n,x_test):
  41. rec = enx(e,n,x_test)
  42. if e not in dic.keys():
  43. dic[e] = {}
  44. if n not in dic[e].keys():
  45. dic[e][n] = []
  46. dic[e][n].append(rec)
  47. return dic
  48.  
  49. def populate_dictionary(self):
  50. E, N = self.cursor
  51. ee = (E - self.map_width, E + self.map_width)
  52. nn = (N - self.map_width, N + self.map_width)
  53. x_lim = self.list_x_search_depth
  54. more_recs = self.create_records_inner(ee, nn, x_lim)
  55. cur_dic = self.record_dic
  56. for e in more_recs.keys():
  57. if e not in cur_dic.keys():
  58. cur_dic[e] = {}
  59. for n in more_recs[e].keys():
  60. if n not in cur_dic[e].keys():
  61. cur_dic[e][n] = []
  62. cur_dic[e][n].extend(more_recs[e][n])
  63.  
  64. def in_dic(self, e, n):
  65. return e in self.record_dic.keys() and n in self.record_dic[e].keys()
  66.  
  67. #record must be set before calling this
  68. def init_params(self):
  69. self.show_pos_d = False
  70. self.show_neg_d = False
  71. self.show_pos_x = False
  72. self.show_neg_x = False
  73. self.show_pos_a = False
  74. self.show_neg_a = False
  75. self.show_pos_b = False
  76. self.show_neg_b = False
  77. self.show_all_recs = True
  78. self.highlight_axis = False
  79. self.highlight_cursor = True
  80. self.highlight_first_entry = True
  81. self.highlight_marked = True
  82. self.highlight_followed = True
  83. self.list_x_search_depth = 100
  84. self.jump_shift = 5
  85. self.list_jump_shift = 3
  86. self.other_recs = []
  87. self.marked_coords = []
  88. self.cur_indexer = self.record[3]
  89. self.grids_to_highlight = []
  90. self.grid_highlight_colors = [15, 16, 13, 12, 14]
  91. self.show_highlighted_grids = True
  92.  
  93. def generate_dims(self):
  94.  
  95. #This is designed to work so that if you only
  96. #modify these two values, it will all workout
  97.  
  98. wind = True
  99. if wind:
  100. self.max_width = 240
  101. self.max_height = 60
  102. else:
  103. self.max_width = 185
  104. self.max_height = 50
  105.  
  106. #detail view:
  107. #this is the bottom view panel where you can track records
  108. self.column_detail_width = 12
  109. self.detail_strings = ['ID','e','n','d','x','a','b','c','t','f','(d+n)','(x+n)']
  110. self.detail_ids = ['%','0','1','2','3','4','5','6','7','8','9','Z','Y','W','V','U','T','S','R','Q']
  111. self.detail_row_height = len(self.detail_strings)
  112.  
  113. self.max_detail_row_entries = 160 / self.column_detail_width
  114. self.detail_total_width = self.max_width
  115. self.detail_total_height = self.detail_row_height # * self.detail_row_count
  116.  
  117. #list view (right side)
  118. self.list_view_strings = self.detail_strings[1:]
  119. self.list_cell_width = 6 # num of digits to show in right list
  120. self.list_cell_max_width = 8
  121. self.list_view_height = self.max_height - self.detail_total_height
  122. self.list_view_values = self.detail_strings #likely to change
  123.  
  124. # The +1 below is to account for the '|' splitting the cells apart -1 for removing ID
  125. self.list_view_width = (self.list_cell_max_width + 1) * (len(self.list_view_values)- 1)
  126.  
  127. self.map_width = self.max_width - self.list_view_width
  128. self.map_height = self.list_view_height
  129.  
  130. def init_screen(self):
  131. self.screen = curses.initscr()
  132.  
  133. self.map_pad = curses.newpad(self.map_height, self.map_width)
  134. self.list_pad = curses.newpad(self.list_view_height, self.list_view_width)
  135. self.detail_pad = curses.newpad(self.detail_total_height, self.detail_total_width)
  136.  
  137. self.screen.keypad(True)
  138. self.screen.immedok(True)
  139. curses.noecho()
  140. curses.cbreak()
  141. curses.resizeterm(self.max_height, self.max_width)
  142.  
  143.  
  144. def parse_words(self):
  145. curses.echo()
  146. input_words = self.screen.getstr().split()
  147. if not input_words:
  148. return
  149. try:
  150. prompt = input_words[0].lower()
  151. rest = input_words[1:]
  152. if prompt == 'help':
  153. if rest:
  154. self.do_help(rest[0])
  155. else:
  156. self.do_help('basic')
  157. elif prompt == 'hl':
  158. if str(rest[0]).lower() in ('d','x','a','c','f','(d+n)','(x+n)','x+n','d+n','e','n','t'):
  159. self.grids_to_highlight.append((rest[0], int(rest[1])))
  160. else:
  161. rest = [int(ttt) for ttt in input_words[1:]]
  162. if prompt == 'ab':
  163. self.set_record(ab(*[int(x) for x in rest[:2]]))
  164. elif prompt == 'end' and all([isinstance(x, int) for x in rest[:2]]):
  165. self.set_record(end(*rest[:3]))
  166. elif prompt == 'enx' and all([isinstance(x, int) for x in rest[:2]]):
  167. self.set_record(enx(*rest[:3]))
  168. elif prompt == 'abd' and all([isinstance(x, int) for x in rest[:2]]):
  169. self.set_record(abd(*rest[:3]))
  170. elif prompt == 'ena' and all([isinstance(x, int) for x in rest[:2]]):
  171. self.set_record(ena(*rest[:3]))
  172. elif prompt == 'mj':
  173. neww = rest[0]
  174. if isinstance(neww, int):
  175. self.jump_shift = neww
  176. elif prompt == 'lj':
  177. neww = rest[0]
  178. if isinstance(neww, int):
  179. self.list_jump_shift = neww
  180. elif prompt == 'xm':
  181. if isinstance(rest[0], int):
  182. self.list_x_search_depth = rest[0]
  183. elif prompt == 'quit':
  184. self.quit()
  185. elif prompt == 'en':
  186. new_curs = [int(mmm) for mmm in rest[:2]]
  187. self.cursor = new_curs
  188.  
  189. except Exception as e:
  190. print('Something happened. Try again')
  191. pass
  192. curses.noecho()
  193.  
  194.  
  195. def in_marked_grids(self, E, N):
  196. for i in range(len(self.grids_to_highlight)):
  197. string, val = self.grids_to_highlight[i]
  198. if self.in_grid_from_things(E, N, string, val):
  199. return i
  200. return -1
  201.  
  202. def in_grid_from_things(self, e, n, grid_string, value):
  203. value = int(value)
  204. grid_string = grid_string.lower()
  205. if grid_string == 'd':
  206. return in_end(e, n, value)
  207. elif grid_string == 'x':
  208. return in_enx(e, n, value)
  209. elif grid_string == 'a':
  210. return in_ena(e, n, value)
  211. elif grid_string in ('(d+n)', 'd+n'):
  212. d = value - n
  213. return in_end(e, n, d)
  214. elif grid_string in ('(x+n)', 'x+n'):
  215. x = value - n
  216. return in_enx(e, n, x)
  217. elif grid_string == 'e':
  218. return e == value
  219. elif grid_string == 'n':
  220. return n == value
  221. elif grid_string == 't':
  222. x = x_from_t(value, e)
  223. if n==0:
  224. return x*x + e == 0
  225. else:
  226. return in_enx(e, n, x_from_t(value, e))
  227. elif grid_string == 'f':
  228. # f = 2d + 1 - e
  229. # f + e - 1 = 2d
  230. d2 = value + e - 1
  231. if not d2 % 2:
  232. d = d2 / 2
  233. return in_end(e, n, d)
  234. elif grid_string == 'c':
  235. dd = value - e
  236. if is_square(dd):
  237. d = int_sqrt(dd)
  238. return in_end(e, n, d)
  239.  
  240.  
  241. def do_help(self, arg):
  242. self.map_pad.clear()
  243. if arg == 'basic':
  244. lines = ['Type "help" followed by one of these ',
  245. 'keywords to learn more:', '',
  246. 'gestalt - basic gestalt',
  247. 'move - about movement',
  248. 'detail - about the bottom view',
  249. 'map - about map settings',
  250. 'commands - about terminal',
  251. 'press any button to return',
  252. '(tutorial step 1) >> gestalt'
  253. ]
  254. elif arg == 'gestalt':
  255. lines = ['So I made this because VQC organized the numbers in this grid',
  256. 'he put them in a grid for a reason and so I wanted to make',
  257. 'a way to study the grid',
  258. ' ',
  259. 'The area this message is taking up is',
  260. 'the map view. It shows the records based',
  261. 'on their e,n coordinates.',
  262. ' ',
  263. 'The area on the right is the list view ',
  264. 'this shows all of the records in the cell',
  265. 'that your cursor is on',
  266. ' ',
  267. 'The bottom area is the detail view. Here you',
  268. 'can select and save records here as you explore the map',
  269. ' ',
  270. '(tutorial step 2) >> commands'
  271. ]
  272. elif arg == 'commands':
  273. lines = ["To enter command mode, type '!' ",
  274. 'once in this mode you can do a number of things:',
  275. 'abd A B D - will change your current record to the record',
  276. ' with those coordinates',
  277. 'ab A B - similar to A B D',
  278. 'enx E N X',
  279. 'end E N D',
  280. 'ena E N A',
  281. 'en E N - will move your cursor to that spot',
  282. 'xm J - will change the max x depth that the program searches',
  283. ' for the list view with the pop command',
  284. 'mj J - changes the jump size to J for shift wasd',
  285. 'lj J - changes the list jump size to J for shift jk'
  286. ' ',
  287. ' (tutorial step 3) >> move'
  288. ]
  289.  
  290. elif arg == 'move':
  291. lines = ['-Use WASD to navigate the map',
  292. "-use JK to navigate the list view",
  293. "-If you hold shift while moving with these, it ",
  294. ' increases the movement size'
  295. "-To change either of these sizes use:",
  296. " 'mj' to change the map jump size",
  297. " 'lj' to change the list jump size",
  298. "-'f' will increase the current d value by one",
  299. " while shift+'f' will decrease it by one",
  300. "-'e' will go to e+2n, 'q' to e-2n",
  301. "-'H' returns your cursor to the starting record",
  302. " (tutorial step 4) >> detail"
  303. ]
  304. elif arg == 'detail':
  305. lines = [
  306. 'Your starting record is on the bottom left',
  307. 'You must have a starting record and it is set by default',
  308. 'h - sets the starting record to the current record',
  309. ' highlighted in the list view',
  310. 'i - adds the current highlighted record to the detail',
  311. ' view. shift + i will delete the last one added',
  312. 'enx, ena, end, abd, ab - will all change the starting',
  313. ' record to whichever is created from these coordinates',
  314. ' (tutorial step 5) >> map'
  315. ]
  316. elif arg == 'map':
  317. lines = [
  318. 'The view this is taking up is the map',
  319. 'o - mark a spot',
  320. 'O - unmark last marked spot. If you want to',
  321. ' unmark a different spot, mark it again, then unmark',
  322. 'm - toggle highlight marked spots',
  323. 'M - toggle highlight records in detail view',
  324. 'c - toggle highlight records in map with same ',
  325. ' d value as the starting record (C does -d)',
  326. 'v - toggle highlight records in map with same ',
  327. ' a value as the starting record (V does -a)',
  328. 'x - toggle highlight records on map with same ',
  329. ' x value as the starting record (X does -x)',
  330. 'z - toggle highlight all records',
  331. 'Z - toggle highlighted grids',
  332. 't - toggle highlight axis',
  333. 'T - toggle highlight cursor',
  334. 'p - refresh the map',
  335. 'hl X Y - will highlight records with the X value as Y',
  336. " ex: 'hl d 7' would highlight records with the",
  337. ' d value equal to 7. Values you can use are:',
  338. " (e, n, d, x, a, b, c, f, t,'(d+n)','(x+n)') ",
  339. "'-' - will delete the most recent highlighted grid",
  340. ' (tutorial done)'
  341. ]
  342.  
  343. for i in range(len(lines)):
  344. self.map_pad.addstr(i+2, 5, lines[i])
  345. self.map_pad.refresh(0, 0, 0, 0, self.map_height, self.map_width)
  346. self.screen.getch()
  347.  
  348.  
  349. def create_colors(self):
  350. curses.start_color()
  351. # 0 1 2 3
  352. self.colors = [curses.COLOR_WHITE, curses.COLOR_BLACK, curses.COLOR_RED, curses.COLOR_GREEN,
  353. curses.COLOR_BLUE, curses.COLOR_CYAN, curses.COLOR_MAGENTA, curses.COLOR_YELLOW]
  354.  
  355. curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) # Basic
  356. curses.init_pair(10, curses.COLOR_BLACK, curses.COLOR_WHITE) # Invert
  357. curses.init_pair(31, curses.COLOR_GREEN, curses.COLOR_BLACK) # Green On Black
  358. curses.init_pair(13, curses.COLOR_BLACK, curses.COLOR_GREEN) # Black on Green
  359. curses.init_pair(21, curses.COLOR_RED, curses.COLOR_BLACK) # Red on Black
  360. curses.init_pair(12, curses.COLOR_BLACK, curses.COLOR_RED) #Black on Red
  361. curses.init_pair(51, curses.COLOR_CYAN, curses.COLOR_BLACK) # Cyan on Black
  362. curses.init_pair(15, curses.COLOR_BLACK, curses.COLOR_CYAN) # Black on Cyan
  363. curses.init_pair(71, curses.COLOR_YELLOW, curses.COLOR_BLACK) # Yellow on Black
  364. curses.init_pair(17, curses.COLOR_BLACK, curses.COLOR_YELLOW) # Black on Yellow
  365. curses.init_pair(67, curses.COLOR_MAGENTA, curses.COLOR_YELLOW) # Magenta on Yellow
  366. curses.init_pair(76, curses.COLOR_YELLOW, curses.COLOR_MAGENTA) # Yellow on Magenta
  367. curses.init_pair(57, curses.COLOR_CYAN, curses.COLOR_YELLOW) # Cyan on Yellow
  368. curses.init_pair(75, curses.COLOR_YELLOW, curses.COLOR_CYAN) # Yellow on Cyan
  369. curses.init_pair(61, curses.COLOR_MAGENTA, curses.COLOR_BLACK) # Magenta on Black
  370. curses.init_pair(16, curses.COLOR_BLACK, curses.COLOR_MAGENTA) # Black on Magenta
  371. curses.init_pair(41, curses.COLOR_BLUE, curses.COLOR_BLACK)
  372.  
  373. self.color_dic = {
  374. 'basic' : 1,
  375. 'd_grid' : 31,
  376. 'x_grid' : 61,
  377. 'a_grid' : 21,
  378. 'b_grid' : 51,
  379. 'highlight': 16,
  380. 'list_view_highlight': 16,
  381. 'cursor' : 75,
  382. 'highlight_marked': 17
  383. }
  384.  
  385. def invert_color(self, color):
  386. modten = color % 10
  387. tens = (color - modten)/10
  388. return 10*modten + tens
  389.  
  390.  
  391.  
  392. def start_loop(self):
  393. while True:
  394. c = self.screen.getch()
  395. if c == ord('w'):
  396. self.cursor = self.cursor[0], self.cursor[1]-1
  397. elif c == ord('s'):
  398. self.cursor = self.cursor[0], self.cursor[1]+1
  399. elif c == ord('a'):
  400. self.cursor = self.cursor[0]-1, self.cursor[1]
  401. elif c == ord('d'):
  402. self.cursor = self.cursor[0]+1, self.cursor[1]
  403. elif c == ord('W'):
  404. self.cursor = self.cursor[0], self.cursor[1]-self.jump_shift
  405. elif c == ord('S'):
  406. self.cursor = self.cursor[0], self.cursor[1]+self.jump_shift
  407. elif c == ord('A'):
  408. self.cursor = self.cursor[0]-self.jump_shift, self.cursor[1]
  409. elif c == ord('D'):
  410. self.cursor = self.cursor[0]+self.jump_shift, self.cursor[1]
  411. ## GRIDS ##
  412. elif c == ord('x'):
  413. self.show_pos_x = not self.show_pos_x
  414. elif c == ord('X'):
  415. self.show_neg_x = not self.show_neg_x
  416. elif c == ord('c'):
  417. self.show_pos_d = not self.show_pos_d
  418. elif c == ord('C'):
  419. self.show_neg_d = not self.show_neg_d
  420. elif c == ord('v'):
  421. self.show_pos_a = not self.show_pos_a
  422. elif c == ord('V'):
  423. self.show_neg_a = not self.show_neg_a
  424. ## ## ## ##
  425.  
  426. elif c == ord('t'):
  427. self.highlight_axis = not self.highlight_axis
  428. elif c == ord('T'):
  429. self.highlight_cursor = not self.highlight_cursor
  430. elif c == ord('M'):
  431. self.highlight_followed = not self.highlight_followed
  432. elif c == ord('m'):
  433. self.highlight_marked = not self.highlight_marked
  434. elif c == ord('o'):
  435. new_spot = self.cursor
  436. if self.cursor in self.marked_coords:
  437. self.marked_coords.remove(new_spot)
  438. self.marked_coords.append(self.cursor)
  439. elif c == ord('O'):
  440. self.marked_coords = self.marked_coords[:-1]
  441. elif c == ord('j'):
  442. self.move_record_in_list(True, 1)
  443. elif c == ord('k'):
  444. self.move_record_in_list(False, 1)
  445. elif c == ord('J'):
  446. self.move_record_in_list(True, self.list_jump_shift)
  447. elif c == ord('K'):
  448. self.move_record_in_list(False, self.list_jump_shift)
  449. elif c == ord('h'):
  450. self.set_record_from_list()
  451. elif c == ord('i'):
  452. self.add_record_from_list()
  453. elif c == ord('I'):
  454. self.other_recs = self.other_recs[:-1]
  455. elif c == ord('z'):
  456. self.show_all_recs = not self.show_all_recs
  457. elif c == ord('Z'):
  458. self.show_highlighted_grids = not self.show_highlighted_grids
  459. elif c == ord('H'):
  460. self.cursor = self.record[:2]
  461. elif c == ord('-'):
  462. self.grids_to_highlight = self.grids_to_highlight[:-1]
  463. elif c == ord('f'):
  464. N = self.cursor[1]
  465. record = self.get_current_list_record()
  466. new_rec = abd(self.record[4], self.record[5], self.record[2]+1)
  467. self.cursor = new_rec[:2]
  468. if N != 0:
  469. self.cur_indexer += 1
  470. elif c == ord('F'):
  471. N = self.cursor[1]
  472. record = self.get_current_list_record()
  473. new_rec = abd(record[4], record[5], record[2]+1)
  474. self.cursor = new_rec[:2]
  475. if N != 0:
  476. self.cur_indexer -= 1
  477. elif c == ord('e'):
  478. E, N = self.cursor
  479. self.cursor = E + 2*N, N
  480. elif c == ord('q'):
  481. E,N = cursor
  482. self.cursor = E - 2*N, N
  483. elif c == ord('p'):
  484. self.populate_dictionary()
  485. elif c == ord('r'):
  486. self.cursor = -self.cursor[0], self.cursor[1]
  487. elif c == ord('R'):
  488. self.cursor = self.cursor[0], - self.cursor[1]
  489. elif c == ord('!'):
  490. self.parse_words()
  491. self.refresh_all()
  492.  
  493. def refresh_all(self):
  494. self.refresh_map()
  495. self.refresh_list()
  496. self.refresh_detail()
  497.  
  498. def refresh_map(self):
  499. self.map_pad.clear()
  500. e_start = self.cursor[0] - self.map_width / 2
  501. n_start = self.cursor[1] - self.map_height / 2
  502.  
  503. marked_indexes = [(rec[0], rec[1]) for rec in self.other_recs]
  504. marked_spots = self.marked_coords
  505. for e_s in range(self.map_width):
  506. e = e_start + e_s
  507. for n_s in range(self.map_height):
  508. n = n_start + n_s
  509. char = False
  510. if self.highlight_cursor and e == self.cursor[0] and n == self.cursor[1]:
  511. col_num = self.color_dic['cursor']
  512. character = '@'
  513. elif self.highlight_first_entry and e == self.record[0] and n == self.record[1]:
  514. col_num = self.color_dic['highlight']
  515. character = '%'
  516. elif self.highlight_followed and (e,n) in marked_indexes:
  517. col_num = self.color_dic['highlight']
  518. idx = marked_indexes.index((e,n))
  519. character = self.detail_ids[1:][idx]
  520. elif self.highlight_marked and (e,n) in marked_spots:
  521. col_num = self.color_dic['highlight_marked']
  522. character = '#'
  523. elif self.show_pos_d and in_end(e, n, self.record[2]):
  524. col_num = self.color_dic['d_grid']
  525. character = 'D'
  526. elif self.show_neg_d and in_end(e, n, -self.record[2]):
  527. col_num = self.color_dic['d_grid']
  528. character = 'd'
  529. elif self.show_pos_a and in_ena(e, n, self.record[4]):
  530. col_num = self.color_dic['a_grid']
  531. character = 'A'
  532. elif self.show_neg_a and in_ena(e, n, -self.record[4]):
  533. col_num = self.color_dic['a_grid']
  534. character = 'a'
  535. elif self.show_pos_x and in_enx(e, n, self.record[3]):
  536. col_num = self.color_dic['x_grid']
  537. character = 'X'
  538. elif self.show_neg_x and in_enx(e, n, -self.record[3]):
  539. col_num = self.color_dic['x_grid']
  540. character = 'x'
  541. elif self.show_highlighted_grids and self.in_marked_grids(e,n) != -1:
  542. iddd = self.in_marked_grids(e,n)
  543. col_num = self.grid_highlight_colors[iddd%len(self.grid_highlight_colors)]
  544. character = '='
  545. elif self.show_all_recs and self.in_dic(e, n):
  546. col_num = self.color_dic['basic']
  547. character = '+'
  548. else:
  549. col_num = self.color_dic['basic']
  550. character = ' '
  551.  
  552. if self.highlight_axis and (e == 0 or n == 0):
  553. col_num = self.invert_color(col_num)
  554.  
  555. try:
  556. self.map_pad.addch(n_s, e_s, ord(character), curses.color_pair(col_num))
  557. except:
  558. pass
  559.  
  560. self.map_pad.box()
  561. refresh_args = (0, 0, 0, 0, self.map_height, self.map_width)
  562. self.map_pad.refresh(*refresh_args)
  563.  
  564.  
  565.  
  566.  
  567. def refresh_list(self):
  568. self.list_pad.clear()
  569. cells = []
  570. E, N = self.cursor
  571. list_strings = self.list_view_strings
  572.  
  573. now_in_n0 = is_square(-E) and N==0
  574. if self.cur_indexer == None:
  575. self.cur_indexer = self.record[3]
  576.  
  577. if now_in_n0:
  578. check_func = in_ena
  579. gen_func = ena
  580. shift_amt = 1
  581. else:
  582. check_func = in_enx
  583. gen_func = enx
  584. if self.cur_indexer % 2 != E % 2:
  585. if E%2:
  586. self.cur_indexer += 1
  587. else:
  588. self.cur_indexer -= 1
  589. shift_amt = 2
  590.  
  591. search_depth = self.list_x_search_depth
  592. cells = []
  593. record_limit = self.list_view_height - 1
  594. good_index = 0
  595.  
  596. doing_pos = True
  597. doing_neg = True
  598.  
  599. i = 1
  600. good_spot = self.list_view_height / 2
  601. dracula = 0
  602. while(good_index != good_spot):
  603. t_shift_a = i * shift_amt
  604. if check_func(E, N, self.cur_indexer - t_shift_a):
  605. cells = [gen_func(E, N, self.cur_indexer - t_shift_a)] + cells
  606. good_index += 1
  607. dracula += 1
  608. i += 1
  609. if len(cells) == record_limit / 2 or dracula > self.list_x_search_depth:
  610. break
  611. dracula = 0
  612. i = 0
  613. while(len(cells) != record_limit and dracula < self.list_x_search_depth):
  614. t_shift_a = i * shift_amt
  615. if check_func(E, N, self.cur_indexer + t_shift_a):
  616. cells = cells + [gen_func(E, N, self.cur_indexer + t_shift_a)]
  617. dracula += 1
  618. i += 1
  619.  
  620. #cells is now all the records we want to show with good_index being the index of the highlighted one
  621.  
  622. words = self.lines_from_list(cells)
  623. for h in range(len(words)):
  624. try:
  625. if h == good_index + 1 or h == 0:
  626. self.list_pad.addstr(h, 0, str(words[h]), curses.color_pair(13))
  627. else:
  628. self.list_pad.addstr(h, 0, str(words[h]), curses.color_pair(1))
  629. except:
  630. pass
  631. refresh_args = (0, 0, 0, self.map_width, self.map_height, self.max_width)
  632. self.list_pad.refresh(*refresh_args)
  633.  
  634.  
  635. def lines_from_list(self, entries):
  636. words = self.list_view_strings
  637. str_rows = [words]
  638. for entry in entries:
  639. try:
  640. row_strs = self.values_from_record(entry, words)
  641. except:
  642. raise Exception(str(words))
  643. str_rows.append(row_strs)
  644. #str_rows = [words] + [self.values_from_record(entry, words) for entry in entries]
  645. def_width = self.list_cell_width
  646. max_widths = []
  647. for i in range(len(words)):
  648. max_w = len(words[i]) if len(words[i]) > def_width else def_width
  649. for str_row in str_rows:
  650. len_piece = str_row[i]
  651. if len(len_piece) > max_w:
  652. max_w = len(len_piece)
  653. max_widths.append(max_w)
  654. for i in range(len(max_widths)):
  655. if max_widths[i] > self.list_cell_max_width:
  656. max_widths[i] = self.list_cell_max_width
  657.  
  658.  
  659. disp_strings = []
  660. for row in str_rows:
  661. row_final = ''
  662. for i in range(len(words)):
  663. piece = row[i]
  664. row_final += ' '*(max_widths[i] - len(piece)+1) + piece + '|'
  665. disp_strings.append(row_final)
  666. return disp_strings
  667.  
  668. def move_record_in_list(self, up=True, big=False):
  669. if big == 0:
  670. self.refresh_list()
  671. return
  672. E,N = self.cursor
  673. now_in_n0 = is_square(-E) and N==0
  674. if big == False:
  675. big = 1
  676. if self.cur_indexer == None:
  677. if now_in_n0:
  678. self.cur_indexer = self.record[4]
  679. else:
  680. self.cur_indexer = self.record[3]
  681. shifter = self.cur_indexer
  682. if up:
  683. mult = 1
  684. else:
  685. mult = -1
  686. if now_in_n0:
  687. check_func = in_ena
  688. shift_amt = 1 * mult
  689. else:
  690. check_func = in_enx
  691. if shifter % 2 != E % 2:
  692. if E%2:
  693. shifter += 1
  694. else:
  695. shifter -= 1
  696. shift_amt = 2 * mult
  697.  
  698. shifter += shift_amt
  699. count = 0
  700. while(not check_func(E, N, shifter)):
  701. shifter += shift_amt
  702. count += 1
  703. if count > 100:
  704. return
  705. self.cur_indexer = shifter
  706. if big:
  707. self.move_record_in_list(up=up, big=big-1)
  708.  
  709.  
  710.  
  711. def get_current_list_record(self):
  712. E, N = self.cursor
  713. if self.cur_indexer:
  714. if is_square(-E) and N==0:
  715. return ena(E, N, self.cur_indexer)
  716. else:
  717. return enx(E, N, self.cur_indexer)
  718.  
  719.  
  720. def set_record_from_list(self):
  721. b = self.get_current_list_record()
  722. if b:
  723. self.record = b
  724. self.other_recs = []
  725. self.refresh_detail()
  726.  
  727. def add_record_from_list(self):
  728. b = self.get_current_list_record()
  729. if b:
  730. if len(self.other_recs) == self.max_detail_row_entries:
  731. return
  732. self.other_recs.append(b)
  733. self.refresh_detail()
  734.  
  735. def values_from_record(self, record, words):
  736. try:
  737. e,n,d,x,a,b = record
  738. except:
  739. raise Exception(str(record))
  740. ret_val = []
  741. for wordd in words:
  742. word = wordd.lower()
  743. if word == 'e':
  744. ret_val.append(str(e))
  745. elif word == 'n':
  746. ret_val.append(str(n))
  747. elif word == 'd':
  748. ret_val.append(str(d))
  749. elif word == 'x':
  750. ret_val.append(str(x))
  751. elif word == 'a':
  752. ret_val.append(str(a))
  753. elif word == 'b':
  754. ret_val.append(str(b))
  755. elif word == 'c':
  756. ret_val.append(str(a*b))
  757. elif word == 'f':
  758. ret_val.append(str(2*d + 1 - e))
  759. elif word in ('d+n', '(d+n)'):
  760. ret_val.append(str(d + n))
  761. elif word in ('x+n', '(x+n)'):
  762. ret_val.append(str(x + n))
  763. elif word == 't':
  764. ret_val.append(str(t_from_x(x)))
  765. else:
  766. ret_val.append(word)
  767. return ret_val
  768.  
  769.  
  770. def refresh_detail(self):
  771. self.detail_pad.clear()
  772. cur_rec = self.record
  773. recs = self.other_recs
  774. all_recs = [cur_rec] + recs
  775. ids = self.detail_ids[:len(all_recs)]
  776. vals_to_calc = self.detail_strings[1:] #cant calculate the id so it just goes off here
  777.  
  778. values = []
  779. for i in range(len(all_recs)):
  780. rec = all_recs[i]
  781. strs = [ids[i]] + self.values_from_record(rec, vals_to_calc)
  782. values.append(strs)
  783.  
  784. row_strings = []
  785. for i in range(len(self.detail_strings)):
  786. max_width = max([len(x) for x in self.detail_strings])
  787. ddddd = str(self.detail_strings[i]) + ' '*(max_width + 1 - len(str(self.detail_strings[i])))
  788. row_strings.append(ddddd)
  789.  
  790. for value in values:
  791. max_width = max([len(x) for x in value])
  792. if max_width < self.column_detail_width:
  793. max_width = self.column_detail_width
  794. for i in range(len(row_strings)):
  795. row_strings[i] += ' '*(max_width + 1 - len(str(value[i]))) + str(value[i]) + '|'
  796.  
  797. for i in range(len(row_strings)):
  798. try:
  799. color = 1
  800. if i == 0:
  801. color = 16
  802. self.detail_pad.addstr(i, 0, row_strings[i], curses.color_pair(color))
  803. except:
  804. pass
  805.  
  806. refresh_args = (0, 0, self.map_height, 0, self.max_height, self.detail_total_width)
  807. self.detail_pad.refresh(*refresh_args)
  808.  
  809.  
  810.  
  811. def x_from_t(t, e):
  812. if e%2==0:
  813. x = (t - 1) * 2
  814. else:
  815. x = t*2 - 1
  816. return x
  817.  
  818. def t_from_x(x):
  819. if x%2:
  820. return x/2 + 1
  821. else:
  822. return (x+1)/2
  823.  
  824. def int_sqrt(n):
  825. x = n
  826. y = (x + 1) // 2
  827. while y < x:
  828. x = y
  829. y = (x + n // x) // 2
  830. return x
  831.  
  832. def is_square(n):
  833. r = int_sqrt(n)
  834. return n == r*r
  835.  
  836. #### RECORD CREATION ####
  837.  
  838. def in_enx(e,n,x):
  839. if n == 0:
  840. return e + x*x == 0
  841. else:
  842. return (x*x + e) % (2*n) == 0
  843.  
  844. def enx(e,n,x):
  845. if n == 0:
  846. a = 0
  847. else:
  848. a = (x*x + e) / (2*n)
  849. b = a + 2*x + 2*n
  850. c = a * b
  851. d = a + x
  852. return (e,n,d,x,a,b)
  853.  
  854.  
  855. def in_end(e,n,d):
  856. c = d*d + e
  857. #dn*dn - xn*xn = c
  858. #dn*dn - c = xn*xn
  859. xpn_s = (d+n)**2 - c
  860. root = int_sqrt(xpn_s)
  861. return root*root == xpn_s
  862.  
  863. def end(e,n,d):
  864. c = d*d + e
  865. x = int_sqrt((d+n)**2 - c) - n
  866. a = d - x
  867. b = a + 2*x + 2*n
  868. return (e,n,d,x,a,b)
  869.  
  870. def in_ena(e,n,a):
  871. xx = 2*n*a - e
  872. root = int_sqrt(xx)
  873. return xx == root*root
  874.  
  875. def ena(e,n,a):
  876. xx = 2*n*a - e
  877. x = int_sqrt(xx)
  878. d = a + x
  879. b = a + 2*x + 2*n
  880. return (e,n,d,x,a,b)
  881.  
  882. def abd(a,b,d):
  883. c = a * b
  884. e = c - d*d
  885. x = d - a
  886. n = (b - a)/2 - x
  887. return (e,n,d,x,a,b)
  888.  
  889. def ab(a,b):
  890. c = a * b
  891. d = int_sqrt(c)
  892. e = c - d*d
  893. n = (b+a)/2 - d
  894. x = d - a
  895. return (e,n,d,x,a,b)
  896.  
  897.  
  898.  
  899. '''
  900. SDOFSDFLJKSDF
  901. SDfaSDF
  902. SDFSDf
  903.  
  904.  
  905. asdfasdfas
  906.  
  907. asdfasdfas
  908. a*bsdfasd
  909. failsdf
  910. asdfasdfas
  911.  
  912.  
  913. '''
  914.  
  915.  
  916.  
  917. if __name__ == '__main__':
  918. x = EndConsole()
  919. wrapper(x.start())
  920. x = wrapper(EndConsole())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement