1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# 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)
def show_item(item):
if item is not None:
return 'Item #%d' % item
with click.progressbar(filter(items), label='Committing transaction',
fill_char=Fore.YELLOW + '#' + Fore.RESET,
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=Fore.BLUE + u'█' + Fore.RESET,
empty_char=' ') as bar:
for item in bar:
process_slowly(item)
@cli.command()
def clear():
"""Clears the entire screen."""
click.clear()
|