fix all fields display

This commit is contained in:
Johann Dreo 2023-08-09 11:53:42 +02:00
commit 2da5685eb2

64
twf.py
View file

@ -9,7 +9,21 @@ import rich
from rich.console import Console
from rich.columns import Columns
def card(task, show_only = None):
def group_by(tasks, field):
"""Group tasks by field values."""
groups = {}
for task in tasks:
if field in task:
if task[field] in groups:
groups[ task[field] ].append(task)
else:
groups[ task[field] ] = [task]
return groups
##### Widgets #####
def card(task, in_widget = None, show_only = None):
"""Widget being a single task."""
if not show_only:
@ -24,45 +38,43 @@ def card(task, show_only = None):
desc = task["description"]
title = sid
segments = [desc.strip()]
segments = []
for key in show_only:
if key in task.keys() and key not in ["id", "description"]:
segments.append(str(task[key]))
val = task[key]
segment = f"{key}: "
if type(val) == str:
segments.append(segment+t)
elif type(val) == list:
g = Columns([f"+{t}" for t in val])
segments.append(g)
else:
segments.append(segment+str(val))
cols = Columns(segments)
panel = rich.panel.Panel(cols, title = title,
title_align="left", expand = True, padding = (0,0))
grp = rich.console.Group(desc.strip(), cols)
panel = rich.panel.Panel(grp, title = title,
title_align="left", expand = True, padding = (0,1))
return panel
def stack(tasks, show_only = None):
def stack(tasks, in_widget = None, show_only = None):
"""Widget being a stack of tasks widgets."""
stack = rich.table.Table()
stack.add_column("Tasks")
for task in tasks:
stack.add_row( card(task, show_only) )
stack.add_row( card(task, in_widget, show_only) )
return stack
def group_by(tasks, field):
"""Group tasks by field values."""
groups = {}
for task in tasks:
if field in task:
if task[field] in groups:
groups[ task[field] ].append(task)
else:
groups[ task[field] ] = [task]
return groups
def sections(tasks, field, values, show_only = None):
def sections(tasks, field, values, in_widget = None, show_only = None):
"""Widget being a panel of stack widgets."""
sections = []
groups = group_by(tasks, field)
for val in values:
if val in groups:
sections.append( rich.panel.Panel(stack(groups[val], show_only), title = val) )
sections.append( rich.panel.Panel(stack(groups[val], in_widget, show_only), title = val) )
return rich.console.Group(*sections)
@ -74,7 +86,7 @@ if __name__ == "__main__":
)
main = parser.add_argument_group('Main')
main.add_argument("-s", "--show", metavar="columns", type=str, default="id,priority,status,description,tags", nargs=1,
main.add_argument("-s", "--show", metavar="columns", type=str, default="id,urgency,description,tags", nargs=1,
help="Ordered list of columns to show.")
config = parser.add_argument_group('Config')
@ -90,7 +102,11 @@ if __name__ == "__main__":
cmd = ['task', 'export']
show_only = asked.show.split(asked.list_separator)
showed = asked.show.split(asked.list_separator)
if not showed:
show_only = None
else:
show_only = showed
try:
p = subprocess.Popen( " ".join(cmd),
@ -107,9 +123,9 @@ if __name__ == "__main__":
else:
jdata = json.loads(out)
# print(json.dumps(jdata, indent=4))
print(json.dumps(jdata, indent=4))
console = Console()
console.rule("taskwarrior-fancy")
console.print(sections(jdata, "status", ["pending","completed"], show_only))
console.print(sections(jdata, "status", ["pending","completed"], None, show_only))