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
Provenance
- Commit
0686ce1- Tree
a274482- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f8efecf
3 files changed+93 added−0 removed
added:examples/termui/README+9 added
1+$ termui_2+3+ termui showcases the different terminal UI helpers that4+ click provides.5+6+Usage:7+8+ $ pip install --editable .9+ $ termui --helpadded:examples/termui/setup.py+16 added
1+from setuptools import setup2+3+setup(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-82+import click3+import time4+import random5+from colorama import Fore6+7+try:8+ range_type = xrange9+except NameError:10+ range_type = range11+12+13+@click.group()14+def cli():15+ """This script showcases different terminal UI helpers in Click."""16+ pass17+18+19+@cli.command()20+def colordemo():21+ """Demonstrates ANSI color support."""22+ click.echo(Fore.YELLOW + 'Hello World!' + Fore.RESET)23+24+25+@cli.command()26+def 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))36+37+38+@cli.command()39+@click.option('--count', default=8000, type=click.IntRange(1, 100000),40+ help='The number of items to process.')41+def progress(count):42+ """Demonstrates the progress bar."""43+ items = range_type(count)44+45+ def process_slowly(item):46+ time.sleep(0.002 * random.random())47+48+ def filter(items):49+ for item in items:50+ if random.random() > 0.3:51+ yield item52+53+ 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)57+58+ 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)62+63+ 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)