summaryrefslogtreecommitdiff
path: root/examples/termui
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2014-05-26 12:12:11 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2014-05-26 12:12:11 +0200
commit0686ce1fc459240f4d7cda5204b197ba94dd8d50 (patch)
treea274482b802e89c30e6507843607997b512ad6fa /examples/termui
parentf8efecfe93a87085fa702721f23bb571933866bc (diff)
downloadclick-0686ce1fc459240f4d7cda5204b197ba94dd8d50.tar.gz
Added an example for the terminal ui functions
Diffstat (limited to 'examples/termui')
-rw-r--r--examples/termui/README9
-rw-r--r--examples/termui/setup.py16
-rw-r--r--examples/termui/termui.py68
3 files changed, 93 insertions, 0 deletions
diff --git a/examples/termui/README b/examples/termui/README
new file mode 100644
index 0000000..93d53b0
--- /dev/null
+++ b/examples/termui/README
@@ -0,0 +1,9 @@
+$ termui_
+
+ termui showcases the different terminal UI helpers that
+ click provides.
+
+Usage:
+
+ $ pip install --editable .
+ $ termui --help
diff --git a/examples/termui/setup.py b/examples/termui/setup.py
new file mode 100644
index 0000000..323cf28
--- /dev/null
+++ b/examples/termui/setup.py
@@ -0,0 +1,16 @@
+from setuptools import setup
+
+setup(
+ name='click-example-termui',
+ version='1.0',
+ py_modules=['termui'],
+ include_package_data=True,
+ install_requires=[
+ 'Click',
+ 'colorama',
+ ],
+ entry_points='''
+ [console_scripts]
+ termui=termui:cli
+ ''',
+)
diff --git a/examples/termui/termui.py b/examples/termui/termui.py
new file mode 100644
index 0000000..0d5d31b
--- /dev/null
+++ b/examples/termui/termui.py
@@ -0,0 +1,68 @@
+# coding: utf-8
+import click
+import time
+import random
+from colorama import Fore
+
+try:
+ range_type = xrange
+except NameError:
+ range_type = range
+
+
+@click.group()
+def cli():
+ """This script showcases different terminal UI helpers in Click."""
+ pass
+
+
+@cli.command()
+def colordemo():
+ """Demonstrates ANSI color support."""
+ click.echo(Fore.YELLOW + 'Hello World!' + Fore.RESET)
+
+
+@cli.command()
+def pager():
+ """Demonstrates using the pager."""
+ lines = []
+ for x in xrange(200):
+ lines.append('%s%d%s. Hello World!' % (
+ Fore.GREEN,
+ x,
+ Fore.RESET
+ ))
+ click.echo_via_pager('\n'.join(lines))
+
+
+@cli.command()
+@click.option('--count', default=8000, type=click.IntRange(1, 100000),
+ help='The number of items to process.')
+def progress(count):
+ """Demonstrates the progress bar."""
+ items = range_type(count)
+
+ def process_slowly(item):
+ time.sleep(0.002 * random.random())
+
+ def filter(items):
+ for item in items:
+ if random.random() > 0.3:
+ yield item
+
+ with click.progressbar(items, label='Processing user accounts',
+ fill_char=Fore.GREEN + '#' + Fore.RESET) as bar:
+ for item in bar:
+ process_slowly(item)
+
+ with click.progressbar(filter(items), label='Committing transaction',
+ fill_char=Fore.YELLOW + '#' + Fore.RESET) as bar:
+ for item in bar:
+ process_slowly(item)
+
+ with click.progressbar(length=count, label='Counting',
+ bar_template='%(label)s %(bar)s | %(info)s',
+ fill_char=Fore.BLUE + u'█' + Fore.RESET,
+ empty_char=' ') as bar:
+ for item in bar:
+ process_slowly(item)