Skip to content
Myr

termui.py

This file

Path

examples/termui/termui.py

169 lines · 4.0 KiB · Python

Last changed by

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

History of this file

Take it

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

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 1–2why, one step back
1import math
2import random

Merge branch '7.x'David Lordbefore adoption7239794

line 3why, one step back
3import time

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 4why, one step back
4

Merge branch '7.x'David Lordbefore adoption7239794

line 5why, one step back
5import click

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 6–16why, one step back
6
78@click.group()9def cli():10 """This script showcases different terminal UI helpers in Click."""11 pass121314@cli.command()15def colordemo():16 """Demonstrates ANSI color support."""

Merge branch '7.x'David Lordbefore adoption7239794

line 17why, one step back
17 for color in "red", "green", "blue":

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

lines 18–19why, one step back
18 click.echo(click.style(f"I am colored {color}", fg=color))
19 click.echo(click.style(f"I am background colored {color}", bg=color))

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 20–25why, one step back
20
2122@cli.command()23def pager():24 """Demonstrates using the pager."""25 lines = []

Merge branch '7.x'David Lordbefore adoption7239794

line 26why, one step back
26 for x in range(200):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 27why, one step back
27 lines.append(f"{click.style(str(x), fg='green')}. Hello World!")

Merge branch '7.x'David Lordbefore adoption7239794

line 28why, one step back
28 click.echo_via_pager("\n".join(lines))

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 29–31why, one step back
29
3031@cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

lines 32–37why, one step back
32@click.option(
33 "--count",34 default=8000,35 type=click.IntRange(1, 100000),36 help="The number of items to process.",37)

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 38–39why, one step back
38def progress(count):
39 """Demonstrates the progress bar."""

Merge branch '7.x'David Lordbefore adoption7239794

line 40why, one step back
40 items = range(count)

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 41–49why, one step back
41
42 def process_slowly(item):43 time.sleep(0.002 * random.random())4445 def filter(items):46 for item in items:47 if random.random() > 0.3:48 yield item49

Merge branch '7.x'David Lordbefore adoption7239794

lines 50–52why, one step back
50 with click.progressbar(
51 items, label="Processing accounts", fill_char=click.style("#", fg="green")52 ) as bar:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 53–57why, one step back
53 for item in bar:
54 process_slowly(item)5556 def show_item(item):57 if item is not None:

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 58why, one step back
58 return f"Item #{item}"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 59why, one step back
59

Merge branch '7.x'David Lordbefore adoption7239794

lines 60–65why, one step back
60 with click.progressbar(
61 filter(items),62 label="Committing transaction",63 fill_char=click.style("#", fg="yellow"),64 item_show_func=show_item,65 ) as bar:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 66–68why, one step back
66 for item in bar:
67 process_slowly(item)68

Merge branch '7.x'David Lordbefore adoption7239794

lines 69–72why, one step back
69 with click.progressbar(
70 length=count,71 label="Counting",72 bar_template="%(label)s %(bar)s | %(info)s",

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 73why, one step back
73 fill_char=click.style("█", fg="cyan"),

Merge branch '7.x'David Lordbefore adoption7239794

lines 74–75why, one step back
74 empty_char=" ",
75 ) as bar:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 76–78why, one step back
76 for item in bar:
77 process_slowly(item)78

Merge branch '7.x'David Lordbefore adoption7239794

lines 79–85why, one step back
79 with click.progressbar(
80 length=count,81 width=0,82 show_percent=False,83 show_eta=False,84 fill_char=click.style("#", fg="magenta"),85 ) as bar:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 86–89why, one step back
86 for item in bar:
87 process_slowly(item)8889 # 'Non-linear progress bar'

Merge branch '7.x'David Lordbefore adoption7239794

line 90why, one step back
90 steps = [math.exp(x * 1.0 / 20) - 1 for x in range(20)]

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 91why, one step back
91 count = int(sum(steps))

Merge branch '7.x'David Lordbefore adoption7239794

lines 92–95why, one step back
92 with click.progressbar(
93 length=count,94 show_percent=False,95 label="Slowing progress bar",

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 96why, one step back
96 fill_char=click.style("█", fg="green"),

Merge branch '7.x'David Lordbefore adoption7239794

line 97why, one step back
97 ) as bar:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 98–103why, one step back
98 for item in steps:
99 time.sleep(item)100 bar.update(item)101102103@cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 104why, one step back
104@click.argument("url")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 105–110why, one step back
105def open(url):
106 """Opens a file or URL In the default application."""107 click.launch(url)108109110@cli.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 111why, one step back
111@click.argument("url")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 112–119why, one step back
112def locate(url):
113 """Opens a file or URL In the default application."""114 click.launch(url, locate=True)115116117@cli.command()118def edit():119 """Opens an editor with some text in it."""

Merge branch '7.x'David Lordbefore adoption7239794

line 120why, one step back
120 MARKER = "# Everything below is ignored\n"

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 121why, one step back
121 message = click.edit(f"\n\n{MARKER}")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 122why, one step back
122 if message is not None:

Merge branch '7.x'David Lordbefore adoption7239794

line 123why, one step back
123 msg = message.split(MARKER, 1)[0].rstrip("\n")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 124why, one step back
124 if not msg:

Merge branch '7.x'David Lordbefore adoption7239794

line 125why, one step back
125 click.echo("Empty message!")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 126why, one step back
126 else:

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 127why, one step back
127 click.echo(f"Message:\n{msg}")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 128why, one step back
128 else:

Merge branch '7.x'David Lordbefore adoption7239794

line 129why, one step back
129 click.echo("You did not enter anything!")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 130–146why, one step back
130
131132@cli.command()133def clear():134 """Clears the entire screen."""135 click.clear()136137138@cli.command()139def pause():140 """Waits for the user to press a button."""141 click.pause()142143144@cli.command()145def menu():146 """Shows a simple menu."""

Merge branch '7.x'David Lordbefore adoption7239794

line 147why, one step back
147 menu = "main"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 148why, one step back
148 while 1:

Merge branch '7.x'David Lordbefore adoption7239794

lines 149–152why, one step back
149 if menu == "main":
150 click.echo("Main menu:")151 click.echo(" d: debug menu")152 click.echo(" q: quit")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 153why, one step back
153 char = click.getchar()

Merge branch '7.x'David Lordbefore adoption7239794

lines 154–157why, one step back
154 if char == "d":
155 menu = "debug"156 elif char == "q":157 menu = "quit"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 158why, one step back
158 else:

Merge branch '7.x'David Lordbefore adoption7239794

lines 159–162why, one step back
159 click.echo("Invalid input")
160 elif menu == "debug":161 click.echo("Debug menu")162 click.echo(" b: back")

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 163why, one step back
163 char = click.getchar()

Merge branch '7.x'David Lordbefore adoption7239794

lines 164–165why, one step back
164 if char == "b":
165 menu = "main"

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 166why, one step back
166 else:

Merge branch '7.x'David Lordbefore adoption7239794

lines 167–168why, one step back
167 click.echo("Invalid input")
168 elif menu == "quit":

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 169why, one step back
169 return