Skip to content
Myr

termui.py

This file

Path

examples/termui/termui.py

156 lines · 4.2 KiB · Python

Last changed by

Added support for manually advancing progressbars.Armin Ronacherbefore adoption90d7ee9

History of this file

Take it

The walk stopped after 200 commits: lines older than 547a4d0 point at it.

This is 3.0 (clonk clonk)Armin Ronacherbefore adoption547a4d0

lines 1–2why, one step back
1# coding: utf-8
2import click

Added support for manually advancing progressbars.Armin Ronacherbefore adoption90d7ee9

line 3why, one step back
3import math

This is 3.0 (clonk clonk)Armin Ronacherbefore adoption547a4d0

lines 4–77why, one step back
4import time
5import random67try: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 for color in 'red', 'green', 'blue':23 click.echo(click.style('I am colored %s' % color, fg=color))24 click.echo(click.style('I am background colored %s' % color, bg=color))252627@cli.command()28def pager():29 """Demonstrates using the pager."""30 lines = []31 for x in range_type(200):32 lines.append('%s. Hello World!' % click.style(str(x), fg='green'))33 click.echo_via_pager('\n'.join(lines))343536@cli.command()37@click.option('--count', default=8000, type=click.IntRange(1, 100000),38 help='The number of items to process.')39def progress(count):40 """Demonstrates the progress bar."""41 items = range_type(count)4243 def process_slowly(item):44 time.sleep(0.002 * random.random())4546 def filter(items):47 for item in items:48 if random.random() > 0.3:49 yield item5051 with click.progressbar(items, label='Processing accounts',52 fill_char=click.style('#', fg='green')) as bar:53 for item in bar:54 process_slowly(item)5556 def show_item(item):57 if item is not None:58 return 'Item #%d' % item5960 with click.progressbar(filter(items), label='Committing transaction',61 fill_char=click.style('#', fg='yellow'),62 item_show_func=show_item) as bar:63 for item in bar:64 process_slowly(item)6566 with click.progressbar(length=count, label='Counting',67 bar_template='%(label)s %(bar)s | %(info)s',68 fill_char=click.style(u'█', fg='cyan'),69 empty_char=' ') as bar:70 for item in bar:71 process_slowly(item)7273 with click.progressbar(length=count, width=0, show_percent=False,74 show_eta=False,75 fill_char=click.style('#', fg='magenta')) as bar:76 for item in bar:77 process_slowly(item)

Added support for manually advancing progressbars.Armin Ronacherbefore adoption90d7ee9

lines 78–87why, one step back
78
79 # 'Non-linear progress bar'80 steps = [math.exp( x * 1. / 20) - 1 for x in range(20)]81 count = int(sum(steps))82 with click.progressbar(length=count, show_percent=False,83 label='Slowing progress bar',84 fill_char=click.style(u'█', fg='green')) as bar:85 for item in steps:86 time.sleep(item)87 bar.update(item)

This is 3.0 (clonk clonk)Armin Ronacherbefore adoption547a4d0

lines 88–156why, one step back
88
8990@cli.command()91@click.argument('url')92def open(url):93 """Opens a file or URL In the default application."""94 click.launch(url)959697@cli.command()98@click.argument('url')99def locate(url):100 """Opens a file or URL In the default application."""101 click.launch(url, locate=True)102103104@cli.command()105def edit():106 """Opens an editor with some text in it."""107 MARKER = '# Everything below is ignored\n'108 message = click.edit('\n\n' + MARKER)109 if message is not None:110 msg = message.split(MARKER, 1)[0].rstrip('\n')111 if not msg:112 click.echo('Empty message!')113 else:114 click.echo('Message:\n' + msg)115 else:116 click.echo('You did not enter anything!')117118119@cli.command()120def clear():121 """Clears the entire screen."""122 click.clear()123124125@cli.command()126def pause():127 """Waits for the user to press a button."""128 click.pause()129130131@cli.command()132def menu():133 """Shows a simple menu."""134 menu = 'main'135 while 1:136 if menu == 'main':137 click.echo('Main menu:')138 click.echo(' d: debug menu')139 click.echo(' q: quit')140 char = click.getchar()141 if char == 'd':142 menu = 'debug'143 elif char == 'q':144 menu = 'quit'145 else:146 click.echo('Invalid input')147 elif menu == 'debug':148 click.echo('Debug menu')149 click.echo(' b: back')150 char = click.getchar()151 if char == 'b':152 menu = 'main'153 else:154 click.echo('Invalid input')155 elif menu == 'quit':156 return