我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用click.get_terminal_size()。
def ctl_each(obj, command, arguments): '''Invoke any kubectl command directly for each pod matched and collate the output.''' width, height = click.get_terminal_size() kubectl = obj.kubey.kubectl collector = tabular.RowCollector() ns_pods = defaultdict(list) for pod in obj.kubey.each_pod(obj.maximum): ns_pods[pod.namespace].append(pod) for ns, pods in ns_pods.items(): args = ['-n', ns] + list(arguments) + [p.name for p in pods] kubectl.call_table_rows(collector.handler_for(ns), command, *args) kubectl.wait() if collector.rows: click.echo(tabular.tabulate(obj, sorted(collector.rows), collector.headers)) if kubectl.final_rc != 0: click.get_current_context().exit(kubectl.final_rc)
def list_examples(self): if isdir(self.examples_dir): # examples = sorted(os.listdir(self.examples_dir)) examples = [dirname(y).replace(self.examples_dir + sep, '') for x in os.walk(self.examples_dir) for y in glob.glob(util.safe_join(x[0], 'info'))] click.secho('') for example in examples: example_dir = util.safe_join(self.examples_dir, example) if isdir(example_dir): info_path = util.safe_join(example_dir, 'info') info = '' if isfile(info_path): with codecs.open(info_path, 'r', 'utf-8') as info_file: info = info_file.read().replace('\n', '') click.secho(' ' + example, fg='blue', bold=True) click.secho('-' * click.get_terminal_size()[0]) click.secho(' ' + info) click.secho('') click.secho(EXAMPLE_DIR_FILE, fg='green') click.secho(EXAMPLE_OF_USE_CAD, fg='green') else: util._check_package('examples') return 1 return 0
def list_boards(self): """Return a list with all the supported boards""" # Print table click.echo('\nSupported boards:\n') BOARDLIST_TPL = ('{board:22} {fpga:20} {type:<5} {size:<5} {pack:<10}') terminal_width, _ = click.get_terminal_size() click.echo('-' * terminal_width) click.echo(BOARDLIST_TPL.format( board=click.style('Board', fg='cyan'), fpga='FPGA', type='Type', size='Size', pack='Pack')) click.echo('-' * terminal_width) for board in self.boards: fpga = self.boards[board]['fpga'] click.echo(BOARDLIST_TPL.format( board=click.style(board, fg='cyan'), fpga=fpga, type=self.fpgas[fpga]['type'], size=self.fpgas[fpga]['size'], pack=self.fpgas[fpga]['pack'])) click.secho(BOARDS_MSG, fg='green')
def list_fpgas(self): """Return a list with all the supported FPGAs""" # Print table click.echo('\nSupported FPGAs:\n') FPGALIST_TPL = ('{fpga:30} {type:<5} {size:<5} {pack:<10}') terminal_width, _ = click.get_terminal_size() click.echo('-' * terminal_width) click.echo(FPGALIST_TPL.format( fpga=click.style('FPGA', fg='cyan'), type='Type', size='Size', pack='Pack')) click.echo('-' * terminal_width) for fpga in self.fpgas: click.echo(FPGALIST_TPL.format( fpga=click.style(fpga, fg='cyan'), type=self.fpgas[fpga]['type'], size=self.fpgas[fpga]['size'], pack=self.fpgas[fpga]['pack']))
def __init__(self, *args): super().__init__(*args) self._width = click.get_terminal_size()[0]
def separated(text, fg, sepchar='='): width = click.get_terminal_size()[0] text = text.center(width, sepchar) click.secho(text, fg=fg)
def test_click_installed(self): import click term_x, term_y = click.get_terminal_size() assert isinstance(term_x, int) assert isinstance(term_y, int)
def cuv(ctx, coverage_fname, exclude, branch): """ Cuv'ner provides ways to visualize your project's coverage data. Everything works on the console and assumes a unicode and 256-color capable terminal. There must be a .coverage file which is loaded for coverage data; it is assumed to be in the top level of your source code checkout. """ if coverage_fname is None: coverage_fname = find_coverage_data('.') # coverage_fname still could be None cfg = Config() ctx.obj = cfg cfg.nice_width = min(80, click.get_terminal_size()[0]) cfg.exclude = exclude cfg.branch = branch if coverage_fname is not None: cfg.data = coverage.Coverage(data_file=coverage_fname) cfg.data.load() else: raise click.UsageError( "No coverage data. Do you have a .coverage file?" )
def show_missing(data, file_coverage, common): max_fname = max([len(nm) - common for nm in file_coverage]) format_str = u'{:>%d}: {}' % (max_fname,) width = click.get_terminal_size()[0] for fname in file_coverage: analysis = create_analysis(data, fname) if len(analysis.missing): print(format_str.format(fname[common:], analysis._missing_formatted))
def print_banner(fname, percent, fill=None, pager=None): """ Prints out a coloured banner showing coverage percent :param fill: the width of the banner; if None, uses 80 or the terminal width, whichever is less. """ echo = pager.echo if pager else click.echo if fill is None: fill = min(click.get_terminal_size()[0], 80) echo(colors.color('-' * fill, bg=226, fg=236), color=True) maxsize = fill - len('coverage: ') - 3 truncfname = fname[-maxsize:] if len(truncfname) != len(fname): truncfname = u'...{}'.format(truncfname) echo(colors.color(u'coverage: {}'.format(truncfname).ljust(fill), bg=226, fg=236), color=True) grsize = int(fill * percent) if grsize >= 5: prcnt_formatted = u'%3d%%' % int(percent * 100.0) gr = colors.color(prcnt_formatted + (u' ' * (grsize - 4)), fg=255, bg=22) else: gr = colors.color(u' ' * grsize, bg=22) red = colors.color(u' ' * int(math.ceil(fill * (1.0 - percent))), bg=52) echo(gr + red, color=True) echo(colors.color(u'-' * fill, bg=226, fg=236), color=True)
def print_table(header, rows, data_type=None): width, _ = click.get_terminal_size() table = texttable.Texttable(max_width=width) table.set_deco(texttable.Texttable.HEADER | texttable.Texttable.BORDER | texttable.Texttable.VLINES) if data_type is not None: table.set_cols_dtype(data_type) table.header(header) for row in rows: table.add_row(row) click.echo(table.draw())
def __init__(self): self.rows = [] self.width, self.height = click.get_terminal_size()
def draw(self): """ Draw the Layout onto screen. """ self.width, self.height = click.get_terminal_size() for element in self.rows: element.draw()
def quickstart(): """Quickstart wizard for setting up twtxt.""" width = click.get_terminal_size()[0] width = width if width <= 79 else 79 click.secho("twtxt - quickstart", fg="cyan") click.secho("==================", fg="cyan") click.echo() help_text = "This wizard will generate a basic configuration file for twtxt with all mandatory options set. " \ "You can change all of these later with either twtxt itself or by editing the config file manually. " \ "Have a look at the docs to get information about the other available options and their meaning." click.echo(textwrap.fill(help_text, width)) click.echo() nick = click.prompt("? Please enter your desired nick", default=os.environ.get("USER", "")) def overwrite_check(path): if os.path.isfile(path): click.confirm("? '{0}' already exists. Overwrite?".format(path), abort=True) cfgfile = click.prompt("? Please enter the desired location for your config file", os.path.join(Config.config_dir, Config.config_name), type=click.Path(readable=True, writable=True, file_okay=True)) cfgfile = os.path.expanduser(cfgfile) overwrite_check(cfgfile) twtfile = click.prompt("? Please enter the desired location for your twtxt file", os.path.expanduser("~/twtxt.txt"), type=click.Path(readable=True, writable=True, file_okay=True)) twtfile = os.path.expanduser(twtfile) overwrite_check(twtfile) twturl = click.prompt("? Please enter the URL your twtxt file will be accessible from", default="https://example.org/twtxt.txt") disclose_identity = click.confirm("? Do you want to disclose your identity? Your nick and URL will be shared when " "making HTTP requests", default=False) click.echo() add_news = click.confirm("? Do you want to follow the twtxt news feed?", default=True) conf = Config.create_config(cfgfile, nick, twtfile, twturl, disclose_identity, add_news) twtfile_dir = os.path.dirname(twtfile) if not os.path.exists(twtfile_dir): os.makedirs(twtfile_dir) open(twtfile, "a").close() click.echo() click.echo("? Created config file at '{0}'.".format(click.format_filename(conf.config_file))) click.echo("? Created twtxt file at '{0}'.".format(click.format_filename(twtfile)))
def init(): """ Interactively initialize a Valohai project. """ project = get_project() if project: error( 'The directory {directory} is already linked to {name}. Please unlink the directory first.'.format( directory=project.directory, name=project.name, ) ) sys.exit(1) click.secho('Hello! This wizard will help you start a Valohai compatible project.', fg='green', bold=True) directory = get_project_directory() if not click.confirm( 'First, let\'s make sure {dir} is the root directory of your project. Is that correct?'.format( dir=click.style(directory, bold=True), ) ): # pragma: no cover click.echo('Alright! Please change to the root directory of your project and try again.') return valohai_yaml_path = os.path.join(directory, 'valohai.yaml') if not os.path.isfile(valohai_yaml_path): click.echo('Looks like you don\'t have a Valohai.yaml file. Let\'s create one!') yaml_wizard(directory) else: click.echo('There is a Valohai.yaml file in this directory, so let\'s skip the creation wizard.') try: get_host_and_token() except NotLoggedIn: # pragma: no cover error('Please log in with `vh login` before continuing.') sys.exit(3) link_or_create_prompt(directory) width = min(70, click.get_terminal_size()[0]) click.secho('*' * width, fg='green', bold=True) click.echo(DONE_TEXT.strip().format( command=click.style('vh exec run --adhoc --watch execute', bold=True), )) click.secho('*' * width, fg='green', bold=True)