def pager(text, color=None): """Decide what method to use for paging through text.""" stdout = _default_text_stdout() if not isatty(sys.stdin) or not isatty(stdout): return _nullpager(stdout, text, color) pager_cmd = (os.environ.get('PAGER', None) or '').strip() if pager_cmd: if WIN: return _tempfilepager(text, pager_cmd, color) return _pipepager(text, pager_cmd, color) if os.environ.get('TERM') in ('dumb', 'emacs'): return _nullpager(stdout, text, color) if WIN or sys.platform.startswith('os2'): return _tempfilepager(text, 'more <', color) if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0: return _pipepager(text, 'less', color) import tempfile fd, filename = tempfile.mkstemp() os.close(fd) try: if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0: return _pipepager(text, 'more', color) return _nullpager(stdout, text, color) finally: os.unlink(filename)
def display_standings(standings, last, contests, all_, global_, recalculate): table = FancyTable() table.add_column(StaticColumn('Place', 6, lambda row: row.place)) table.add_column(StaticColumn.padding(1)) table.add_column(StaticColumn('User', 34, lambda row: row.user, right_just=False)) table.add_column(StaticColumn('Solved', 6, lambda row: row.solved)) table.add_column(StaticColumn('Score', 6, lambda row: row.score)) if global_: table.add_column(StaticColumn('Group', 5, lambda row: get_group_id(row.contest_id))) terminal_width = get_terminal_width() if isatty(sys.stdout): contests_width = terminal_width - table.calc_width() - 1 default_contest_count = get_default_contest_count( standings.contests, standings.tasks_by_contest, contests_width ) else: default_contest_count = len(standings.contests) contests = select_contests(standings, last, contests, all_, default_contest_count) if contests is None: return if recalculate: recalculate_score(standings, contests) table.add_column(TasksColumn(contests, standings.tasks_by_contest)) table.show(standings.rows, allow_high_tables=not global_)
def __init__(self, stream_name, file, color): # print(f'@ Redirecting {stream_name}') # print(f'> Redirecting {stream_name}', file=getattr(sys, stream_name)) self.stream_name = stream_name self.stream = getattr(sys, stream_name) self.file = file.open('a') self.stream_color = color or isatty(self.stream) self.file_color = color setattr(sys, stream_name, self)
def show(self, rows, allow_high_tables=False): terminal_width, terminal_height = shutil.get_terminal_size() exceeds_width = self.calc_width() > terminal_width exceeds_height = len(rows) > terminal_height lines = self.render(rows) output = '\n'.join(lines) if isatty(sys.stdout) and (exceeds_width or exceeds_height and not allow_high_tables): if 'LESS' not in os.environ: os.environ['LESS'] = '-S -R' click.echo_via_pager(output) else: click.secho(output)
def raw_mode(): """ Enables terminal raw mode during the context. Note: Currently noop for Windows systems. Usage: :: with raw_mode(): do_some_stuff() """ if WIN: # No implementation for windows yet. yield # needed for the empty context manager to work else: # imports are placed here because this will fail under Windows import tty import termios if not isatty(sys.stdin): f = open("/dev/tty") fd = f.fileno() else: fd = sys.stdin.fileno() f = None try: old_settings = termios.tcgetattr(fd) tty.setraw(fd) except termios.error: pass try: yield finally: # this block sets the terminal to sane mode again, # also in case an exception occured in the context manager try: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) # sys.stdout.flush() # not needed I think. if f is not None: f.close() except termios.error: pass