def _N_lines(self): ''' Determine how many lines to print, such that the number of items displayed will fit on the terminal (i.e one 'screen-ful' of items) This looks at the environmental prompt variable, and tries to determine how many lines it takes up. On Windows, it does this by looking for the '$_' sequence, which indicates a new line, in the environmental variable PROMPT. Otherwise, it looks for a newline ('\n') in the environmental variable PS1. ''' lines_in_prompt = 1 # prompt is assumed to take up one line, even # without any newlines in it if "win32" in sys.platform: lines_in_prompt += 1 # Windows will typically print a free line after # the program output a = re.findall('\$_', os.getenv('PROMPT', '')) lines_in_prompt += len(a) else: a = re.findall('\\n', os.getenv('PS1', '')) lines_in_prompt += len(a) n_lines = get_terminal_size().lines - lines_in_prompt # print a minimum of one item n_lines = max(n_lines, 1) return n_lines
def _N_lines(): ''' Determine how many lines to print, such that the number of items displayed will fit on the terminal (i.e one 'screen-ful' of items) This looks at the environmental prompt variable, and tries to determine how many lines it takes up. On Windows, it does this by looking for the '$_' sequence, which indicates a new line, in the environmental variable PROMPT. Otherwise, it looks for a newline ('\n') in the environmental variable PS1. ''' lines_in_prompt = 1 # prompt is assumed to take up one line, even # without any newlines in it if "win32" in sys.platform: lines_in_prompt += 1 # Windows will typically print a free line after # the program output a = re.findall(r'\$_', os.getenv('PROMPT', '')) lines_in_prompt += len(a) else: a = re.findall('\\n', os.getenv('PS1', '')) lines_in_prompt += len(a) n_lines = get_terminal_size().lines - lines_in_prompt # print a minimum of one item n_lines = max(n_lines, 1) return n_lines
def _process_flags(self): opts, args = self.getopt('f:F:i:n:Ns:x') for opt, value in opts: if opt == '-x': self.show_all = True elif opt == '-s': self.sort_expression = value elif opt == '-f': if value == 'json': from topydo.lib.JsonPrinter import JsonPrinter self.printer = JsonPrinter() elif value == 'ical': if self._poke_icalendar(): from topydo.lib.IcalPrinter import IcalPrinter self.printer = IcalPrinter(self.todolist) else: self.printer = None elif opt == '-F': self.format = value elif opt == '-N': # 2 lines are assumed to be taken up by printing the next prompt # display at least one item self.limit = max(get_terminal_size().lines - 2, 1) elif opt == '-n': try: self.limit = int(value) except ValueError: pass # use default value in configuration elif opt == '-i': self.ids = value.split(',') # when a user requests a specific ID, it should always be shown self.show_all = True self.args = args
def _columns(): """ Returns the number of columns of the terminal. """ return get_terminal_size().columns
def __init__(self): super().__init__() self._process_flags() self.column_width = config().column_width() self.todofile = TodoFile.TodoFile(config().todotxt()) self.todolist = TodoList.TodoList(self.todofile.read()) self.marked_todos = [] self.columns = urwid.Columns([], dividechars=0, min_width=config().column_width()) self.commandline = CommandLineWidget('topydo> ') self.keystate_widget = KeystateWidget() self.status_line = urwid.Columns([ ('weight', 1, urwid.Filler(self.commandline)), ]) self.keymap = config().column_keymap() self._alarm = None self._last_cmd = None # console widget self.console = ConsoleWidget() get_terminal_size(self._console_width) urwid.connect_signal(self.commandline, 'blur', self._blur_commandline) urwid.connect_signal(self.commandline, 'execute_command', self._execute_handler) def hide_console(p_focus_commandline=False): self._console_visible = False if p_focus_commandline: self._focus_commandline() urwid.connect_signal(self.console, 'close', hide_console) # view widget self.viewwidget = ViewWidget(self.todolist) urwid.connect_signal(self.viewwidget, 'save', lambda: self._update_view(self.viewwidget.data)) def hide_viewwidget(): self._viewwidget_visible = False self._blur_commandline() urwid.connect_signal(self.viewwidget, 'close', hide_viewwidget) self.mainwindow = MainPile([ ('weight', 1, self.columns), (1, self.status_line), ]) urwid.connect_signal(self.mainwindow, 'blur_console', hide_console) # the columns should have keyboard focus self._blur_commandline() self._screen = urwid.raw_display.Screen() if config().colors(): self._screen.register_palette(self._create_color_palette()) else: self._screen.register_palette(self._create_mono_palette()) self._screen.set_terminal_properties(256) self.mainloop = urwid.MainLoop( self.mainwindow, screen=self._screen, unhandled_input=self._handle_input, pop_ups=True ) self.column_mode = _APPEND_COLUMN self._set_alarm_for_next_midnight_update()
def __init__(self): super().__init__() args = self._process_flags() try: opts, args = getopt.getopt(args[1:], 'l:') except getopt.GetoptError as e: error(str(e)) sys.exit(1) self.alt_layout_path = None for opt, value in opts: if opt == "-l": self.alt_layout_path = value def callback(): self.todolist.erase() self.todolist.add_list(self.todofile.read()) self._update_all_columns() self._redraw() self.column_width = config().column_width() self.todofile = TodoFileWatched(config().todotxt(), callback) self.todolist = TodoList.TodoList(self.todofile.read()) self.marked_todos = set() self.columns = urwid.Columns([], dividechars=0, min_width=config().column_width()) self.columns.contents.set_focus_changed_callback(self._move_highlight) completer = ColumnCompleter(self.todolist) self.commandline = CommandLineWidget(completer, 'topydo> ') self.keystate_widget = KeystateWidget() self.status_line = urwid.Columns([ ('weight', 1, urwid.Filler(self.commandline)), ]) self.cli_wrapper = CliWrapper([(1, self.status_line)]) self.keymap = config().column_keymap() self._alarm = None self._last_cmd = None # console widget self.console = ConsoleWidget() get_terminal_size(self._console_width) urwid.connect_signal(self.commandline, 'blur', self._blur_commandline) urwid.connect_signal(self.commandline, 'execute_command', self._execute_handler) urwid.connect_signal(self.commandline, 'show_completions', self._show_completion_box) urwid.connect_signal(self.commandline, 'hide_completions', self._hide_completion_box) def hide_console(p_focus_commandline=False): if p_focus_commandline: self._focus_commandline() else: self._console_visible = False urwid.connect_signal(self.console, 'close', hide_console) # view widget self.viewwidget = ViewWidget(self.todolist) urwid.connect_signal(self.viewwidget, 'save', lambda: self._update_view(self.viewwidget.data)) def hide_viewwidget(): # prevent the view widget to be hidden when the last column was # deleted if self.columns.contents: self._viewwidget_visible = False self._blur_commandline() urwid.connect_signal(self.viewwidget, 'close', hide_viewwidget) self.mainwindow = MainPile([ ('weight', 1, self.columns), ('pack', self.cli_wrapper), ]) urwid.connect_signal(self.mainwindow, 'blur_console', hide_console) # the columns should have keyboard focus self._blur_commandline() self._screen = urwid.raw_display.Screen() def create_color_palette(): project_color = to_urwid_color(config().project_color()) context_color = to_urwid_color(config().context_color()) metadata_color = to_urwid_color(config().metadata_color()) link_color = to_urwid_color(config().link_color()) focus_background_color = to_urwid_color( config().focus_background_color()) marked_background_color = to_urwid_color( config().marked_background_color()) palette = [ (PaletteItem.PROJECT, '', '', '', project_color, ''), (PaletteItem.PROJECT_FOCUS, '', 'light gray', '', project_color, focus_background_color), (PaletteItem.CONTEXT, '', '', '', context_color, ''), (PaletteItem.CONTEXT_FOCUS, '', 'light gray', '', context_color, focus_background_color), (PaletteItem.METADATA, '', '', '', metadata_color, ''), (PaletteItem.METADATA_FOCUS, '', 'light gray', '', metadata_color, focus_background_color), (PaletteItem.LINK, '', '', '', link_color, ''), (PaletteItem.LINK_FOCUS, '', 'light gray', '', link_color, focus_background_color), (PaletteItem.DEFAULT_FOCUS, '', 'light gray', '', '', focus_background_color), (PaletteItem.MARKED, '', 'light blue', '', '', marked_background_color), ] for C in ascii_uppercase: pri_color_cfg = config().priority_color(C) pri_color = to_urwid_color(pri_color_cfg) pri_color_focus = pri_color if not pri_color_cfg.is_neutral( ) else 'black' palette.append(('pri_' + C, '', '', '', pri_color, '')) palette.append(('pri_' + C + '_focus', '', 'light gray', '', pri_color_focus, focus_background_color)) return palette def create_mono_palette(): palette = [ (PaletteItem.DEFAULT_FOCUS, 'black', 'light gray'), (PaletteItem.PROJECT_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.CONTEXT_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.METADATA_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.LINK_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.MARKED, 'default,underline,bold', 'default'), ] for C in ascii_uppercase: palette.append( ('pri_' + C + '_focus', PaletteItem.DEFAULT_FOCUS)) return palette if config().colors(): self._screen.register_palette(create_color_palette()) else: self._screen.register_palette(create_mono_palette()) self._screen.set_terminal_properties(256) self.mainloop = urwid.MainLoop(self.mainwindow, screen=self._screen, unhandled_input=self._handle_input, pop_ups=True) self.column_mode = _APPEND_COLUMN self._set_alarm_for_next_midnight_update()
def _columns(): """ Returns the number of columns of the terminal. """ return get_terminal_size().columns
def __init__(self): super().__init__() args = self._process_flags() try: opts, args = getopt.getopt(args[1:], 'l:') except getopt.GetoptError as e: error(str(e)) sys.exit(1) self.alt_layout_path = None for opt, value in opts: if opt == "-l": self.alt_layout_path = value def callback(): self.todolist.erase() self.todolist.add_list(self.todofile.read()) self._update_all_columns() self._redraw() self.column_width = config().column_width() self.todofile = TodoFileWatched(config().todotxt(), callback) self.todolist = TodoList.TodoList(self.todofile.read()) self.marked_todos = set() self.columns = urwid.Columns([], dividechars=0, min_width=config().column_width()) completer = ColumnCompleter(self.todolist) self.commandline = CommandLineWidget(completer, 'topydo> ') self.keystate_widget = KeystateWidget() self.status_line = urwid.Columns([ ('weight', 1, urwid.Filler(self.commandline)), ]) self.cli_wrapper = CliWrapper([(1, self.status_line)]) self.keymap = config().column_keymap() self._alarm = None self._last_cmd = None # console widget self.console = ConsoleWidget() get_terminal_size(self._console_width) urwid.connect_signal(self.commandline, 'blur', self._blur_commandline) urwid.connect_signal(self.commandline, 'execute_command', self._execute_handler) urwid.connect_signal(self.commandline, 'show_completions', self._show_completion_box) urwid.connect_signal(self.commandline, 'hide_completions', self._hide_completion_box) def hide_console(p_focus_commandline=False): if p_focus_commandline: self._focus_commandline() else: self._console_visible = False urwid.connect_signal(self.console, 'close', hide_console) # view widget self.viewwidget = ViewWidget(self.todolist) urwid.connect_signal(self.viewwidget, 'save', lambda: self._update_view(self.viewwidget.data)) def hide_viewwidget(): # prevent the view widget to be hidden when the last column was # deleted if self.columns.contents: self._viewwidget_visible = False self._blur_commandline() urwid.connect_signal(self.viewwidget, 'close', hide_viewwidget) self.mainwindow = MainPile([ ('weight', 1, self.columns), ('pack', self.cli_wrapper), ]) urwid.connect_signal(self.mainwindow, 'blur_console', hide_console) # the columns should have keyboard focus self._blur_commandline() self._screen = urwid.raw_display.Screen() if config().colors(): self._screen.register_palette(self._create_color_palette()) else: self._screen.register_palette(self._create_mono_palette()) self._screen.set_terminal_properties(256) self.mainloop = urwid.MainLoop(self.mainwindow, screen=self._screen, unhandled_input=self._handle_input, pop_ups=True) self.column_mode = _APPEND_COLUMN self._set_alarm_for_next_midnight_update()
def __init__(self): super().__init__() args = self._process_flags() try: opts, args = getopt.getopt(args[1:], 'l:') except getopt.GetoptError as e: error(str(e)) sys.exit(1) self.alt_layout_path = None for opt, value in opts: if opt == "-l": self.alt_layout_path = value def callback(): self.todolist.erase() self.todolist.add_list(self.todofile.read()) self._update_all_columns() self._redraw() self.column_width = config().column_width() self.todofile = TodoFileWatched(config().todotxt(), callback) self.todolist = TodoList.TodoList(self.todofile.read()) self.marked_todos = set() self.columns = urwid.Columns([], dividechars=0, min_width=config().column_width()) completer = ColumnCompleter(self.todolist) self.commandline = CommandLineWidget(completer, 'topydo> ') self.keystate_widget = KeystateWidget() self.status_line = urwid.Columns([ ('weight', 1, urwid.Filler(self.commandline)), ]) self.cli_wrapper = CliWrapper([(1, self.status_line)]) self.keymap = config().column_keymap() self._alarm = None self._last_cmd = None # console widget self.console = ConsoleWidget() get_terminal_size(self._console_width) urwid.connect_signal(self.commandline, 'blur', self._blur_commandline) urwid.connect_signal(self.commandline, 'execute_command', self._execute_handler) urwid.connect_signal(self.commandline, 'show_completions', self._show_completion_box) urwid.connect_signal(self.commandline, 'hide_completions', self._hide_completion_box) def hide_console(p_focus_commandline=False): if p_focus_commandline: self._focus_commandline() else: self._console_visible = False urwid.connect_signal(self.console, 'close', hide_console) # view widget self.viewwidget = ViewWidget(self.todolist) urwid.connect_signal(self.viewwidget, 'save', lambda: self._update_view(self.viewwidget.data)) def hide_viewwidget(): # prevent the view widget to be hidden when the last column was # deleted if self.columns.contents: self._viewwidget_visible = False self._blur_commandline() urwid.connect_signal(self.viewwidget, 'close', hide_viewwidget) self.mainwindow = MainPile([ ('weight', 1, self.columns), ('pack', self.cli_wrapper), ]) urwid.connect_signal(self.mainwindow, 'blur_console', hide_console) # the columns should have keyboard focus self._blur_commandline() self._screen = urwid.raw_display.Screen() def create_color_palette(): project_color = to_urwid_color(config().project_color()) context_color = to_urwid_color(config().context_color()) metadata_color = to_urwid_color(config().metadata_color()) link_color = to_urwid_color(config().link_color()) focus_background_color = to_urwid_color(config().focus_background_color()) marked_background_color = to_urwid_color(config().marked_background_color()) palette = [ (PaletteItem.PROJECT, '', '', '', project_color, ''), (PaletteItem.PROJECT_FOCUS, '', 'light gray', '', project_color, focus_background_color), (PaletteItem.CONTEXT, '', '', '', context_color, ''), (PaletteItem.CONTEXT_FOCUS, '', 'light gray', '', context_color, focus_background_color), (PaletteItem.METADATA, '', '', '', metadata_color, ''), (PaletteItem.METADATA_FOCUS, '', 'light gray', '', metadata_color, focus_background_color), (PaletteItem.LINK, '', '', '', link_color, ''), (PaletteItem.LINK_FOCUS, '', 'light gray', '', link_color, focus_background_color), (PaletteItem.DEFAULT_FOCUS, '', 'light gray', '', '', focus_background_color), (PaletteItem.MARKED, '', 'light blue', '', '', marked_background_color), ] for C in ascii_uppercase: pri_color_cfg = config().priority_color(C) pri_color = to_urwid_color(pri_color_cfg) pri_color_focus = pri_color if not pri_color_cfg.is_neutral() else 'black' palette.append(( 'pri_' + C, '', '', '', pri_color, '' )) palette.append(( 'pri_' + C + '_focus', '', 'light gray', '', pri_color_focus, focus_background_color )) return palette def create_mono_palette(): palette = [ (PaletteItem.DEFAULT_FOCUS, 'black', 'light gray'), (PaletteItem.PROJECT_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.CONTEXT_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.METADATA_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.LINK_FOCUS, PaletteItem.DEFAULT_FOCUS), (PaletteItem.MARKED, 'default,underline,bold', 'default'), ] for C in ascii_uppercase: palette.append( ('pri_' + C + '_focus', PaletteItem.DEFAULT_FOCUS) ) return palette if config().colors(): self._screen.register_palette(create_color_palette()) else: self._screen.register_palette(create_mono_palette()) self._screen.set_terminal_properties(256) self.mainloop = urwid.MainLoop( self.mainwindow, screen=self._screen, unhandled_input=self._handle_input, pop_ups=True ) self.column_mode = _APPEND_COLUMN self._set_alarm_for_next_midnight_update()