Guest User

Untitled

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