def main(stdscr): set_screen_defaults(stdscr) # Add initial tab tab.add_new_tab() globvar.redraw = True key = '' while key != ord('q') and globvar.quitting is False: if globvar.redraw: draw_screen(stdscr) globvar.redraw = False key = stdscr.getch() if key == curses.ERR: continue if key == ord('a'): tab.add_new_tab() if key == ord('d'): tab.delete_tab() if key == ord('f'): popup.set_grep_word(stdscr) if key == ord('h'): help.show_help(stdscr) if key == ord('s'): # Save profile profile.save_profile(stdscr) if key == ord('l'): # Load profile profile.load_profile(stdscr) if key == ord('*'): # highlight another word popup.highlight(stdscr) if key == ord('?'): # highlight another word goto.goto_backward(stdscr) if key == ord('p'): tab.get_current_tab().toggle_pause() if key == curses.KEY_LEFT: move.move_left() if key == curses.KEY_RIGHT: move.move_right() if key == curses.KEY_UP: set_scroll(1) if key == curses.KEY_DOWN: set_scroll(-1) if key == curses.KEY_PPAGE: set_scroll(10) if key == curses.KEY_NPAGE: set_scroll(-10) if key == curses.KEY_HOME: # size of the window goto.scroll_to_top() KEY_ENTER = 10 if key == KEY_ENTER or key == curses.KEY_END: goto.scroll_to_bottom() globvar.quitting = True
def set_grep_word(stdscr): word = get_input(stdscr, "Grep for: ") tab.get_current_tab().grep = word tab.get_current_tab().name = word # Clear screen is required since the word size (in the tab) could have changed globvar.clear_screen = True globvar.redraw = True
def print_body(): """Print body text""" win = curses.newwin(curses.LINES - 3, curses.COLS, 3, 0) win.scrollok(True) if tab.get_idx() < 0 or tab.get_idx() > tab.get_amount_tabs() - 1: raise Exception( f"Something goes wrong with tabs {tab.get_idx()} - {tab.get_idx()} | {tab.get_amount_tabs()}" ) current = tab.get_current_tab() # Word to highlight light word = current.grep # any additional word to highlight array_and_colors = [(word, colors.COLOR_ADDITIONAL_WORD_FOUND) for word in current.highlight] # Main word should come first if word: array_and_colors.insert(0, (word, colors.COLOR_WORD_FOUND)) pb = PrintBuffer(array_and_colors) scroll = tab.get_current_tab().scroll # Grep from bottoms up (Performance) for line in reversed(globvar.pristine.get_main()): if word in line: pb.add(line) if pb.is_full(scroll): # Do not import more. Not going to display anyway break pb.print_all(win, scroll)
def print_tab(stdscr, title, active): """Print a new tab in the header""" uly = 0 ulx = globvar.cur_pos lry = hsize lrx = globvar.cur_pos + wsize if lrx >= curses.COLS: # No enough space in the x row return rectangle(stdscr, uly, ulx, lry, lrx) if active: color = curses.color_pair(COLOR_SENSITIVE_SELECTED) else: color = curses.color_pair(COLOR_SENSITIVE_NOT_SELECTED) stdscr.addstr(uly + 1, globvar.cur_pos + 1, f"{title} ", color) globvar.cur_pos += wsize + 1 current = tab.get_current_tab() if current.paused: stdscr.addstr(uly + 1, curses.COLS - 10, f"PAUSED", curses.color_pair(COLOR_STOPPED))
def set_scroll(param): current = tab.get_current_tab() length_min = curses.LINES - 4 current.scroll += param if current.scroll < 0: current.scroll = 0 globvar.redraw = True
def move_left(): globvar.clear_screen = True globvar.redraw = True # is the current tab the last one if tab.is_last_tab() and tab.get_current_tab().name == "Unfiltered": tab.delete_tab() return # Just go left tab.decrease_idx()
def get_scrolled_text(self, text, scroll): if not scroll: return text length = len(text) length_min = curses.LINES - 4 # length - lenth_min = first page of the text if scroll == tab.SCROLL_TO_HOME: scroll = length - length_min to_be_displayed = length - scroll if to_be_displayed <= length_min: current = tab.get_current_tab() current.scroll = length - length_min return text[0:length_min] # Crop the last `scroll` lines return text[0:length - scroll]
def highlight(stdscr): globvar.redraw = True word = get_input(stdscr, "Highlight word: ") if not word: return tab.get_current_tab().highlight.append(word)
def goto_backward(stdscr): globvar.redraw = True word = get_input(stdscr, "Go to: ") if not word: return tab.get_current_tab().goto = (word, BACK)
def scroll_to_bottom(): """ Go to the last line""" current = tab.get_current_tab() current.scroll = -1 globvar.redraw = True
def scroll_to_top(): """ Go to the line 0""" current = tab.get_current_tab() current.scroll = tab.SCROLL_TO_HOME globvar.redraw = True