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 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: 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: 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: for item in bar: process_slowly(item) # 'Non-linear progress bar' steps = [math.exp(x * 1. / 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: for item in steps: time.sleep(item) bar.update(item)
def test_styling(): examples = [ ('x', dict(fg='black'), '\x1b[30mx\x1b[0m'), ('x', dict(fg='red'), '\x1b[31mx\x1b[0m'), ('x', dict(fg='green'), '\x1b[32mx\x1b[0m'), ('x', dict(fg='yellow'), '\x1b[33mx\x1b[0m'), ('x', dict(fg='blue'), '\x1b[34mx\x1b[0m'), ('x', dict(fg='magenta'), '\x1b[35mx\x1b[0m'), ('x', dict(fg='cyan'), '\x1b[36mx\x1b[0m'), ('x', dict(fg='white'), '\x1b[37mx\x1b[0m'), ('x', dict(bg='black'), '\x1b[40mx\x1b[0m'), ('x', dict(bg='red'), '\x1b[41mx\x1b[0m'), ('x', dict(bg='green'), '\x1b[42mx\x1b[0m'), ('x', dict(bg='yellow'), '\x1b[43mx\x1b[0m'), ('x', dict(bg='blue'), '\x1b[44mx\x1b[0m'), ('x', dict(bg='magenta'), '\x1b[45mx\x1b[0m'), ('x', dict(bg='cyan'), '\x1b[46mx\x1b[0m'), ('x', dict(bg='white'), '\x1b[47mx\x1b[0m'), ('foo bar', dict(blink=True), '\x1b[5mfoo bar\x1b[0m'), ('foo bar', dict(underline=True), '\x1b[4mfoo bar\x1b[0m'), ('foo bar', dict(bold=True), '\x1b[1mfoo bar\x1b[0m'), ('foo bar', dict(dim=True), '\x1b[2mfoo bar\x1b[0m'), ] for text, styles, ref in examples: assert click.style(text, **styles) == ref assert click.unstyle(ref) == text
def test_echo_color_flag(monkeypatch, capfd): isatty = True monkeypatch.setattr(click._compat, 'isatty', lambda x: isatty) text = 'foo' styled_text = click.style(text, fg='red') assert styled_text == '\x1b[31mfoo\x1b[0m' click.echo(styled_text, color=False) out, err = capfd.readouterr() assert out == text + '\n' click.echo(styled_text, color=True) out, err = capfd.readouterr() assert out == styled_text + '\n' isatty = True click.echo(styled_text) out, err = capfd.readouterr() assert out == styled_text + '\n' isatty = False click.echo(styled_text) out, err = capfd.readouterr() assert out == text + '\n'
def cli(): """This script prints some colors. If colorama is installed this will also work on Windows. It will also automatically remove all ANSI styles if data is piped into a file. Give it a try! """ for color in all_colors: click.echo(click.style('I am colored %s' % color, fg=color)) for color in all_colors: click.echo( click.style('I am colored %s and bold' % color, fg=color, bold=True)) for color in all_colors: click.echo( click.style('I am reverse colored %s' % color, fg=color, reverse=True)) click.echo(click.style('I am blinking', blink=True)) click.echo(click.style('I am underlined', underline=True))
async def test_with_color(): @click.command() def cli(): click.secho('hello world', fg='blue') runner = CliRunner() result = await runner.invoke(cli) assert result.output == 'hello world\n' assert not result.exception result = await runner.invoke(cli, color=True) assert result.output == click.style('hello world', fg='blue') + '\n' assert not result.exception
def pager(): """Demonstrates using the pager.""" lines = [] for x in range_type(200): lines.append('%s. Hello World!' % click.style(str(x), fg='green')) click.echo_via_pager('\n'.join(lines))
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))