Aaaand colors. On a bar also.

This commit is contained in:
Geoffrey Frogeye 2018-08-22 12:04:55 +02:00
parent 8857158edb
commit 1c7efc4a76
2 changed files with 145 additions and 33 deletions

View file

@ -1,5 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""
Debugging script
"""
import i3ipc import i3ipc
import os import os
import psutil import psutil

View file

@ -4,12 +4,15 @@ import enum
import logging import logging
import threading import threading
import time import time
import subprocess
# TODO Update strategies (periodic, inotify file) # TODO Update strategies (periodic, inotify file)
# TODO Section order (priority system maybe ?) # TODO Section order (priority system maybe ?)
# TODO Allow deletion of Bar, BarGroup and Section for screen changes # TODO Allow deletion of Bar, BarGroup and Section for screen changes
# TODO Optimize to use write() calls instead of string concatenation (writing # TODO Optimize to use write() calls instead of string concatenation (writing
# BarGroup strings should be a good compromise) # BarGroup strings should be a good compromise)
# TODO Use bytes rather than strings
# TODO Use default colors of lemonbar sometimes
class BarGroupType(enum.Enum): class BarGroupType(enum.Enum):
@ -24,18 +27,28 @@ class Bar:
One bar for each screen One bar for each screen
""" """
everyone = set() # Constants
string = "" FONT = "DejaVu Sans Mono for Powerline"
@staticmethod @staticmethod
def init(): def init():
Section.init() Section.init()
# Debug cmd = ['lemonbar', '-f', Bar.FONT, '-b']
Bar() Bar.process = subprocess.Popen(cmd, stdin=subprocess.PIPE)
def __init__(self): # Debug
self.screen = 0 # TODO # Bar(0)
Bar(1)
# Class globals
everyone = set()
string = ""
process = None
def __init__(self, screen):
assert isinstance(screen, int)
self.screen = "%{S" + str(screen) + "}"
self.groups = dict() self.groups = dict()
for groupType in BarGroupType: for groupType in BarGroupType:
@ -65,17 +78,25 @@ class Bar:
def update(self): def update(self):
if self.childsChanged: if self.childsChanged:
self.string = "" self.string = self.screen
self.string += self.groups[BarGroupType.LEFT].string
self.string += self.groups[BarGroupType.RIGHT].string
self.childsChanged = False self.childsChanged = False
@staticmethod @staticmethod
def updateAll(): def updateAll():
Bar.string = ""
for bar in Bar.everyone: for bar in Bar.everyone:
bar.update() bar.update()
Bar.string += bar.string
# Color for empty sections
Bar.string += BarGroup.color(*Section.EMPTY)
Bar.string = "" print(Bar.string)
Bar.process.stdin.write(bytes(Bar.string + '\n', 'utf-8'))
Bar.process.stdin.flush()
class BarGroup: class BarGroup:
@ -94,7 +115,7 @@ class BarGroup:
self.sections = list() self.sections = list()
self.string = '' self.string = ''
self.form = '' self.parts = []
#: One of the sections that had their theme or visibility changed #: One of the sections that had their theme or visibility changed
self.childsThemeChanged = False self.childsThemeChanged = False
@ -108,17 +129,58 @@ class BarGroup:
self.sections.append(section) self.sections.append(section)
section.parents.add(self) section.parents.add(self)
ALIGNS = {BarGroupType.LEFT: "%{l}", BarGroupType.RIGHT: "%{r}"}
@staticmethod
def fgColor(color):
return "%{F" + color + "}"
@staticmethod
def bgColor(color):
return "%{B" + color + "}"
@staticmethod
def color(fg, bg):
return BarGroup.fgColor(fg) + BarGroup.bgColor(bg)
def update(self): def update(self):
if self.childsThemeChanged: if self.childsThemeChanged:
# TODO parts = [BarGroup.ALIGNS[self.groupType]]
self.form = ">".join([sec.curText for sec in self.sections
if sec.visible]) secs = [sec for sec in self.sections if sec.visible]
lenS = len(secs)
for s in range(lenS):
sec = secs[s]
theme = Section.THEMES[sec.theme]
if self.groupType == BarGroupType.LEFT:
oSec = secs[s + 1] if s < lenS - 1 else None
else:
oSec = secs[s - 1] if s > 1 else None
oTheme = Section.THEMES[oSec.theme] \
if oSec is not None else Section.EMPTY
if self.groupType == BarGroupType.LEFT:
if s == 0:
parts.append(BarGroup.bgColor(theme[1]))
parts.append(BarGroup.fgColor(theme[0]))
parts.append(sec)
parts.append(BarGroup.color(theme[1], oTheme[1]) + "")
else:
parts.append(BarGroup.fgColor(theme[1]) + "")
parts.append(BarGroup.color(*theme))
parts.append(sec)
# TODO Concatenate successive strings
self.parts = parts
if self.childsTextChanged or self.childsThemeChanged: if self.childsTextChanged or self.childsThemeChanged:
print("118") self.string = ""
self.string = ">".join([sec.curText for sec in self.sections for part in self.parts:
if sec.visible]) if isinstance(part, str):
print("|{0}|".format(self.string)) self.string += part
elif isinstance(part, Section):
self.string += part.curText
self.parent.childsChanged = True self.parent.childsChanged = True
@ -128,9 +190,8 @@ class BarGroup:
@staticmethod @staticmethod
def updateAll(): def updateAll():
print("130")
for group in BarGroup.everyone: for group in BarGroup.everyone:
group.update() group.update()
Bar.updateAll() Bar.updateAll()
@ -147,21 +208,36 @@ class SectionThread(threading.Thread):
class Section: class Section:
# TODO Update all of that to base16
COLORS = ['#002b36', '#dc322f', '#859900', '#b58900', '#268bd2', '#6c71c4',
'#2aa198', '#93a1a1', '#657b83', '#dc322f', '#859900', '#b58900',
'#268bd2', '#6c71c4', '#2aa198', '#fdf6e3']
FGCOLOR = '#93a1a1'
BGCOLOR = '#002b36'
THEMES = list()
EMPTY = (FGCOLOR, BGCOLOR)
@staticmethod
def init():
for t in range(8, 16):
Section.THEMES.append((Section.COLORS[0], Section.COLORS[t]))
Section.updateThread = SectionThread(daemon=True)
Section.updateThread.start()
#: Sections that do not have their destination size #: Sections that do not have their destination size
sizeChanging = set() sizeChanging = set()
somethingChanged = threading.Event() somethingChanged = threading.Event()
updateThread = None updateThread = None
@staticmethod def __init__(self, theme=0):
def init():
Section.updateThread = SectionThread(daemon=True)
Section.updateThread.start()
def __init__(self):
#: Displayed section #: Displayed section
#: Note: A section can be empty and displayed! #: Note: A section can be empty and displayed!
self.visible = False self.visible = False
self.theme = theme
#: Displayed text #: Displayed text
self.curText = '' self.curText = ''
#: Displayed text size #: Displayed text size
@ -175,6 +251,12 @@ class Section:
#: Groups that have this section #: Groups that have this section
self.parents = set() self.parents = set()
def __str__(self):
return "<{}><{}>{:01d}{}{:02d}/{:02d}" \
.format(self.curText, self.dstText,
self.theme, "+" if self.visible else "-",
self.curSize, self.dstSize)
def informParentsThemeChanged(self): def informParentsThemeChanged(self):
for parent in self.parents: for parent in self.parents:
parent.childsThemeChanged = True parent.childsThemeChanged = True
@ -197,6 +279,13 @@ class Section:
Section.sizeChanging.add(self) Section.sizeChanging.add(self)
Section.somethingChanged.set() Section.somethingChanged.set()
def updateTheme(self, theme):
assert isinstance(theme, int)
assert theme < len(Section.THEMES)
self.theme = theme
self.informParentsThemeChanged()
Section.somethingChanged.set()
def updateVisibility(self, visibility): def updateVisibility(self, visibility):
assert isinstance(visibility, bool) assert isinstance(visibility, bool)
@ -237,7 +326,6 @@ class Section:
for sizeChanging in Section.sizeChanging.copy(): for sizeChanging in Section.sizeChanging.copy():
sizeChanging.update() sizeChanging.update()
# print("{1:3d} |{0}|".format(sizeChanging.curText, sizeChanging.curSize))
BarGroup.updateAll() BarGroup.updateAll()
@ -246,17 +334,37 @@ class Section:
if __name__ == '__main__': if __name__ == '__main__':
Bar.init() Bar.init()
sec = Section() sec = Section(0)
sec1 = Section() sech = Section(1)
sec1 = Section(2)
sec2 = Section(3)
sec2h = Section(4)
sec3 = Section(5)
Bar.addSectionAll(sec, BarGroupType.LEFT) Bar.addSectionAll(sec, BarGroupType.LEFT)
Bar.addSectionAll(sech, BarGroupType.LEFT)
Bar.addSectionAll(sec1, BarGroupType.LEFT) Bar.addSectionAll(sec1, BarGroupType.LEFT)
Bar.addSectionAll(sec2, BarGroupType.RIGHT)
Bar.addSectionAll(sec2h, BarGroupType.RIGHT)
Bar.addSectionAll(sec3, BarGroupType.RIGHT)
time.sleep(1)
sec.updateText("A")
time.sleep(1)
sec.updateText("")
time.sleep(1)
sec.updateText("Hello") sec.updateText("Hello")
# sec1.updateText("world!") sec1.updateText("world!")
time.sleep(2) sec2.updateText("Salut")
sec.updateText("Salut") sec2h.updateText("le")
# sec1.updateText("le monde !") sec3.updateText("monde !")
time.sleep(3)
sech.updateText("the")
sec2h.updateText("")
time.sleep(2) time.sleep(2)
sec.updateText("") sec.updateText("")
# sec1.updateText("") sech.updateText("")
time.sleep(2) sec1.updateText("")
sec2.updateText("")
sec2h.updateText("")
sec3.updateText("")
time.sleep(5)