我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用shutil.get_terminal_size()。
def get_terminal_size(): def ioctl_GWINSZ(fd): try: import fcntl import termios import struct cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) except: return None return cr cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) if not cr: try: fd = os.open(os.ctermid(), os.O_RDONLY) cr = ioctl_GWINSZ(fd) os.close(fd) except: pass if not cr: try: cr = (os.env['LINES'], os.env['COLUMNS']) except: cr = (25, 80) return int(cr[1]), int(cr[0])
def format_article(article, header=None, footer=None, line=False): content = '' if header: content = '{}\n'.format(header) if line: content += '{}\n'.format('=' * (get_terminal_size()[0]-1)) content += '{} - {}\nReading Time: {} Mins\nURL: {}\n'.format( article['id'], article['title'] if article['title'] else '(No Title)', article['reading_time'], article['url'] ) if footer: content += footer return content
def say(msg): """Print a message. Unlike print(), this deals with de-denting and wrapping of text to fit within the width of the terminal. Paragraphs separated by blank lines in the input will be wrapped separately. """ msg = str(msg) msg = re.sub(r'^[ \t]*(.*?)[ \t]*$', r'\1', msg, flags=re.M) width = get_terminal_size()[0] paragraphs = re.split(r'\n(?:[ \t]*\n)', msg) formatted = (textwrap.fill(p.strip(), width=width) for p in paragraphs) print('\n\n'.join(formatted))
def collect_statuses(workspace): result = list() repos = workspace.get_repos() if not repos: return result num_repos = len(repos) max_len = max((len(x.src) for x in repos)) for i, repo, full_path in workspace.enumerate_repos(): ui.info_count(i, num_repos, "Checking", repo.src.ljust(max_len + 1), end="\r") status = tsrc.git.get_status(full_path) result.append((repo.src, status)) terminal_size = shutil.get_terminal_size() ui.info(" " * terminal_size.columns, end="\r") return result
def show(args, _config, _extra_args): """Show details about an experiment.""" import pickle import pprint name = args.name with shelve.open('.em') as emdb: if name not in emdb or name == EM_KEY: return _die(E_NO_EXP.format(name)) for info_name, info_val in sorted(emdb[name].items()): if isinstance(info_val, datetime.date): info_val = info_val.ctime() print(f'{info_name}: {info_val}') if not args.opts: return opts_path = _expath(name, 'run', 'opts.pkl') with open(opts_path, 'rb') as f_opts: print('\noptions:') opts = pickle.load(f_opts) cols = shutil.get_terminal_size((80, 20)).columns pprint.pprint(vars(opts), indent=2, compact=True, width=cols)
def columnize(lines, indent=0, pad=2): term_width = shutil.get_terminal_size((80, 20)).columns # prints a list of items in a fashion similar to the dir command # borrowed from https://gist.github.com/critiqjo/2ca84db26daaeb1715e1 n_lines = len(lines) if n_lines == 0: return col_width = max(len(line) for line in lines) n_cols = int((term_width + pad - indent)/(col_width + pad)) n_cols = min(n_lines, max(1, n_cols)) col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1) if (n_cols - 1) * col_len >= n_lines: n_cols -= 1 cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)] rows = list(zip(*cols)) rows_missed = zip(*[col[len(rows):] for col in cols[:-1]]) rows.extend(rows_missed) for row in rows: yield [" "*indent + (" "*pad).join(line.ljust(col_width) for line in row)]
def __init__(self, output_type="list", initial_len=1, interval=0, force_single_line=False, no_warning=False, sort_key=lambda x:x[0]): self.sort_key = sort_key self.no_warning = no_warning no_warning and print("All reprint warning diabled.") global is_atty # reprint does not work in the IDLE terminal, and any other environment that can't get terminal_size if is_atty and not all(get_terminal_size()): if not no_warning: r = input("Fail to get terminal size, we got {}, continue anyway? (y/N)".format(get_terminal_size())) if not (r and isinstance(r, str) and r.lower()[0] in ['y','t','1']): sys.exit(0) is_atty = False if output_type == "list": self.warped_obj = output.SignalList(self, [''] * initial_len) elif output_type == "dict": self.warped_obj = output.SignalDict(self, {}) self.interval = interval self.force_single_line = force_single_line self._last_update = int(time.time()*1000)
def __exit__(self, exc_type, exc_val, exc_tb): global is_atty self.refresh(forced=True) if is_atty: columns, _ = get_terminal_size() if self.force_single_line: print('\n' * len(self.warped_obj), end="") else: print('\n' * lines_of_content(self.warped_obj, columns), end="") global last_output_lines global overflow_flag last_output_lines = 0 if overflow_flag: if not self.no_warning: print("Detected that the lines of output has been exceeded the height of terminal windows, which \ caused the former output remained and keep adding new lines.") print("????????, ??????????????, ???????????, ???????????????????")
def test_stty_match(self): """Check if stty returns the same results ignoring env This test will fail if stdin and stdout are connected to different terminals with different sizes. Nevertheless, such situations should be pretty rare. """ try: size = subprocess.check_output(['stty', 'size']).decode().split() except (FileNotFoundError, subprocess.CalledProcessError): self.skipTest("stty invocation failed") expected = (int(size[1]), int(size[0])) # reversed order with support.EnvironmentVarGuard() as env: del env['LINES'] del env['COLUMNS'] actual = shutil.get_terminal_size() self.assertEqual(expected, actual)
def update(self, pos, *, force=False): self._pos = pos if not self._activated: return if not force and time.time() - self._updated < self._update_threshold: return percentage = int(pos / self._total * 100) term_width, _ = shutil.get_terminal_size() bar_length = max(40, term_width - 1 - self._static_length) filled_length = max(int(bar_length * pos / self._total), 1) bar = '=' * (filled_length - 1) + '>' + ' ' * (bar_length - filled_length) sys.stderr.write(self._fmt.format( pos=pos, bar=bar, percentage=percentage, )) self._updated = time.time() # After calling done(), call activate() to redraw and re-activate the bar.
def run(self, _args: argparse.Namespace) -> None: text = util.get_data('howto.txt') width, _height = shutil.get_terminal_size((80, 20)) body = '\n'.join([ '\n'.join( wrap( line, width=width, break_long_words=False, replace_whitespace=False)) for line in text.splitlines() ]) print(body)
def show_help(args=[], **options): """ help Prints the synopsis and a list of the most commonly used commands. """ help_dict = set_help(cmdlookup) help_size = len(help_dict) console_width = terminal_size().columns / 1.1 console_space = (terminal_size().columns - console_width) / 2.3 index = 0 for func_name in sorted(help_dict.keys()): func_doc = help_dict[func_name] index += 1 print(func_doc) if (index < help_size): print('{:>{}}*{:-<{}}*'.format(' ', console_space, '', console_width)) # make it pretty. return ''
def write(self, data): if not data.rstrip("\n"): return f = self._file cols = get_terminal_size().columns # Clear the last line. f.write("\r" + " " * (cols-1)) f.flush() # Return and write the data. out = "\r" + data if not out.endswith("\n"): out += "\n" f.write(out) # Write our string. f.write(self.line + "\r") f.flush()
def print_fitting(path): img = Image.open(path) termsize = shutil.get_terminal_size((9001, 9001)) if args.bilevel: scale = min(1, termsize[0]/img.size[0], 2*(termsize[1]-2)/(img.size[1])) newsize = (2*int(scale*img.size[0]), int(scale*img.size[1])) newimg = img.convert("1").resize(newsize, Image.LANCZOS) print_image_bl(newimg) else: scale = min(1, termsize[0]/img.size[0], 2*(termsize[1]-2)/(img.size[1])) newsize = (int(scale*img.size[0]), int(scale*img.size[1])) newimg = img.convert("RGBA").resize(newsize, Image.LANCZOS) print_image_tc(newimg)
def get_hr(): term_size = shutil.get_terminal_size((80, 25)) hr = '=' * term_size.columns return hr
def fill(self, string, indent=' ', max_width=72): """Wrap string so it fits within at most ``max_width`` columns. If the terminal is less than ``max_width`` columns, the string will be filled into that smaller width instead. """ if not isinstance(string, str): string = ' '.join(string) width = min(max_width, get_terminal_size().columns) return textwrap.fill( string, width=width, initial_indent=indent, subsequent_indent=indent, break_on_hyphens=False)
def exec_command(self, client, command, timeout=None, get_pty=False, environment=None): channel = client._transport.open_session(timeout=timeout) if get_pty: width, height = get_terminal_size() channel.get_pty(width=width, height=height) channel.settimeout(timeout) if environment: channel.update_environment(environment) channel.exec_command(command) return channel
def __init__(self, parent): self._parent = parent self.width = shutil.get_terminal_size((80, 20)).columns - 5
def print_icon(): """ Prints the MUTE Icon according to the width & height """ global FIRST_START if FIRST_START is False: os.system('clear') width, height = shutil.get_terminal_size((80, 20)) space = (width - 39) // 2 * ' ' middle = (height - 20) // 2 for _ in range(middle - 10): print('') # Which is a '\n' print(space + W + ' ####' + R + '#####' + W + '## ##' + R + '#####' + W + '####') print(space + R + ' #### ####') print(space + R + ' ### ###') print(space + R + ' ## ##') print(space + R + ' #########') print(space + R + ' #########') print(space + R + ' ## ##') print(space + R + ' ### ###') print(space + R + ' ###### ######') print(space + W + '########' + R + '#####' + W + '# #' + R + '#####' + W + '########') print('\n') if not height < 31: space = (width - 37) // 2 * ' ' print(space + R + '## ## ' + W + '## ## ######## ######## ') print(space + R + '### ### ' + W + '## ## ## ## ') print(space + R + '#### #### ' + W + '## ## ## ## ') print(space + R + '## ### ## ' + W + '## ## ## ###### ') print(space + R + '## ## ' + W + '## ## ## ## ') print(space + R + '## ## ' + W + '## ## ## ## ') print(space + R + '## ## ' + W + ' ####### ## ######## ') space = (width - 32) // 2 * ' ' print('\n' + space + GR + '(C) K4YT3X 2017 (C) fa11en 2017' + W) FIRST_START = False
def __init__(self, output, columns = None): super().__init__(output) if columns is None: columns, lines = shutil.get_terminal_size((80, 24)) self.columns = columns self.chars_on_line = 0 self._prefix = '' self.prefix_printable_length = 0 self.break_regex = re.compile(' ')
def refresh(self, cur_len): terminal_width = get_terminal_size().columns # ?????? info = "%s '%s'... %.2f%%" % (self.prefix_info, self.title, cur_len/self.total * 100) while len(info) > terminal_width - 20: self.title = self.title[0:-4] + '...' info = "%s '%s'... %.2f%%" % (self.prefix_info, self.title, cur_len/self.total * 100) end_str = '\r' if cur_len < self.total else '\n' print(info, end=end_str)
def count_down(self): terminal_width = get_terminal_size().columns for i in range(self.count_time + 1): info = "%s %ss" % (self.prefix_info, self.count_time - i) print(' ' * (terminal_width - 5), end='\r') print(info, end='\r') sleep(1)
def __init__(self, option_cls): self.__option_cls = option_cls self._parser = argparse.ArgumentParser(formatter_class= lambda prog: argparse.HelpFormatter(prog, width=shutil.get_terminal_size()[0]))
def get_terminal_size(fallback=(80, 24)): return fallback
def clear_and_output(self): """ Clears the terminal up to the right line then outputs the information of the task. """ # See if output is paused if self.output_paused: return # OK, print with console_lock: # Get terminal width terminal_width = shutil.get_terminal_size((80, 20)).columns # Get the output we need to print output = list(self.output(terminal_width)) # Scroll the terminal down/up enough for any new lines needed_lines = len(output) new_lines = needed_lines - self.cleared_lines if new_lines > 0: print("\n" * new_lines, flush=True, end="") elif new_lines < 0: print( (UP_ONE + CLEAR_LINE) * abs(new_lines), flush=True, end="", ) self.cleared_lines = needed_lines # Move cursor to top of cleared section print( (UP_ONE + CLEAR_LINE) * needed_lines, flush=True, end="", ) for line in output: print(line)
def __init__(self, get, update_channel, **options): """Init AutomaBot. :param get: The Queue reader side. :param update_channel: The notification channel id :param **options: Default commands.Bot parameters """ super().__init__(**options) self.get = get self.update_channel = update_channel self.terminal_width = shutil.get_terminal_size((80, 20))[0]
def get_terminal_dimensions(): columns, lines = shutil.get_terminal_size() return lines, columns
def __init__(self, filename=None, timing=False, **kwargs): cmd.Cmd.__init__(self, **kwargs) if 'stdin' in kwargs: cmd.Cmd.use_rawinput = 0 self.real_stdout = self.stdout self.smart_stdout = SmartFile(self.stdout) self.stderr = SmartFile(sys.stderr) self.filename = filename self.line_num = 0 self.timing = timing global cur_dir cur_dir = os.getcwd() self.prev_dir = cur_dir self.columns = shutil.get_terminal_size().columns self.redirect_dev = None self.redirect_filename = '' self.redirect_mode = '' self.quit_when_no_output = False self.quit_serial_reader = False readline.set_completer_delims(DELIMS) self.set_prompt()
def get_terminal_size(defaultx=80, defaulty=25): return _get_terminal_size((defaultx, defaulty))
def get_terminal_size(): size = namedtuple("_", ["rows", "columns"]) try: rows, columns = subprocess.check_output( ['stty', 'size'], stderr=subprocess.STDOUT ).split() return size(rows=int(rows), columns=int(columns)) # this won't work # - on windows (FileNotFoundError/OSError) # - python 2.6 (AttributeError) # - if the output is somehow mangled (ValueError) except (ValueError, FileNotFoundError, OSError, AttributeError, subprocess.CalledProcessError): return size(rows=0, columns=0)
def report(vulns, full=False, json_report=False, bare_report=False, checked_packages=0, db=None, key=None): if bare_report: return BareReport.render(vulns, full=full) if json_report: return JsonReport.render(vulns, full=full) size = get_terminal_size() used_db = get_used_db(key=key, db=db) if size.columns >= 80: return SheetReport.render(vulns, full=full, checked_packages=checked_packages, used_db=used_db) return BasicReport.render(vulns, full=full, checked_packages=checked_packages, used_db=used_db)
def expand(lines, indent=4, maxwidth=100): """Expand all tabstops (\t) in each line intelligently "Intelligently" means that consecutive lines with the same amount of '\t' characters are treated like a table, giving each column the same space. Return `lines` with all tabs expanded """ width = min(get_terminal_size()[0], maxwidth) expanded = [] for section in _split_sections(_explode(lines, indent)): widths = _col_widths(section) for line in section: expanded.append(_join_line(line, widths, width)) return expanded
def _width(self) -> int: """Get the number of columns to wrap text to. This is either the width of the terminal window or the maximum width set in the Formatter instance, whichever is smaller. """ if self.formatter.auto_width: return min( self.formatter.max_width, shutil.get_terminal_size().columns) else: return self.formatter.max_width
def get_terminal_size(): size = get_terminal_dimensions((120, 40)) return TerminalSize(size.columns, size.lines)
def __init__(self, lifecycle, stop_event, *args): self.lifecycle = lifecycle self.stop_event = stop_event self.last_size = get_terminal_size() self.should_run = True super(ResizeChecker, self).__init__()
def run(self): logger.debug('ResizeChecker started') while not self.stop_event.wait(self.CHECK_INTERVAL): new_size = get_terminal_size() if self.last_size != get_terminal_size(): logger.debug('Terminal size changed to %s' % (new_size, )) self.lifecycle.player.trigger_redraw() self.last_size = new_size
def __init__(self, prog): terminal_width = shutil.get_terminal_size().columns os.environ['COLUMNS'] = str(terminal_width) max_help_position = min(max(24, terminal_width // 3), 40) super().__init__(prog, max_help_position=max_help_position)
def test_does_not_crash(self): """Check if get_terminal_size() returns a meaningful value. There's no easy portable way to actually check the size of the terminal, so let's check if it returns something sensible instead. """ size = shutil.get_terminal_size() self.assertGreaterEqual(size.columns, 0) self.assertGreaterEqual(size.lines, 0)
def test_os_environ_first(self): "Check if environment variables have precedence" with support.EnvironmentVarGuard() as env: env['COLUMNS'] = '777' size = shutil.get_terminal_size() self.assertEqual(size.columns, 777) with support.EnvironmentVarGuard() as env: env['LINES'] = '888' size = shutil.get_terminal_size() self.assertEqual(size.lines, 888)
def _pretty_wrap(text, pretext, *, width=-1): if width is None: return pretext + text elif width == -1: width = shutil.get_terminal_size().columns l1, *lx = textwrap.wrap(text, width=width - 1 - len(pretext)) lx = [((' ' * len(pretext)) + l).rstrip().ljust(width) for l in lx] l1 = (pretext + l1).ljust(width) return ''.join([l1, *lx])
def __del__(self): if self.draw: print(' ' * (get_terminal_size().columns-1), end='\r')
def _pprint_meter(self, perc, *, char='#', text='', shift=True): tx, ty = get_terminal_size() if shift: outstr = text + "{}".format(char * (int((tx - len(text)) * perc) - 1)) else: outstr = text + "{}".format(char * (int(tx * perc) - 1))[len(text):] print(outstr.ljust(tx - 1), end='\r')
def callback(parser): @ivy.hooks.register('main') def tree_callback(): if not ivy.site.home(): sys.exit("Error: cannot locate the site's home directory.") cols, _ = shutil.get_terminal_size() print('?' * cols) print('Site: %s' % ivy.site.home()) print('?' * cols) print(ivy.nodes.root().str()) print('?' * cols)
def get_available_screen_for_user_agent(): """ Get the available length for the user agent """ column_padding = 18 avail_length = 0 for c in Client.query.all(): columns_length = len(str(c.id)) + len(str(c.client_id)) + len(str(c.last_beaconed)) + len(str(c.ip)) current_avail_length = shutil.get_terminal_size().columns - columns_length - column_padding avail_length = max(avail_length, current_avail_length) return avail_length
def __init__(self, num_lines=shutil.get_terminal_size().lines): self.num_lines = num_lines
def _size_36(): """ returns the rows, columns of terminal """ from shutil import get_terminal_size dim = get_terminal_size() if isinstance(dim, list): return dim[0], dim[1] return dim.lines, dim.columns
def print_command(self, keyword, description, max_len, out_stream): keyword_column = ('{0: <%s} ' % max_len).format(keyword) if six.PY3: width = shutil.get_terminal_size().columns or 80 else: width = 80 output = textwrap.fill(description, width=width, initial_indent=keyword_column, subsequent_indent=' '*(max_len+2)) print(output, file=out_stream)