fix: compute width when displaying
This commit is contained in:
parent
6d58990e1d
commit
2bc42388b5
1 changed files with 40 additions and 20 deletions
|
|
@ -72,6 +72,7 @@ class Color:
|
||||||
|
|
||||||
return R,G,B
|
return R,G,B
|
||||||
|
|
||||||
|
|
||||||
def lum(dC):
|
def lum(dC):
|
||||||
"""Linearize an RGB component."""
|
"""Linearize an RGB component."""
|
||||||
if dC <= 0.03928:
|
if dC <= 0.03928:
|
||||||
|
|
@ -79,6 +80,7 @@ class Color:
|
||||||
else:
|
else:
|
||||||
return pow( (dC + 0.055)/1.055 , 2.4)
|
return pow( (dC + 0.055)/1.055 , 2.4)
|
||||||
|
|
||||||
|
|
||||||
def luminance(R, G, B):
|
def luminance(R, G, B):
|
||||||
"""RGB linear (reljative) luminance."""
|
"""RGB linear (reljative) luminance."""
|
||||||
dR = R / 255
|
dR = R / 255
|
||||||
|
|
@ -91,6 +93,7 @@ class Color:
|
||||||
|
|
||||||
return 0.2126 * lR + 0.7152 * lG + 0.0722 * lB
|
return 0.2126 * lR + 0.7152 * lG + 0.0722 * lB
|
||||||
|
|
||||||
|
|
||||||
def lightness(luminance):
|
def lightness(luminance):
|
||||||
"""Perceived lightness in [0,100] (i.e. [darkest, lightest])."""
|
"""Perceived lightness in [0,100] (i.e. [darkest, lightest])."""
|
||||||
if luminance < 216/24389:
|
if luminance < 216/24389:
|
||||||
|
|
@ -98,6 +101,7 @@ class Color:
|
||||||
else:
|
else:
|
||||||
return pow(luminance, 1/3) * 116 - 16
|
return pow(luminance, 1/3) * 116 - 16
|
||||||
|
|
||||||
|
|
||||||
def ansi_lightness(ansi):
|
def ansi_lightness(ansi):
|
||||||
R,G,B = Color.ansi2rgb(ansi)
|
R,G,B = Color.ansi2rgb(ansi)
|
||||||
return Color.lightness(Color.luminance(R,G,B))
|
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_match_string_non_blocking("eavesdrop=true, interface='org.freedesktop.Notifications', member='Notify'")
|
||||||
bus.add_message_filter(self.receive)
|
bus.add_message_filter(self.receive)
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
mainloop = gi.repository.GLib.MainLoop()
|
mainloop = gi.repository.GLib.MainLoop()
|
||||||
mainloop.run()
|
mainloop.run()
|
||||||
|
|
||||||
|
|
||||||
def receive(self, bus, notification):
|
def receive(self, bus, notification):
|
||||||
# print(notification, flush = True)
|
# print(notification, flush = True)
|
||||||
# print("---------------------------------------------", flush = True)
|
# print("---------------------------------------------", flush = True)
|
||||||
|
|
@ -237,48 +243,62 @@ class Broker:
|
||||||
def width(self, deck):
|
def width(self, deck):
|
||||||
w = 0
|
w = 0
|
||||||
for msg,mlen in deck:
|
for msg,mlen in deck:
|
||||||
w += mlen
|
w += msg.print_on(None)
|
||||||
return w
|
return w
|
||||||
|
|
||||||
|
|
||||||
def print(self):
|
def print(self, bounds = "><"):
|
||||||
# print(len(self.deck),"message in deck")
|
# print(len(self.deck),"message in deck")
|
||||||
console = Console() # Re-instantiate in case the terminal window changed.
|
console = Console() # Re-instantiate in case the terminal window changed.
|
||||||
console.print("\r>", end="")
|
console.print(f"\r{bounds[0]}", end="")
|
||||||
displayed = copy.deepcopy(self.deck)
|
|
||||||
# Filter out messages that would not fit the terminal width.
|
# Filter out messages that would not fit the terminal width.
|
||||||
|
displayed = copy.deepcopy(self.deck)
|
||||||
while self.width(displayed) >= console.size.width:
|
while self.width(displayed) >= console.size.width:
|
||||||
displayed.popleft()
|
displayed.popleft()
|
||||||
|
|
||||||
# print(len(self.displayed),"message displayed")
|
# print(len(self.displayed),"message displayed")
|
||||||
# Print what fits.
|
# Print what fits.
|
||||||
|
width = 0
|
||||||
for msg,mlen in displayed:
|
for msg,mlen in displayed:
|
||||||
msg.print_on(console)
|
width += msg.print_on(console)
|
||||||
|
|
||||||
# Print overlapping spaces until the end.
|
# Print overlapping spaces until the end.
|
||||||
width = sum(mlen for (msg,mlen) in displayed)
|
console.print(" "*(console.size.width-width-len(bounds)), end="") # Minus first and last char.
|
||||||
console.print(" "*(console.size.width-width-2), end="") # Minus first and last char.
|
console.print(f"{bounds[1]}", end="\r")
|
||||||
console.print("<", 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__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == "--test":
|
if len(sys.argv) == 3 and sys.argv[1] == "--test":
|
||||||
from faker import Faker
|
|
||||||
fake = Faker()
|
|
||||||
broker = Broker(max_msg = 100)
|
broker = Broker(max_msg = 100)
|
||||||
console = Console()
|
notifs = test_messages(int(sys.argv[2]))
|
||||||
for i in range(7):
|
for notif in notifs:
|
||||||
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="")
|
|
||||||
broker.receive(None, notif)
|
broker.receive(None, notif)
|
||||||
print("\n", end="")
|
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:
|
else:
|
||||||
broker = Broker()
|
broker = Broker()
|
||||||
broker.run()
|
broker.run()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue