frobarng: Mirroring and more
This commit is contained in:
parent
7d60269b49
commit
6690f3aa0d
|
@ -102,6 +102,23 @@ class Module(ComposableText):
|
|||
def __init__(self, parent: "Side") -> None:
|
||||
super().__init__(parent=parent)
|
||||
self.sections: list[Section] = []
|
||||
self.mirroring: Module | None = None
|
||||
self.mirrors: list[Module] = list()
|
||||
|
||||
def mirror(self, module: "Module") -> None:
|
||||
self.mirroring = module
|
||||
module.mirrors.append(self)
|
||||
|
||||
def getSections(self) -> list[Section]:
|
||||
if self.mirroring:
|
||||
return self.mirroring.sections
|
||||
else:
|
||||
return self.sections
|
||||
|
||||
def updateMarkup(self) -> None:
|
||||
super().updateMarkup()
|
||||
for mirror in self.mirrors:
|
||||
mirror.updateMarkup()
|
||||
|
||||
|
||||
class Alignment(enum.Enum):
|
||||
|
@ -122,7 +139,7 @@ class Side(ComposableText):
|
|||
text = "%{" + self.alignment.value + "}"
|
||||
lastSection: Section | None = None
|
||||
for module in self.modules:
|
||||
for section in module.sections:
|
||||
for section in module.getSections():
|
||||
if section.isHidden():
|
||||
continue
|
||||
if lastSection is None:
|
||||
|
@ -184,15 +201,15 @@ class Bar(ComposableText):
|
|||
self.screens.append(screen)
|
||||
|
||||
async def run(self) -> None:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
cmd = [
|
||||
"lemonbar",
|
||||
"-b",
|
||||
"-a",
|
||||
"64",
|
||||
"-f",
|
||||
"DejaVuSansM Nerd Font:size=10",
|
||||
stdin=asyncio.subprocess.PIPE,
|
||||
)
|
||||
]
|
||||
proc = await asyncio.create_subprocess_exec(*cmd, stdin=asyncio.subprocess.PIPE)
|
||||
|
||||
async def refresher() -> None:
|
||||
assert proc.stdin
|
||||
|
@ -214,18 +231,13 @@ class Bar(ComposableText):
|
|||
provider: "Provider",
|
||||
alignment: Alignment = Alignment.LEFT,
|
||||
screenNum: int | None = None,
|
||||
screenCount: int | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
screenNum: the provider will be added on this screen if set, all otherwise
|
||||
screenCount: the provider will be added if there is this many screens,
|
||||
always otherwise
|
||||
"""
|
||||
modules = list()
|
||||
for s, screen in enumerate(self.screens):
|
||||
if (screenCount is None or len(self.screens) == screenCount) and (
|
||||
screenNum is None or s == screenNum
|
||||
):
|
||||
if screenNum is None or s == screenNum:
|
||||
side = screen.sides[alignment]
|
||||
module = Module(parent=side)
|
||||
side.modules.append(module)
|
||||
|
@ -243,32 +255,77 @@ class Provider:
|
|||
raise NotImplementedError()
|
||||
|
||||
|
||||
class TimeProvider(Provider):
|
||||
class MirrorProvider(Provider):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.module: Module
|
||||
|
||||
async def run(self) -> None:
|
||||
sections = list()
|
||||
for module in self.modules:
|
||||
section = Section(parent=module)
|
||||
module.sections.append(section)
|
||||
sections.append(section)
|
||||
# FIXME Allow for mirror(ed) modules so no need for updaters to handle all
|
||||
self.module = self.modules[0]
|
||||
for module in self.modules[1:]:
|
||||
module.mirror(self.module)
|
||||
|
||||
|
||||
class SingleSectionProvider(MirrorProvider):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.section: Section
|
||||
|
||||
async def run(self) -> None:
|
||||
await super().run()
|
||||
self.section = Section(parent=self.module)
|
||||
self.module.sections.append(self.section)
|
||||
|
||||
|
||||
class StaticProvider(SingleSectionProvider):
|
||||
def __init__(self, text: str) -> None:
|
||||
self.text = text
|
||||
|
||||
async def run(self) -> None:
|
||||
await super().run()
|
||||
self.section.setText(self.text)
|
||||
|
||||
|
||||
class TimeProvider(SingleSectionProvider):
|
||||
async def run(self) -> None:
|
||||
await super().run()
|
||||
|
||||
while True:
|
||||
now = datetime.datetime.now()
|
||||
for s, section in enumerate(sections):
|
||||
# section.setText(now.strftime("%a %y-%m-%d %H:%M:%S.%f"))
|
||||
section.setText("-" * (now.second % 10))
|
||||
# section.setText(now.strftime("%a %y-%m-%d %H:%M:%S.%f"))
|
||||
self.section.setText("-" * (now.second % 10))
|
||||
remaining = 1 - now.microsecond / 1000000
|
||||
await asyncio.sleep(remaining)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
bar = Bar()
|
||||
bar.addProvider(TimeProvider())
|
||||
bar.addProvider(TimeProvider(), screenNum=1)
|
||||
bar.addProvider(TimeProvider(), alignment=Alignment.CENTER, screenCount=2)
|
||||
bar.addProvider(TimeProvider(), alignment=Alignment.CENTER)
|
||||
bar.addProvider(TimeProvider(), alignment=Alignment.RIGHT)
|
||||
dualScreen = len(bar.screens) > 1
|
||||
|
||||
bar.addProvider(StaticProvider(text="i3 workspaces"), alignment=Alignment.LEFT)
|
||||
if dualScreen:
|
||||
bar.addProvider(
|
||||
StaticProvider(text="i3 title"), screenNum=0, alignment=Alignment.CENTER
|
||||
)
|
||||
bar.addProvider(
|
||||
StaticProvider(text="mpris"),
|
||||
screenNum=1 if dualScreen else None,
|
||||
alignment=Alignment.CENTER,
|
||||
)
|
||||
|
||||
bar.addProvider(StaticProvider("C L M T B"), alignment=Alignment.RIGHT)
|
||||
bar.addProvider(
|
||||
StaticProvider("pulse"),
|
||||
screenNum=1 if dualScreen else None,
|
||||
alignment=Alignment.RIGHT,
|
||||
)
|
||||
bar.addProvider(
|
||||
StaticProvider("network"),
|
||||
screenNum=0 if dualScreen else None,
|
||||
alignment=Alignment.RIGHT,
|
||||
)
|
||||
bar.addProvider(TimeProvider(), alignment=Alignment.RIGHT)
|
||||
|
||||
await bar.run()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue