Skip to content
Myr

Added an example for the terminal ui functions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent f8efecf · Browse files · 3 files+93 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+93 added−0 removed

  • other:+93 added−0 removed

3 files+93 added−0 removed

added:examples/termui/README+9 added
1$ termui_23 termui showcases the different terminal UI helpers that4 click provides.56Usage:78 $ pip install --editable .9 $ termui --help
added:examples/termui/setup.py+16 added
1from setuptools import setup23setup(4 name='click-example-termui',5 version='1.0',6 py_modules=['termui'],7 include_package_data=True,8 install_requires=[9 'Click',10 'colorama',11 ],12 entry_points='''13 [console_scripts]14 termui=termui:cli15 ''',16)
added:examples/termui/termui.py+68 added
1# coding: utf-82import click3import time4import random5from colorama import Fore67try:8 range_type = xrange9except NameError:10 range_type = range111213@click.group()14def cli():15 """This script showcases different terminal UI helpers in Click."""16 pass171819@cli.command()20def colordemo():21 """Demonstrates ANSI color support."""22 click.echo(Fore.YELLOW + 'Hello World!' + Fore.RESET)232425@cli.command()26def pager():27 """Demonstrates using the pager."""28 lines = []29 for x in xrange(200):30 lines.append('%s%d%s. Hello World!' % (31 Fore.GREEN,32 x,33 Fore.RESET34 ))35 click.echo_via_pager('\n'.join(lines))363738@cli.command()39@click.option('--count', default=8000, type=click.IntRange(1, 100000),40 help='The number of items to process.')41def progress(count):42 """Demonstrates the progress bar."""43 items = range_type(count)4445 def process_slowly(item):46 time.sleep(0.002 * random.random())4748 def filter(items):49 for item in items:50 if random.random() > 0.3:51 yield item5253 with click.progressbar(items, label='Processing user accounts',54 fill_char=Fore.GREEN + '#' + Fore.RESET) as bar:55 for item in bar:56 process_slowly(item)5758 with click.progressbar(filter(items), label='Committing transaction',59 fill_char=Fore.YELLOW + '#' + Fore.RESET) as bar:60 for item in bar:61 process_slowly(item)6263 with click.progressbar(length=count, label='Counting',64 bar_template='%(label)s %(bar)s | %(info)s',65 fill_char=Fore.BLUE + u'█' + Fore.RESET,66 empty_char=' ') as bar:67 for item in bar:68 process_slowly(item)