use the Rich module

- instead of tabulate
- remove DEADLINE from default show_keys
This commit is contained in:
Johann Dreo 2023-07-29 15:42:09 +02:00
commit 81f0bc062c
2 changed files with 29 additions and 16 deletions

View file

@ -7,7 +7,7 @@ title_key = TITLE
details_key = DETAILS details_key = DETAILS
tags_key = TAGS tags_key = TAGS
deadline_key = DEADLINE deadline_key = DEADLINE
show_keys = ID,TITLE,DETAILS,DEADLINE,TAGS show_keys = ID,TITLE,DETAILS,TAGS
[options.add] [options.add]
details = "" details = ""

View file

@ -8,7 +8,13 @@ from configparser import ConfigParser
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import click import click
import tabulate # import tabulate
import rich.console as richconsole
from rich.table import Table
from rich.text import Text
from rich.panel import Panel
from rich import box
error_codes = { error_codes = {
"INVALID_KEY": 100, "INVALID_KEY": 100,
@ -118,7 +124,7 @@ def check_id(context, param, value):
@click.option('-i', '--input' , help="CSV data file.", default='.klyban.csv', type=click.Path(writable=True, readable=True, allow_dash=True), show_default=True) @click.option('-i', '--input' , help="CSV data file.", default='.klyban.csv', type=click.Path(writable=True, readable=True, allow_dash=True), show_default=True)
# Display options. # Display options.
@click.option('-H','--show-headers', is_flag=True, help="Show the headers.") @click.option('-H','--show-headers', is_flag=True, help="Show the headers.")
@click.option('-S', '--show-keys' , default='ID,TITLE,DETAILS,DEADLINE,TAGS', type=str , show_default=True, help="Comma-separated, ordered list of fields that should be shown (use 'all' for everything).") @click.option('-S', '--show-keys' , default='ID,TITLE,DETAILS,TAGS', type=str , show_default=True, help="Comma-separated, ordered list of fields that should be shown (use 'all' for everything).")
# Low-level configuration options. # Low-level configuration options.
@click.option('--status-key' , default='STATUS' , type=str, show_default=True, help="Header key defining the status of tasks.") @click.option('--status-key' , default='STATUS' , type=str, show_default=True, help="Header key defining the status of tasks.")
@click.option('--status-list' , default='TODO,DOING,HOLD,DONE', type=str, show_default=True, help="Comma-separated, ordered list of possible values for the status of tasks.") @click.option('--status-list' , default='TODO,DOING,HOLD,DONE', type=str, show_default=True, help="Comma-separated, ordered list of possible values for the status of tasks.")
@ -189,27 +195,34 @@ def show(context, tid):
# Group by status. # Group by status.
tables = df.groupby(context.obj['status_key']) tables = df.groupby(context.obj['status_key'])
# Loop over the asked ordered status groups. # Loop over the asked ordered status groups.
for k in context.obj['status_list']: # Ordered. for section in context.obj['status_list']: # Ordered.
if k in tables.groups: if section in tables.groups:
df = tables.get_group(k) df = tables.get_group(section)
# Bring back TID as a regular column. # Bring back TID as a regular column.
df = df.reset_index() df = df.reset_index().fillna("")
# Print status as header.
print(k)
try: try:
# Print asked columns. # Print asked columns.
t = df[context.obj['show_keys']] t = df[context.obj['show_keys']]
except KeyError as e: except KeyError as e:
msg = "" msg = ""
for k in context.obj['show_keys']: for section in context.obj['show_keys']:
if k not in df.columns: if section not in df.columns:
msg += "cannot show field `{}`, not found in `{}` ".format(k, context.obj['input']) msg += "cannot show field `{}`, not found in `{}` ".format(section, context.obj['input'])
error("INVALID_KEY", msg) error("INVALID_KEY", msg)
else: else:
if context.obj['show_headers']: console = richconsole.Console()
print(tabulate.tabulate(t.fillna(""), headers=context.obj['show_keys'], tablefmt="fancy_grid", showindex=False)) # table = Table(show_header = context.obj['show_headers'], row_styles=["color(39)","color(33)"], box = None)
else: table = Table(show_header = context.obj['show_headers'], box = None)
print(tabulate.tabulate(t.fillna(""), tablefmt="fancy_grid", showindex=False)) for h in context.obj['show_keys']:
table.add_column(h)
for i,row in t.iterrows():
items = (str(row[k]) for k in context.obj['show_keys'])
table.add_row(*items)
# console.print(table)
# panel = Panel.fit(table, title = section, title_align="left", border_style="bold blue")
panel = Panel.fit(table, title = section, title_align="left")
console.print(panel)
else: # tid is not None. else: # tid is not None.
# Show a task card. # Show a task card.