horizontal pretty print + status config

This commit is contained in:
Johann Dreo 2023-07-26 22:47:06 +02:00
commit 6d22bde91c

View file

@ -2,29 +2,48 @@ import sys
import agate import agate
import click import click
import tabulate
# Global group holding global options. # Global group holding global options.
@click.group() @click.group()
@click.option('-i', '--input', default='.klyban.csv', type=click.Path(writable=True, readable=True, allow_dash=True)) @click.option('-i', '--input', help="CSV data file.", default='.klyban.csv', type=click.Path(writable=True, readable=True, allow_dash=True))
@click.option('--status-key', help="Header key defining the status of items.", default='STATUS', type=str)
@click.option('--status-list', help="Comma-separated, ordered list of possible values for the status of items.", default='TODO,DOING,HOLD,DONE', type=str)
@click.pass_context @click.pass_context
def cli(context,input): def cli(context, input, status_key, status_list):
# ensure that context.obj exists and is a dict (in case `cli()` is called # ensure that context.obj exists and is a dict (in case `cli()` is called
# by means other than the `if` block below) # by means other than the `if` block below)
context.ensure_object(dict) context.ensure_object(dict)
context.obj['input'] = input context.obj['input'] = input
context.obj['status_key'] = status_key
context.obj['status_list'] = status_list.split(',')
@cli.command() @cli.command()
@click.pass_context @click.pass_context
def show(context): def show(context):
"""Show the kanban."""
# Automagically manages standard input if input=="-", thanks to allow_dash=True.
with click.open_file(context.obj['input'], mode='r') as fd: with click.open_file(context.obj['input'], mode='r') as fd:
table = agate.Table.from_csv(fd) table = agate.Table.from_csv(fd)
table.print_table(output = sys.stdout) tables = table.group_by(context.obj['status_key'])
for k in context.obj['status_list']:
try:
table = tables[k]
except KeyError:
pass
else:
print(table.columns[0][0])
t = table.exclude(context.obj['status_key'])
print(tabulate.tabulate(t.columns, headers=t.column_names, tablefmt="fancy_grid"))
@cli.command() @cli.command()
@click.pass_context @click.pass_context
def config(context): def config(context):
"""Show the current configuration."""
click.echo('Configuration:') click.echo('Configuration:')
click.echo(f"Data file: `{context.obj['input']}`") click.echo(f"Data file: `{context.obj['input']}`")