refactor printing

This commit is contained in:
Johann Dreo 2024-02-11 21:53:23 +01:00
commit 66fc4f3b3e

View file

@ -3,6 +3,7 @@
import collections
# import rich
from rich.console import Console
from rich.style import Style
import humanize
import datetime
from enum import IntEnum
@ -28,6 +29,7 @@ class N:
},
-1
]
def get_member(self):
return self.member
@ -37,6 +39,7 @@ class N:
def get_args_list(self):
return self.args_list
class Message:
def __init__(self, notification):
# print("Notification")
@ -53,11 +56,40 @@ class Message:
self.expire_timeout = int(args[7])
self.date = datetime.datetime.now()
self.hdate = humanize.naturaltime(self.date)
self.urgency = {0: "low", 1: "normal", 2: "critical", None: "unknown"}
self.urgencies = {0: "low", 1: "normal", 2: "critical", None: "unknown"}
self.urgency = self.urgencies[self.hints["urgency"]]
self.last_color = "black"
self.color = {
# Urgencies
"low": "105",
"normal": "33",
"critical": "220",
"unknown": "69",
"date": "21",
"summary": "254",
"body": "242",
}
self.style = {"none": "black"}
for k in self.color:
self.style[k] = f"color({self.color[k]})"
def print_segment(self, key, text, console):
console.print("", style=f"{self.last_color} on {self.style[key]}", end="")
# FIXME automated black or white
console.print(text, style=f"black on {self.style[key]}", end="")
self.last_color = f"{self.style[key]}"
def print_on(self, console = Console()):
console.print(f"[color(33)][white on color(33)]{self.hdate}:[bold black on color(33)]{self.app}[color(33) on color(254)][not bold black on color(254)]{self.summary}[color(254) on color(239)][white on color(239)]{self.body}[reset][color(239)]", end="")
hdate = humanize.naturaltime(self.date)
self.print_segment("date", hdate, console)
self.print_segment(self.urgency, self.app, console)
self.print_segment("summary", self.summary, console)
self.print_segment("body", self.body, console)
# console.print(f"[color(21)][white on color(21)]{hdate}[color(21) on color(33)][bold color(232) on color(33)]{self.app}[color(33) on color(254)][not bold color(232) on color(254)]{self.summary}[color(254) on color(239)][white on color(239)]{self.body}[reset][color(239)]", end="")
self.print_segment("none", "", console)
class Broker:
@ -87,8 +119,9 @@ class Broker:
self.print()
def print(self):
self.console.print(end = "\r")
self.console.print(">", end = "\r") # It is necssary to print something before the chariot return.
for msg in self.deck:
self.console.print(" ", end="")
msg.print_on(self.console)