From 2bc42388b5abe2bc9b8933d328c18faa672696d6 Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 16 Feb 2024 18:50:42 +0100 Subject: [PATCH] fix: compute width when displaying --- src/clibard/clibard.py | 60 ++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/clibard/clibard.py b/src/clibard/clibard.py index ddb92aa..2710a7c 100755 --- a/src/clibard/clibard.py +++ b/src/clibard/clibard.py @@ -72,6 +72,7 @@ class Color: return R,G,B + def lum(dC): """Linearize an RGB component.""" if dC <= 0.03928: @@ -79,6 +80,7 @@ class Color: else: return pow( (dC + 0.055)/1.055 , 2.4) + def luminance(R, G, B): """RGB linear (reljative) luminance.""" dR = R / 255 @@ -91,6 +93,7 @@ class Color: return 0.2126 * lR + 0.7152 * lG + 0.0722 * lB + def lightness(luminance): """Perceived lightness in [0,100] (i.e. [darkest, lightest]).""" if luminance < 216/24389: @@ -98,6 +101,7 @@ class Color: else: return pow(luminance, 1/3) * 116 - 16 + def ansi_lightness(ansi): R,G,B = Color.ansi2rgb(ansi) return Color.lightness(Color.luminance(R,G,B)) @@ -215,10 +219,12 @@ class Broker: bus.add_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'") bus.add_message_filter(self.receive) + def run(self): mainloop = gi.repository.GLib.MainLoop() mainloop.run() + def receive(self, bus, notification): # print(notification, flush = True) # print("---------------------------------------------", flush = True) @@ -237,48 +243,62 @@ class Broker: def width(self, deck): w = 0 for msg,mlen in deck: - w += mlen + w += msg.print_on(None) return w - def print(self): + def print(self, bounds = "><"): # print(len(self.deck),"message in deck") console = Console() # Re-instantiate in case the terminal window changed. - console.print("\r>", end="") - displayed = copy.deepcopy(self.deck) + console.print(f"\r{bounds[0]}", end="") + # Filter out messages that would not fit the terminal width. + displayed = copy.deepcopy(self.deck) while self.width(displayed) >= console.size.width: displayed.popleft() # print(len(self.displayed),"message displayed") # Print what fits. + width = 0 for msg,mlen in displayed: - msg.print_on(console) + width += msg.print_on(console) # Print overlapping spaces until the end. - width = sum(mlen for (msg,mlen) in displayed) - console.print(" "*(console.size.width-width-2), end="") # Minus first and last char. - console.print("<", end="\r") + console.print(" "*(console.size.width-width-len(bounds)), end="") # Minus first and last char. + console.print(f"{bounds[1]}", end="\r") + + +def test_messages(nb = 7): + from faker import Faker + import random + fake = Faker() + notifs = [] + for i in range(nb): + lorem_app = fake.name() + lorem_summary = fake.sentence(nb_words = 3, variable_nb_words = True) + lorem_body = fake.sentence(nb_words = 7, variable_nb_words = True) + notifs.append( N(lorem_app, lorem_summary, lorem_body, urgency = random.randint(0,2)) ) + return notifs if __name__ == "__main__": import sys - if len(sys.argv) > 1 and sys.argv[1] == "--test": - from faker import Faker - fake = Faker() + if len(sys.argv) == 3 and sys.argv[1] == "--test": broker = Broker(max_msg = 100) - console = Console() - for i in range(7): - lorem_app = fake.name() - lorem_summary = fake.sentence(nb_words = 3, variable_nb_words = True) - lorem_body = fake.sentence(nb_words = 7, variable_nb_words = True) - notif = N(lorem_app, lorem_summary, lorem_body, urgency = i%3) - # msg = Message(notif) - # msg.print_on(console) - # console.print("\n", end="") + notifs = test_messages(int(sys.argv[2])) + for notif in notifs: broker.receive(None, notif) print("\n", end="") + + elif len(sys.argv) == 3 and sys.argv[1] == "--send": + import os + import time + notifs = test_messages(int(sys.argv[2])) + for notif in notifs: + m = Message(notif) + os.system(f"""notify-send "{m.summary}" "{m.body}" -u {m.urgency}""") + time.sleep(0.1) else: broker = Broker() broker.run()