python-click/examples/termui/termui.py

170 lines
4.1 KiB
Python
Raw Normal View History

2015-07-16 14:26:14 +02:00
import math
2014-10-16 20:40:34 +02:00
import random
2020-07-21 08:23:42 +02:00
import time
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
import click
2014-10-16 20:40:34 +02:00
@click.group()
def cli():
"""This script showcases different terminal UI helpers in Click."""
pass
@cli.command()
def colordemo():
"""Demonstrates ANSI color support."""
2020-07-21 08:23:42 +02:00
for color in "red", "green", "blue":
2021-10-10 03:31:57 +02:00
click.echo(click.style(f"I am colored {color}", fg=color))
click.echo(click.style(f"I am background colored {color}", bg=color))
2014-10-16 20:40:34 +02:00
@cli.command()
def pager():
"""Demonstrates using the pager."""
lines = []
2020-07-21 08:23:42 +02:00
for x in range(200):
2021-10-10 03:31:57 +02:00
lines.append(f"{click.style(str(x), fg='green')}. Hello World!")
2020-07-21 08:23:42 +02:00
click.echo_via_pager("\n".join(lines))
2014-10-16 20:40:34 +02:00
@cli.command()
2020-07-21 08:23:42 +02:00
@click.option(
"--count",
default=8000,
type=click.IntRange(1, 100000),
help="The number of items to process.",
)
2014-10-16 20:40:34 +02:00
def progress(count):
"""Demonstrates the progress bar."""
2020-07-21 08:23:42 +02:00
items = range(count)
2014-10-16 20:40:34 +02:00
def process_slowly(item):
time.sleep(0.002 * random.random())
def filter(items):
for item in items:
if random.random() > 0.3:
yield item
2020-07-21 08:23:42 +02:00
with click.progressbar(
items, label="Processing accounts", fill_char=click.style("#", fg="green")
) as bar:
2014-10-16 20:40:34 +02:00
for item in bar:
process_slowly(item)
def show_item(item):
if item is not None:
2021-10-10 03:31:57 +02:00
return f"Item #{item}"
2020-07-21 08:23:42 +02:00
with click.progressbar(
filter(items),
label="Committing transaction",
fill_char=click.style("#", fg="yellow"),
item_show_func=show_item,
) as bar:
2014-10-16 20:40:34 +02:00
for item in bar:
process_slowly(item)
2020-07-21 08:23:42 +02:00
with click.progressbar(
length=count,
label="Counting",
bar_template="%(label)s %(bar)s | %(info)s",
2021-10-10 03:31:57 +02:00
fill_char=click.style("", fg="cyan"),
2020-07-21 08:23:42 +02:00
empty_char=" ",
) as bar:
2014-10-16 20:40:34 +02:00
for item in bar:
process_slowly(item)
2020-07-21 08:23:42 +02:00
with click.progressbar(
length=count,
width=0,
show_percent=False,
show_eta=False,
fill_char=click.style("#", fg="magenta"),
) as bar:
2014-10-16 20:40:34 +02:00
for item in bar:
process_slowly(item)
2015-07-16 14:26:14 +02:00
# 'Non-linear progress bar'
2020-07-21 08:23:42 +02:00
steps = [math.exp(x * 1.0 / 20) - 1 for x in range(20)]
2015-07-16 14:26:14 +02:00
count = int(sum(steps))
2020-07-21 08:23:42 +02:00
with click.progressbar(
length=count,
show_percent=False,
label="Slowing progress bar",
2021-10-10 03:31:57 +02:00
fill_char=click.style("", fg="green"),
2020-07-21 08:23:42 +02:00
) as bar:
2015-07-16 14:26:14 +02:00
for item in steps:
time.sleep(item)
bar.update(item)
2014-10-16 20:40:34 +02:00
@cli.command()
2020-07-21 08:23:42 +02:00
@click.argument("url")
2014-10-16 20:40:34 +02:00
def open(url):
"""Opens a file or URL In the default application."""
click.launch(url)
@cli.command()
2020-07-21 08:23:42 +02:00
@click.argument("url")
2014-10-16 20:40:34 +02:00
def locate(url):
"""Opens a file or URL In the default application."""
click.launch(url, locate=True)
@cli.command()
def edit():
"""Opens an editor with some text in it."""
2020-07-21 08:23:42 +02:00
MARKER = "# Everything below is ignored\n"
2021-10-10 03:31:57 +02:00
message = click.edit(f"\n\n{MARKER}")
2014-10-16 20:40:34 +02:00
if message is not None:
2020-07-21 08:23:42 +02:00
msg = message.split(MARKER, 1)[0].rstrip("\n")
2014-10-16 20:40:34 +02:00
if not msg:
2020-07-21 08:23:42 +02:00
click.echo("Empty message!")
2014-10-16 20:40:34 +02:00
else:
2021-10-10 03:31:57 +02:00
click.echo(f"Message:\n{msg}")
2014-10-16 20:40:34 +02:00
else:
2020-07-21 08:23:42 +02:00
click.echo("You did not enter anything!")
2014-10-16 20:40:34 +02:00
@cli.command()
def clear():
"""Clears the entire screen."""
click.clear()
@cli.command()
def pause():
"""Waits for the user to press a button."""
click.pause()
@cli.command()
def menu():
"""Shows a simple menu."""
2020-07-21 08:23:42 +02:00
menu = "main"
2021-10-10 03:31:57 +02:00
while True:
2020-07-21 08:23:42 +02:00
if menu == "main":
click.echo("Main menu:")
click.echo(" d: debug menu")
click.echo(" q: quit")
2014-10-16 20:40:34 +02:00
char = click.getchar()
2020-07-21 08:23:42 +02:00
if char == "d":
menu = "debug"
elif char == "q":
menu = "quit"
2014-10-16 20:40:34 +02:00
else:
2020-07-21 08:23:42 +02:00
click.echo("Invalid input")
elif menu == "debug":
click.echo("Debug menu")
click.echo(" b: back")
2014-10-16 20:40:34 +02:00
char = click.getchar()
2020-07-21 08:23:42 +02:00
if char == "b":
menu = "main"
2014-10-16 20:40:34 +02:00
else:
2020-07-21 08:23:42 +02:00
click.echo("Invalid input")
elif menu == "quit":
2014-10-16 20:40:34 +02:00
return