use Widget.rtext everywhere

This commit is contained in:
Johann Dreo 2023-08-28 12:32:05 +02:00
commit acc410214b

View file

@ -30,7 +30,7 @@ class Widget:
self.config = config self.config = config
self.list_separator = list_separator self.list_separator = list_separator
def swatch_of(key, val, prefix = "color."): def swatch_of(self, key, val, prefix = "color."):
if key: if key:
key = prefix+key key = prefix+key
value = re.sub(r"\s", "_", val) value = re.sub(r"\s", "_", val)
@ -62,9 +62,8 @@ class Widget:
else: # Not key. else: # Not key.
return "" return ""
def rtext(self, val, swatch, prefix = "color.", end="\n"):
def rtext(val, key, prefix = "color."): return rich.text.Text(val, style=self.swatch_of(swatch, val, prefix), end=end)
return rich.text.Text(val, style=swatch_of(key, val, prefix))
class Tasker(Widget): class Tasker(Widget):
@ -137,16 +136,16 @@ class task:
sid = str(task["id"]) sid = str(task["id"])
if ":" in task["description"]: if ":" in task["description"]:
short, desc = task["description"].split(":") short, desc = task["description"].split(":")
title = rich.text.Text(sid, style="color.id") + rich.text.Text(":", style="default") + rich.text.Text(short.strip(), style="color.description.short") title = self.rtext(sid, "id") + rich.text.Text(":", style="default") + self.rtext(short.strip(), "description.short")
desc = rich.text.Text("\n".join(textwrap.wrap(desc.strip(), self.wrap_width)), style="color.description.long") desc = rtext("\n".join(textwrap.wrap(desc.strip(), self.wrap_width)), "description.long")
elif len(task["description"]) <= self.wrap_width - 8: elif len(task["description"]) <= self.wrap_width - 8:
d = task["description"].strip() d = task["description"].strip()
title = rich.text.Text(sid, style="color.id") + rich.text.Text(":", style="default") + rich.text.Text(d, style="color.description.short") title = self.rtext(sid, "id") + rich.text.Text(":", style="default") + self.rtext(d, "description.short")
desc = None desc = None
else: else:
desc = task["description"] desc = task["description"]
desc = rich.text.Text("\n".join(textwrap.wrap(desc.strip(), self.wrap_width)), style="color.description") desc = self.rtext("\n".join(textwrap.wrap(desc.strip(), self.wrap_width)),"description")
title = rich.text.Text(sid, style="color.id") title = self.rtext(sid, "id")
segments = [] segments = []
for key in self.show_only: for key in self.show_only:
@ -154,22 +153,22 @@ class task:
val = task[key] val = task[key]
segment = f"{key}: " segment = f"{key}: "
if type(val) == str: if type(val) == str:
segments.append(rich.text.Text(segment+val, style=f"color.{key}")) segments.append( self.rtext(segment+val, f"{key}") )
elif type(val) == list: elif type(val) == list:
# FIXME Columns does not fit. # FIXME Columns does not fit.
# g = Columns([f"+{t}" for t in val], expand = False) # g = Columns([f"+{t}" for t in val], expand = False)
lst = [] lst = []
for t in val: for t in val:
lst.append( \ lst.append( \
rich.text.Text(self.tag_icons[0], style="color.tags.ends") + \ self.rtext(self.tag_icons[0], "tags.ends") + \
rich.text.Text(t, style=f"color.{key}") + \ self.rtext(t, f"{key}") + \
rich.text.Text(self.tag_icons[1], style="color.tags.ends") \ self.rtext(self.tag_icons[1], "tags.ends") \
) )
g = rich.console.Group(*lst, fit = True) g = rich.console.Group(*lst, fit = True)
# g = rich.console.Group(*[rich.text.Text(f"{self.tag_icons[0]}{t}{self.tag_icons[1]}", style=f"color.{key}") for t in val], fit = True) # g = rich.console.Group(*[rich.text.Text(f"{self.tag_icons[0]}{t}{self.tag_icons[1]}", style=f"color.{key}") for t in val], fit = True)
segments.append(g) segments.append(g)
else: else:
segments.append(rich.text.Text(segment+str(val), style=f"color.{key}")) segments.append(self.rtext(segment+str(val), f"{key}"))
# FIXME Columns does not fit. # FIXME Columns does not fit.
# cols = Columns(segments) # cols = Columns(segments)
@ -202,9 +201,9 @@ class task:
def __call__(self, task): def __call__(self, task):
title, body = self._make(task) title, body = self._make(task)
t = rich.text.Text(self.title_ends[0], style="color.description.short.ends") + \ t = self.rtext(self.title_ends[0], "description.short.ends") + \
title + \ title + \
rich.text.Text(self.title_ends[1], style="color.description.short.ends") self.rtext(self.title_ends[1], "description.short.ends")
sid = str(task["id"]) sid = str(task["id"])
if sid in self.touched: if sid in self.touched:
@ -284,7 +283,7 @@ class stack:
for task in self.sorter(tasks): for task in self.sorter(tasks):
taskers = self.tasker(task) taskers = self.tasker(task)
if str(task["id"]) in self.tasker.touched: if str(task["id"]) in self.tasker.touched:
row = [rich.text.Text("", style = "color.touched")] row = [self.rtext("", "r.touched")]
else: else:
row = [""] row = [""]
@ -304,13 +303,13 @@ class stack:
# )) # ))
# FIXME style leaks on all texts: # FIXME style leaks on all texts:
# (Note that "default" is a special color for Rich.) # (Note that "default" is a special color for Rich.)
row.append( rich.text.Text(short, style="color.description.short", end="") + \ row.append( self.rtext(short, "description.short", end="") + \
rich.text.Text(":", style="default", end="") + \ rich.text.Text(":", style="default", end="") + \
rich.text.Text(desc, style="color.description.long", end="") ) self.rtext(desc, "description.long", end="") )
# Strings, but not description. # Strings, but not description.
else: else:
row.append( rich.text.Text(val, style=f"color.{k}") ) row.append( self.rtext(val, f"{k}") )
##### List keys. ##### ##### List keys. #####
elif type(val) == list: elif type(val) == list:
# Tags are a special case. # Tags are a special case.
@ -319,17 +318,17 @@ class stack:
for t in val: for t in val:
# FIXME use Columns if/when it does not expand. # FIXME use Columns if/when it does not expand.
tags += \ tags += \
rich.text.Text(self.tag_icons[0], style="color.tags.ends") + \ self.rtext(self.tag_icons[0], "tags.ends") + \
rich.text.Text(t, style=f"color.{k}") + \ self.rtext(t, f"{k}") + \
rich.text.Text(self.tag_icons[1], style="color.tags.ends") + \ self.rtext(self.tag_icons[1], "tags.ends") + \
" " " "
row.append( tags ) row.append( tags )
# List, but not tags. # List, but not tags.
else: else:
row.append( rich.text.Text(" ".join(val), style=f"color.{k}") ) row.append( self.rtext(" ".join(val), f"{k}") )
##### Other type of keys. ##### ##### Other type of keys. #####
else: else:
row.append( rich.text.Text(str(val), style=f"color.{k}") ) row.append( self.rtext(str(val), f"{k}") )
else: else:
row.append("") row.append("")
table.add_row(*[t for t in row]) table.add_row(*[t for t in row])
@ -368,7 +367,7 @@ class sections:
groups = self.group(tasks) groups = self.group(tasks)
for key in self.order(groups): for key in self.order(groups):
if key in groups: if key in groups:
sections.append( rich.panel.Panel(self.stacker(groups[key]), title = rich.text.Text(str(key).upper(), style=f"color.{key}"), title_align = "left", expand = True)) sections.append( rich.panel.Panel(self.stacker(groups[key]), title = self.rtext(str(key).upper(), f"{key}"), title_align = "left", expand = True))
return rich.console.Group(*sections) return rich.console.Group(*sections)
class Horizontal(Sectioner): class Horizontal(Sectioner):
@ -387,7 +386,7 @@ class sections:
row = [] row = []
for k in keys: for k in keys:
row.append( rich.panel.Panel(self.stacker(groups[k]), title = rich.text.Text(k.upper(), style=f"color.{k}"), title_align = "left", expand = True, border_style="color.title")) row.append( rich.panel.Panel(self.stacker(groups[k]), title = self.rtext(k.upper(), f"{k}"), title_align = "left", expand = True, border_style="color.title"))
table.add_row(*row) table.add_row(*row)
return table return table