def _refresh_content(popwindow): files = os.listdir(path) header_pane = popwindow.children["header_pane"] poph, popw = popwindow.size() popcontent_pane = popwindow.children["content_pane"] header_pane.text = toColoredText(NORMAL_COLOR, f"{path}\n{'-' * (popw - 2)}") popcontent_pane._replace( toColoredText(NORMAL_COLOR, "\n".join([".."] + files)))
def create_window(popwindow): _, popw = popwindow.size() a_text = popwindow.text("a_text", 1, popw - 2, 1, 1, toColoredText(NORMAL_COLOR, f"a: {key_a}")) b_text = popwindow.text("b_text", 1, popw - 2, 2, 1, toColoredText(NORMAL_COLOR, f"b: {key_b}")) c_textfield = popwindow.textfield("c_textfield", popw - 2, 3, 1, "c:", "") popwindow.focus = "c_textfield"
def refresh_footer(window, config, mode): if window.focus == "left_pane" or window.focus == "right_pane" or window.focus == "bottom_pane": footer = toColoredText(NORMAL_COLOR, "ESCAPE exit") else: i = get_current_row_id(window.children["top_pane"]) + 1 n = get_total_rows(config, mode) if i > n: # i might be on the ... i = n footer = toColoredText(HIGHLIGHT_COLOR, f"{i} / {n}") + toColoredText( NORMAL_COLOR, f" {HELP_TEXT_SHORT}") window.set_footer(footer)
def refresh_bottom_panes(window, config, mode, force=False): name = config.table_names[mode.cache_tables.current_table] row = get_current_row(window.children["top_pane"], config, mode) if isinstance(mode, DiffMode): if row is not None: key_a, key_b, _, _, _, _ = row if key_a is None: dump_get_a = "" else: dump_get_a = mode.a_cache_file.fil.dump_get(name, key_a) if key_b is None: dump_get_b = "" else: dump_get_b = mode.b_cache_file.fil.dump_get(name, key_b) else: key_a = None key_b = None dump_get_a = "" dump_get_b = "" if force or key_a != mode.a_cache_file.old_key: mode.a_cache_file.old_key = key_a left_pane = window.children["left_pane"] left_pane._clear() left_pane._append(toColoredText(NORMAL_COLOR, dump_get_a)) if force or key_b != mode.b_cache_file.old_key: mode.b_cache_file.old_key = key_b right_pane = window.children["right_pane"] right_pane._clear() right_pane._append(toColoredText(NORMAL_COLOR, dump_get_b)) else: # focused mode if row is not None: key, _ = row if key is None: dump_get = "" else: dump_get = mode.cache_file.fil.dump_get(name, key) else: key = None dump_get = "" if force or key != mode.cache_file.old_key: mode.cache_file.old_key = key bottom_pane = window.children["bottom_pane"] bottom_pane._clear() bottom_pane._append(toColoredText(NORMAL_COLOR, dump_get)) refresh_footer(window, config, mode) window.update()
def enter_var_name(window, key_a, key_b): def create_window(popwindow): _, popw = popwindow.size() a_text = popwindow.text("a_text", 1, popw - 2, 1, 1, toColoredText(NORMAL_COLOR, f"a: {key_a}")) b_text = popwindow.text("b_text", 1, popw - 2, 2, 1, toColoredText(NORMAL_COLOR, f"b: {key_b}")) c_textfield = popwindow.textfield("c_textfield", popw - 2, 3, 1, "c:", "") popwindow.focus = "c_textfield" def key_handler(window, ch): if key_enter(ch): c_textfield = window.children["c_textfield"] return WindowExit(c_textfield.text) elif key_escape(ch): return WindowExit(None) else: return WindowPass() c = window.popup(toColoredText(NORMAL_COLOR, "ENTER confirm ESCAPE exit"), create_window, key_handler, h=5) return c
def menu(window, mode): def create_window(popwindow): poph, popw = popwindow.size() menu_pane = popwindow.pane("menu_pane", poph - 1, popw - 1, 1, 1, selectable=True) menu_pane.lines = toColoredText(NORMAL_COLOR, MENU_TEXT(mode)) popwindow.window.keypad(1) def key_handler(popwin, ch): if ch in menu_keys(mode): return WindowExit(ch) elif key_escape(ch): return WindowExit(None) elif key_enter(ch): menu_pane = popwin.children["menu_pane"] key_name = menu_pane.lines.toString().split("\n")[ menu_pane.current_document_y].split(" ")[0] key = next(c.key for c in COMMANDS if c.key_name == key_name) return WindowExit(key) else: return WindowPass() c = window.popup(toColoredText(NORMAL_COLOR, "ENTER confirm ESCAPE exit"), create_window, key_handler, w=max(map(len, MENU_TEXT(mode).split("\n"))) + 2) return c
def create_window(popwindow): popwindow.window.keypad(1) poph, popw = popwindow.size() header_pane = popwindow.text("header_pane", 2, popw - 2, 1, 1, toColoredText(NORMAL_COLOR, "")) popcontent_pane = popwindow.pane("content_pane", poph - 4, popw - 2, 3, 1, True) _refresh_content(popwindow)
def create_window(popwindow): poph, popw = popwindow.size() menu_pane = popwindow.pane("menu_pane", poph - 1, popw - 1, 1, 1, selectable=True) menu_pane.lines = toColoredText(NORMAL_COLOR, MENU_TEXT(mode)) popwindow.window.keypad(1)
def popup_text(window, text_content): def create_window(popwindow): poph, popw = popwindow.size() text = popwindow.text("text_pane", poph - 1, popw - 1, 1, 1, toColoredText(NORMAL_COLOR, text_content)) def key_handler(win, ch): return WindowExit(None) if key_escape(ch) else WindowPass() window.popup(toColoredText(NORMAL_COLOR, "ESCAPE exit"), create_window, key_handler, None)
def update_content(source, oc, c): nonlocal candidates_filtered popcontent_pane = popwindow.children["content_pane"] candidates_filtered = [ candidate for candidate in candidates if match(c, candidate) ] header, content, footer = f(candidates_filtered) content += toColoredText(NORMAL_COLOR, "\n") content += footer popcontent_pane._replace(content)
def pick_file(window, starting_path): path = os.path.abspath(starting_path) help_text_short = "UP DOWN PAGE UP PAGE DOWN navigate ENTER confirm ESCAPE exit" def _refresh_content(popwindow): files = os.listdir(path) header_pane = popwindow.children["header_pane"] poph, popw = popwindow.size() popcontent_pane = popwindow.children["content_pane"] header_pane.text = toColoredText(NORMAL_COLOR, f"{path}\n{'-' * (popw - 2)}") popcontent_pane._replace( toColoredText(NORMAL_COLOR, "\n".join([".."] + files))) def create_window(popwindow): popwindow.window.keypad(1) poph, popw = popwindow.size() header_pane = popwindow.text("header_pane", 2, popw - 2, 1, 1, toColoredText(NORMAL_COLOR, "")) popcontent_pane = popwindow.pane("content_pane", poph - 4, popw - 2, 3, 1, True) _refresh_content(popwindow) def key_handler(popwindow, ch): nonlocal path popcontent_pane = popwindow.children["content_pane"] if key_enter(ch): filename = popcontent_pane.lines.lines()[ popcontent_pane.current_document_y].toString() if filename == "..": path = os.path.dirname(path) _refresh_content(popwindow) return WindowContinue() else: path = os.path.join(path, filename) if os.path.isfile(path): return WindowExit(path) elif os.path.isdir(path): _refresh_content(popwindow) return WindowContinue() elif key_escape(ch): return WindowExit(None) else: return WindowPass() return window.popup(toColoredText(NORMAL_COLOR, help_text_short), create_window, key_handler, None)
def format_table(color, column_names, rows, upper_border=True, lower_border=True, left_border=True, right_border=True): columns = list(map(list, zip(*rows))) if len(rows) > 0 else [[] for _ in column_names] column_widths = [max(chain([len(column_name)], (len(a) for a in column if a is not None))) + 2 for column_name, column in zip(column_names, columns)] table_width = sum(column_widths) + len(column_widths) -1 + (1 if left_border else 0) + (1 if right_border else 0) if upper_border: header = toColoredText(color, "-" * table_width) header += toColoredText(color, "\n") else: header = ColoredText() header += (toColoredText(color, "|") if left_border else ColoredText()) + toColoredText(color, "|").join([column_name.center(color, w) for w, column_name in zip(column_widths, column_names)]) + (toColoredText(color, "|") if right_border else ColoredText()) header += toColoredText(color, "\n") header += toColoredText(color, "-" * table_width) contents = [(toColoredText(color, "|") if left_border else ColoredText()) + toColoredText(color, "|").join([x.center(color, w) for w, x in zip(column_widths, row)]) + (toColoredText(color, "|") if right_border else ColoredText()) for row in rows] content = toColoredText(color, "\n").join(contents) if lower_border: footer = toColoredText(color, "-" * table_width) else: footer = ColoredText() return header, content, footer
def colorize_diff(a: str, b: str) -> Tuple[ColoredText, ColoredText]: sm = difflib.SequenceMatcher(a=a, b=b, autojunk=False) opcodes = sm.get_opcodes() a_colorized = ColoredText() b_colorized = ColoredText() ab = HIGHLIGHT_COLOR bb = HIGHLIGHT_COLOR for tag, i1, i2, j1, j2 in opcodes: a_segment = a[i1:i2] b_segment = b[j1:j2] if tag == "equal": a_colorized += toColoredText(NORMAL_COLOR, a_segment) b_colorized += toColoredText(NORMAL_COLOR, b_segment) elif tag == "delete": a_colorized += toColoredText(ab, a_segment) elif tag == "insert": b_colorized += toColoredText(bb, b_segment) elif tag == "replace": a_colorized += toColoredText(ab, a_segment) b_colorized += toColoredText(bb, b_segment) return (a_colorized, b_colorized)
def handle_command(ch): nonlocal mode ntables = len(config.table_names) name = config.table_names[mode.cache_tables.current_table] row = get_current_row(window.children["top_pane"], config, mode) if ch == ord("."): mode.cache_tables.current_table += 1 mode.cache_tables.current_table %= ntables refresh(window, config, mode) elif ch == ord(","): mode.cache_tables.current_table += ntables - 1 mode.cache_tables.current_table %= ntables refresh(window, config, mode) elif ch == ord("i"): mode = focus_a(window, config) elif ch == ord("j"): mode = focus_b(window, config) elif ch == ord("k"): mode = diff_mode(window, config) logger.info(f"handle_command: mode = {mode}") elif ch == ord("n"): new_row(window, config, mode) elif ch == ord("h"): help(window, mode) refresh_content(window, config, mode) else: if isinstance(mode, DiffMode): if row is not None: if ch == ord("f"): key_b = row[1] a = mode.a_cache_file.fil.get_keys(name) candidates_a = find_candidates( a, key_b, config.similarity_threshold, config.max_entries, config.ignore_suffix) c = choose_candidate_ratio(window, candidates_a) if c is not None: candidate_a, ratio = c row[0] = candidate_a row[2] = ratio row[3] = "o" refresh_content(window, config, mode) elif ch == ord("g"): key_a = row[0] b = mode.b_cache_file.fil.get_keys(name) candidates_b = find_candidates( b, key_a, config.similarity_threshold, config.max_entries, config.ignore_suffix) c = choose_candidate_ratio(window, candidates_b) if c is not None: candidate_b, ratio = c row[1] = candidate_b row[2] = ratio row[4] = "o" refresh_content(window, config, mode) elif ch == ord("s"): row[5] = Noop() refresh_content(window, config, mode) else: if mode.a_cache_file.update is not None or mode.b_cache_file.update is not None: if ch == ord("c"): key_a, key_b, _, _, _, _ = row c = enter_var_name(window, key_a, key_b) if c is not None: row[5] = Customize(c) refresh_content(window, config, mode) elif ch == ord("u"): a_file = mode.a_cache_file.fil b_file = mode.b_cache_file.fil a_update = mode.a_cache_file.update b_update = mode.b_cache_file.update for name, ( table, _) in mode.cache_tables.tables.items(): window.set_footer( toColoredText( NORMAL_COLOR, f"updating table {name} ...")) for row in table: key_a, key_b, _, _, _, action = row action.update(name, key_a, a_file, key_b, b_file) row[5] = Noop() if a_update is not None: window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {a_update} ..." )) a_file.dump(a_update) config.a_updated = True if b_update is not None: window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {b_update} ..." )) b_file.dump(b_update) config.b_updated = True reload_files(window, config, mode) else: if mode.b_cache_file.update is not None: if ch == ord("a"): row[5] = UseA() refresh_content(window, config, mode) elif ch == ord("d"): key_a, key_b, _, _, _, _ = row c = enter_var_name( window, key_a, key_b) if c is not None: row[5] = CustomizeB(c) refresh_content( window, config, mode) elif ch == ord("r"): edit_right(window, config, mode) elif ch == ord("w"): filename = pick_file(window, ".") if filename is not None: b_file = mode.b_cache_file.fil a_file = mode.a_cache_file.fil mappings = extract_mappings( b_file, filename) _, key_b, _, _, _, _ = row b_file.update_value( name, key_b, mappings) refresh_bottom_panes(window, config, mode, force=True) if mode.a_cache_file.update is not None: if ch == ord("b"): row[5] = UseB() refresh_content(window, config, mode) elif ch == ord("e"): key_a, key_b, _, _, _, _ = row c = enter_var_name( window, key_a, key_b) if c is not None: row[5] = CustomizeA(c) refresh_content( window, config, mode) elif ch == ord("l"): edit_left(window, config, mode) elif ch == ord("v"): filename = pick_file(window, ".") if filename is not None: b_file = mode.b_cache_file.fil a_file = mode.a_cache_file.fil mappings = extract_mappings( a_file, filename) key_a, _, _, _, _, _ = row a_file.update_value( name, key_a, mappings) refresh_bottom_panes(window, config, mode, force=True) else: # focused mode if row is not None: if ch == ord("f"): keys = mode.cache_file.fil.get_keys(name) c = choose_candidate(window, keys) if c is not None: row[0] = c refresh_content(window, config, mode) elif ch == ord("s"): row[1] = Noop() refresh_content(window, config, mode) else: if mode.cache_file.update is not None: if ch == ord("e"): key, _ = row c = enter_var_name1(window, key) if c is not None: row[1] = CustomizeA(c) refresh_content(window, config, mode) elif ch == ord("l"): edit_bottom(window, config, mode) elif ch == ord("u"): fil = mode.cache_file.fil update = mode.cache_file.update for name, ( table, _) in mode.cache_tables.tables.items(): window.set_footer( toColoredText( NORMAL_COLOR, f"updating table {name} ...")) for row in table: key, action = row action.update(name, key, fil, None, None) row[1] = Noop() window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {update} ...")) fil.dump(update) if mode.a_focused: config.a_updated = True else: config.b_updated = True reload_files(window, config, mode) elif ch == ord("v"): filename = pick_file(window, ".") if filename is not None: mappings = extract_mappings( mode.cache_file.fil, filename) key, _ = row mode.cache_file.fil.update_value( name, key, mappings) refresh_bottom_panes(window, config, mode, force=True)
def print_matches(window, config, init_mode): mode = init_mode setFocusByName(window, "top_pane") reload_files(window, config, mode) def handle_command(ch): nonlocal mode ntables = len(config.table_names) name = config.table_names[mode.cache_tables.current_table] row = get_current_row(window.children["top_pane"], config, mode) if ch == ord("."): mode.cache_tables.current_table += 1 mode.cache_tables.current_table %= ntables refresh(window, config, mode) elif ch == ord(","): mode.cache_tables.current_table += ntables - 1 mode.cache_tables.current_table %= ntables refresh(window, config, mode) elif ch == ord("i"): mode = focus_a(window, config) elif ch == ord("j"): mode = focus_b(window, config) elif ch == ord("k"): mode = diff_mode(window, config) logger.info(f"handle_command: mode = {mode}") elif ch == ord("n"): new_row(window, config, mode) elif ch == ord("h"): help(window, mode) refresh_content(window, config, mode) else: if isinstance(mode, DiffMode): if row is not None: if ch == ord("f"): key_b = row[1] a = mode.a_cache_file.fil.get_keys(name) candidates_a = find_candidates( a, key_b, config.similarity_threshold, config.max_entries, config.ignore_suffix) c = choose_candidate_ratio(window, candidates_a) if c is not None: candidate_a, ratio = c row[0] = candidate_a row[2] = ratio row[3] = "o" refresh_content(window, config, mode) elif ch == ord("g"): key_a = row[0] b = mode.b_cache_file.fil.get_keys(name) candidates_b = find_candidates( b, key_a, config.similarity_threshold, config.max_entries, config.ignore_suffix) c = choose_candidate_ratio(window, candidates_b) if c is not None: candidate_b, ratio = c row[1] = candidate_b row[2] = ratio row[4] = "o" refresh_content(window, config, mode) elif ch == ord("s"): row[5] = Noop() refresh_content(window, config, mode) else: if mode.a_cache_file.update is not None or mode.b_cache_file.update is not None: if ch == ord("c"): key_a, key_b, _, _, _, _ = row c = enter_var_name(window, key_a, key_b) if c is not None: row[5] = Customize(c) refresh_content(window, config, mode) elif ch == ord("u"): a_file = mode.a_cache_file.fil b_file = mode.b_cache_file.fil a_update = mode.a_cache_file.update b_update = mode.b_cache_file.update for name, ( table, _) in mode.cache_tables.tables.items(): window.set_footer( toColoredText( NORMAL_COLOR, f"updating table {name} ...")) for row in table: key_a, key_b, _, _, _, action = row action.update(name, key_a, a_file, key_b, b_file) row[5] = Noop() if a_update is not None: window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {a_update} ..." )) a_file.dump(a_update) config.a_updated = True if b_update is not None: window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {b_update} ..." )) b_file.dump(b_update) config.b_updated = True reload_files(window, config, mode) else: if mode.b_cache_file.update is not None: if ch == ord("a"): row[5] = UseA() refresh_content(window, config, mode) elif ch == ord("d"): key_a, key_b, _, _, _, _ = row c = enter_var_name( window, key_a, key_b) if c is not None: row[5] = CustomizeB(c) refresh_content( window, config, mode) elif ch == ord("r"): edit_right(window, config, mode) elif ch == ord("w"): filename = pick_file(window, ".") if filename is not None: b_file = mode.b_cache_file.fil a_file = mode.a_cache_file.fil mappings = extract_mappings( b_file, filename) _, key_b, _, _, _, _ = row b_file.update_value( name, key_b, mappings) refresh_bottom_panes(window, config, mode, force=True) if mode.a_cache_file.update is not None: if ch == ord("b"): row[5] = UseB() refresh_content(window, config, mode) elif ch == ord("e"): key_a, key_b, _, _, _, _ = row c = enter_var_name( window, key_a, key_b) if c is not None: row[5] = CustomizeA(c) refresh_content( window, config, mode) elif ch == ord("l"): edit_left(window, config, mode) elif ch == ord("v"): filename = pick_file(window, ".") if filename is not None: b_file = mode.b_cache_file.fil a_file = mode.a_cache_file.fil mappings = extract_mappings( a_file, filename) key_a, _, _, _, _, _ = row a_file.update_value( name, key_a, mappings) refresh_bottom_panes(window, config, mode, force=True) else: # focused mode if row is not None: if ch == ord("f"): keys = mode.cache_file.fil.get_keys(name) c = choose_candidate(window, keys) if c is not None: row[0] = c refresh_content(window, config, mode) elif ch == ord("s"): row[1] = Noop() refresh_content(window, config, mode) else: if mode.cache_file.update is not None: if ch == ord("e"): key, _ = row c = enter_var_name1(window, key) if c is not None: row[1] = CustomizeA(c) refresh_content(window, config, mode) elif ch == ord("l"): edit_bottom(window, config, mode) elif ch == ord("u"): fil = mode.cache_file.fil update = mode.cache_file.update for name, ( table, _) in mode.cache_tables.tables.items(): window.set_footer( toColoredText( NORMAL_COLOR, f"updating table {name} ...")) for row in table: key, action = row action.update(name, key, fil, None, None) row[1] = Noop() window.set_footer( toColoredText( NORMAL_COLOR, f"writing to file {update} ...")) fil.dump(update) if mode.a_focused: config.a_updated = True else: config.b_updated = True reload_files(window, config, mode) elif ch == ord("v"): filename = pick_file(window, ".") if filename is not None: mappings = extract_mappings( mode.cache_file.fil, filename) key, _ = row mode.cache_file.fil.update_value( name, key, mappings) refresh_bottom_panes(window, config, mode, force=True) while True: try: ch = window.getch() if ch == curses.KEY_RESIZE: handle_window_resize(window) elif window.focus == "top_pane": if ch in command_keys( mode) and ch != ord("q") and ch != ord("m"): handle_command(ch) elif ch == ord("m"): ch2 = menu(window, mode) if ch2 == ord("q"): break handle_command(ch2) refresh_footer(window, config, mode) elif ch == ord("q"): break else: window._onKey(ch) else: if key_escape(ch): name = config.table_names[mode.cache_tables.current_table] row = get_current_row(window.children["top_pane"], config, mode) if window.focus == "left_pane": if row is not None: key_a, _, _, _, _, _ = row a_file = mode.a_cache_file.fil a_file.update_value( name, key_a, a_file.yaml.load(window.children["left_pane"]. lines.toString())) elif window.focus == "right_pane": if row is not None: _, key_b, _, _, _, _ = row b_file = mode.b_cache_file.fil b_file.update_value( name, key_b, b_file.yaml.load(window.children["right_pane"]. lines.toString())) elif window.focus == "bottom_pane": if row is not None: key, _ = row fil = mode.cache_file.fil fil.update_value( name, key, fil.yaml.load(window.children["bottom_pane"]. lines.toString())) setFocusByName(window, "top_pane") refresh_footer(window, config, mode) else: window._onKey(ch) window.update() except Exception as e: logger.error(traceback.format_exc()) window.set_footer(toColoredText(ERROR_COLOR, str(e)))
def refresh_content(window, config, mode): nav = toColoredText(NORMAL_COLOR, f"{APPLICATION_TITLE} ") for i, n in enumerate(config.table_names): if i > 0: nav += toColoredText(NORMAL_COLOR, " ") if i == mode.cache_tables.current_table: nav += toColoredText(HIGHLIGHT_COLOR, n) else: nav += toColoredText(NORMAL_COLOR, n) window._set_header(nav) name = config.table_names[mode.cache_tables.current_table] table, ellipsis = mode.cache_tables.tables[name] table_copy = list(table) if ellipsis: if isinstance(mode, DiffMode): table_copy.append(["...", None, None, "", Noop()]) else: table_copy.append(["...", Noop()]) if isinstance(mode, DiffMode): column_names = [ toColoredText(NORMAL_COLOR, mode.a_cache_file.filename), toColoredText(NORMAL_COLOR, mode.b_cache_file.filename), toColoredText(NORMAL_COLOR, "ratio"), toColoredText(NORMAL_COLOR, "a"), toColoredText(NORMAL_COLOR, "b"), toColoredText(NORMAL_COLOR, "update") ] else: column_names = [ toColoredText(NORMAL_COLOR, mode.cache_file.filename), toColoredText(NORMAL_COLOR, "update") ] rows = list(map(partial(to_prettytable, mode), table_copy)) header, content, footer = format_table(NORMAL_COLOR, column_names, rows) content += toColoredText(NORMAL_COLOR, "\n") content += footer top_pane = window.children["top_pane"] header_pane = window.children["header_pane"] top_pane.bottom_padding = footer.numberOfLines() header_pane._replace(header) top_pane._replace(content) window.update() refresh_bottom_panes(window, config, mode)
def create_window(popwindow): poph, popw = popwindow.size() text = popwindow.text("text_pane", poph - 1, popw - 1, 1, 1, toColoredText(NORMAL_COLOR, text_content))
def format_row(row): return [toColoredText(NORMAL_COLOR, header) for header in row]
def reload_files(window, config, mode): if isinstance(mode, DiffMode): a_filename = mode.a_cache_file.update if config.a_updated else mode.a_cache_file.filename b_filename = mode.b_cache_file.update if config.b_updated else mode.b_cache_file.filename a_type = mode.a_cache_file.typ b_type = mode.b_cache_file.typ window.set_header(toColoredText(NORMAL_COLOR, APPLICATION_TITLE)) window.set_footer( toColoredText(NORMAL_COLOR, f"loading {a_filename} ...")) try: a_file = make_file(a_type, a_filename) except Exception as e: logger.error(f"error loading {a_filename}\n") raise window.set_footer( toColoredText(NORMAL_COLOR, f"loading {b_filename} ...")) try: b_file = make_file(b_type, b_filename) except Exception as e: logger.error(f"error loading {b_filename}\n") raise window.set_footer(toColoredText(NORMAL_COLOR, f"comparing...")) tables = {} for table in config.table_names: a_var_names = a_file.get_keys(table) b_var_names = b_file.get_keys(table) tables[table] = truncate_set(a_var_names, b_var_names, config.a_only, config.b_only, config.similarity_threshold, config.max_entries, config.ignore_suffix) mode.update_files(config, a_file, b_file, tables) else: filename = mode.cache_file.update if ( config.a_updated and mode.a_focused) or ( config.b_updated and not mode.a_focused) else mode.cache_file.filename typ = mode.cache_file.typ window.set_header(toColoredText(NORMAL_COLOR, APPLICATION_TITLE)) window.set_footer( toColoredText(NORMAL_COLOR, f"loading {filename} ...")) try: fil = make_file(typ, filename) except Exception as e: logger.error(f"error loading {filename}\n") raise mode.update_file( config, fil, { name: ([[key, Noop()] for key in fil.get_keys(name)], False) for name in config.table_names }) refresh(window, config, mode)
def to_prettytable(mode, l): if isinstance(mode, DiffMode): if l[0] is None: if l[1] is None: return [ toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, l[3]), toColoredText(NORMAL_COLOR, l[4]), toColoredText(NORMAL_COLOR, str(l[5])) ] else: return [ toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, l[1]), toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, l[3]), toColoredText(NORMAL_COLOR, l[4]), toColoredText(NORMAL_COLOR, str(l[5])) ] elif l[1] is None: return [ toColoredText(NORMAL_COLOR, l[0]), toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, l[3]), toColoredText(NORMAL_COLOR, l[4]), toColoredText(NORMAL_COLOR, str(l[5])) ] else: return list(colorize_diff(l[0], l[1])) + [ toColoredText(NORMAL_COLOR, f"{l[2]:.2f}"), toColoredText(NORMAL_COLOR, l[3]), toColoredText(NORMAL_COLOR, l[4]), toColoredText(NORMAL_COLOR, str(l[5])) ] else: if l[0] is None: return [ toColoredText(NORMAL_COLOR, ""), toColoredText(NORMAL_COLOR, str(l[1])) ] else: return [ toColoredText(NORMAL_COLOR, l[0]), toColoredText(NORMAL_COLOR, str(l[1])) ]
def choose_candidate(window, candidates): help_text_short = "UP DOWN PAGE UP PAGE DOWN navigate ENTER confirm ESCAPE exit" candidates_w = max(len("candidate"), max(len(candidate) for candidate in candidates)) + 2 column_ws = [candidates_w] table_w = reduce(lambda x, y: x + y + 1, column_ws, 0) headers = ["candidate"] def f(candidates): return format_table(NORMAL_COLOR, format_row(headers), list( map(format_row, map(lambda r: [r], candidates))), upper_border=False, left_border=False, right_border=False) header, content, footer = f(candidates) content += toColoredText(NORMAL_COLOR, "\n") content += footer _, w = window.size() popw = min(len(header.lines()[0]) + 2, w * 4 // 5) popwindow = None candidates_filtered = None def match(c, candidate): return c.lower() in candidate.lower() def update_content(source, oc, c): nonlocal candidates_filtered popcontent_pane = popwindow.children["content_pane"] candidates_filtered = [ candidate for candidate in candidates if match(c, candidate) ] header, content, footer = f(candidates_filtered) content += toColoredText(NORMAL_COLOR, "\n") content += footer popcontent_pane._replace(content) def create_window(window): nonlocal popwindow popwindow = window popwindow.window.keypad(1) poph, popw = popwindow.size() popheader_pane = popwindow.pane("header_pane", 3, popw - 2, 1, 1, False) popcontent_pane = popwindow.pane("content_pane", poph - 5, popw - 2, 3, 1, True) popcontent_pane.bottom_padding = 1 popsearch_textfield = popwindow.textfield("search_textfield", popw - 2, poph - 2, 1, "search:", "") popsearch_textfield.addChangeHandler(update_content) popwindow.focus = "search_textfield" popheader_pane._replace(header) popcontent_pane._replace(content) update_content(None, None, "") def candidates_get_current_row_id(popcontent_pane): return max(0, popcontent_pane.current_document_y) def key_handler(popwindow, ch): popcontent_pane = popwindow.children["content_pane"] search_textfield = popwindow.children["search_textfield"] if key_enter(ch): i = candidates_get_current_row_id(popcontent_pane) c = candidates_filtered[i] return WindowExit(c) elif key_escape(ch): return WindowExit(None) c = window.popup(toColoredText(NORMAL_COLOR, help_text_short), create_window, key_handler, w=popw) return c