summaryrefslogtreecommitdiff
path: root/examples/termui
diff options
context:
space:
mode:
Diffstat (limited to 'examples/termui')
-rw-r--r--examples/termui/setup.py14
-rw-r--r--examples/termui/termui.py118
2 files changed, 75 insertions, 57 deletions
diff --git a/examples/termui/setup.py b/examples/termui/setup.py
index 14558e8..7791bae 100644
--- a/examples/termui/setup.py
+++ b/examples/termui/setup.py
@@ -1,17 +1,17 @@
from setuptools import setup
setup(
- name='click-example-termui',
- version='1.0',
- py_modules=['termui'],
+ name="click-example-termui",
+ version="1.0",
+ py_modules=["termui"],
include_package_data=True,
install_requires=[
- 'click',
+ "click",
# Colorama is only required for Windows.
- 'colorama',
+ "colorama",
],
- entry_points='''
+ entry_points="""
[console_scripts]
termui=termui:cli
- ''',
+ """,
)
diff --git a/examples/termui/termui.py b/examples/termui/termui.py
index 08f6a04..2af114d 100644
--- a/examples/termui/termui.py
+++ b/examples/termui/termui.py
@@ -15,9 +15,9 @@ def cli():
@cli.command()
def colordemo():
"""Demonstrates ANSI color support."""
- for color in 'red', 'green', 'blue':
- click.echo(click.style('I am colored %s' % color, fg=color))
- click.echo(click.style('I am background colored %s' % color, bg=color))
+ for color in "red", "green", "blue":
+ click.echo(click.style("I am colored %s" % color, fg=color))
+ click.echo(click.style("I am background colored %s" % color, bg=color))
@cli.command()
@@ -25,13 +25,17 @@ def pager():
"""Demonstrates using the pager."""
lines = []
for x in range(200):
- lines.append('%s. Hello World!' % click.style(str(x), fg='green'))
- click.echo_via_pager('\n'.join(lines))
+ lines.append("%s. Hello World!" % click.style(str(x), fg="green"))
+ 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.')
+@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(count)
@@ -44,54 +48,68 @@ def progress(count):
if random.random() > 0.3:
yield item
- with click.progressbar(items, label='Processing accounts',
- fill_char=click.style('#', fg='green')) as bar:
+ with click.progressbar(
+ items, label="Processing accounts", fill_char=click.style("#", fg="green")
+ ) as bar:
for item in bar:
process_slowly(item)
def show_item(item):
if item is not None:
- return 'Item #%d' % item
-
- with click.progressbar(filter(items), label='Committing transaction',
- fill_char=click.style('#', fg='yellow'),
- item_show_func=show_item) as bar:
+ return "Item #%d" % item
+
+ with click.progressbar(
+ filter(items),
+ label="Committing transaction",
+ fill_char=click.style("#", fg="yellow"),
+ item_show_func=show_item,
+ ) 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=click.style(u'█', fg='cyan'),
- empty_char=' ') as bar:
+ with click.progressbar(
+ length=count,
+ label="Counting",
+ bar_template="%(label)s %(bar)s | %(info)s",
+ fill_char=click.style(u"█", fg="cyan"),
+ empty_char=" ",
+ ) as bar:
for item in bar:
process_slowly(item)
- with click.progressbar(length=count, width=0, show_percent=False,
- show_eta=False,
- fill_char=click.style('#', fg='magenta')) as bar:
+ with click.progressbar(
+ length=count,
+ width=0,
+ show_percent=False,
+ show_eta=False,
+ fill_char=click.style("#", fg="magenta"),
+ ) as bar:
for item in bar:
process_slowly(item)
# 'Non-linear progress bar'
- steps = [math.exp( x * 1. / 20) - 1 for x in range(20)]
+ steps = [math.exp(x * 1.0 / 20) - 1 for x in range(20)]
count = int(sum(steps))
- with click.progressbar(length=count, show_percent=False,
- label='Slowing progress bar',
- fill_char=click.style(u'█', fg='green')) as bar:
+ with click.progressbar(
+ length=count,
+ show_percent=False,
+ label="Slowing progress bar",
+ fill_char=click.style(u"█", fg="green"),
+ ) as bar:
for item in steps:
time.sleep(item)
bar.update(item)
@cli.command()
-@click.argument('url')
+@click.argument("url")
def open(url):
"""Opens a file or URL In the default application."""
click.launch(url)
@cli.command()
-@click.argument('url')
+@click.argument("url")
def locate(url):
"""Opens a file or URL In the default application."""
click.launch(url, locate=True)
@@ -100,16 +118,16 @@ def locate(url):
@cli.command()
def edit():
"""Opens an editor with some text in it."""
- MARKER = '# Everything below is ignored\n'
- message = click.edit('\n\n' + MARKER)
+ MARKER = "# Everything below is ignored\n"
+ message = click.edit("\n\n" + MARKER)
if message is not None:
- msg = message.split(MARKER, 1)[0].rstrip('\n')
+ msg = message.split(MARKER, 1)[0].rstrip("\n")
if not msg:
- click.echo('Empty message!')
+ click.echo("Empty message!")
else:
- click.echo('Message:\n' + msg)
+ click.echo("Message:\n" + msg)
else:
- click.echo('You did not enter anything!')
+ click.echo("You did not enter anything!")
@cli.command()
@@ -127,26 +145,26 @@ def pause():
@cli.command()
def menu():
"""Shows a simple menu."""
- menu = 'main'
+ menu = "main"
while 1:
- if menu == 'main':
- click.echo('Main menu:')
- click.echo(' d: debug menu')
- click.echo(' q: quit')
+ if menu == "main":
+ click.echo("Main menu:")
+ click.echo(" d: debug menu")
+ click.echo(" q: quit")
char = click.getchar()
- if char == 'd':
- menu = 'debug'
- elif char == 'q':
- menu = 'quit'
+ if char == "d":
+ menu = "debug"
+ elif char == "q":
+ menu = "quit"
else:
- click.echo('Invalid input')
- elif menu == 'debug':
- click.echo('Debug menu')
- click.echo(' b: back')
+ click.echo("Invalid input")
+ elif menu == "debug":
+ click.echo("Debug menu")
+ click.echo(" b: back")
char = click.getchar()
- if char == 'b':
- menu = 'main'
+ if char == "b":
+ menu = "main"
else:
- click.echo('Invalid input')
- elif menu == 'quit':
+ click.echo("Invalid input")
+ elif menu == "quit":
return