def _render(self, highlight_color: VtColors, nav_color: VtColors) -> None: """TODO""" length = len(self.items) dummy, columns = screen_size() restore_cursor() for idx in range(self.show_from, self.show_to): selector = self.UNSELECTED if idx < length: # When the number of items is lower than the max_rows, skip the other lines option_line = str(self.items[idx])[0:int(columns)] vt_print('%EL2%\r') # Erase current line before repaint # Print the selector if the index is currently selected if idx == self.sel_index: vt_print(highlight_color.code()) selector = self.SELECTED fmt = " {:>" + str(len( str(length))) + "}{:>" + str(1 + len(str(selector))) + "} {}" sysout(fmt.format(idx + 1, selector, option_line)) # Check if the text fits the screen and print it, otherwise print '...' if len(option_line) >= int(columns): sysout("%CUB(4)%%EL0%...%NC%", end='') else: break sysout(self.NAV_FMT.format(nav_color.placeholder(), self.NAV_ICONS, str(length)), end='') self.re_render = False
def syserr(string: str, end: str = '\n') -> None: """Print the unicode input_string decoding vt100 placeholders :param string: values to be printed to sys.stderr :param end: string appended after the last value, default a newline """ if Validator.is_not_blank(string): msg = VtColors.colorize(VtCodes.decode(f"%RED%{string}%NC%")) print(msg, file=sys.stderr, flush=True, end=end)
def prompt(prompt_msg: str = '', validator: Callable = None, default_value: Any = None, any_key: bool = False, on_blank_abort: bool = True, color: VtColors = VtColors.GREEN, end: str = ': ') -> Optional[Any]: """TODO""" valid = False input_data = None while not valid: try: colorized = VtColors.colorize('{}{}{}{}{}'.format( color.placeholder(), prompt_msg, '[{}]'.format(default_value) if default_value else '', end, VtColors.NC.placeholder())) input_data = input(colorized) if not validator: valid = True elif default_value: return default_value elif any_key: return None elif Validator.is_not_blank(input_data): valid, msg = validator(input_data) if not valid: MenuUtils.print_error("{}: ".format(msg), input_data) else: if not on_blank_abort: MenuUtils.print_error("Input can't be empty: ", input_data) else: raise InputAbortedError('Input process was aborted') except EOFError as err: MenuUtils.print_error("Input failed: ", str(err)) break return input_data
def __init__(self, parent: Menu, items: dict, title: str = None, color: VtColors = VtColors.ORANGE): super().__init__(parent, title) title_len = round(len(title) / 2) + 2 self.menu_data = MENU_TPL.format( '{}{}\n {}\n{}'.format(color.placeholder(), '-=' * title_len, self.title, '-=' * title_len), '\n'.join([str(value) for key, value in items.items()])) self.options = range(0, len(items)) self.items = items
def _render(self, nav_color: VtColors) -> None: """TODO""" restore_cursor() set_enable_echo() for idx, item in enumerate(self.items): self._print_cell( idx, item, MenuDashBoard.CELL_TPL if self.tab_index != idx else MenuDashBoard.SEL_CELL_TPL) sysout(f'%EL2%\r> %GREEN%{self.items[self.tab_index].tooltip}%NC%') sysout(self.NAV_FMT.format(nav_color.placeholder(), self.NAV_ICONS), end='') self.re_render = False