Skip to content

Commit

Permalink
Tinkering
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcantrell committed Nov 5, 2024
1 parent 4a0ac40 commit 0703fc4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
26 changes: 19 additions & 7 deletions src/swaystatus/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def load_elements(order, include, settings):
modules = Modules(include)

for key in order:
ids = key.split(":", maxsplit=1)
ids.append(None)

name, instance = ids[:2]
try:
name, instance = key.split(":", maxsplit=1)
except ValueError:
name, instance = key, None

module = modules.find(name)

Expand All @@ -147,15 +147,27 @@ def load_elements(order, include, settings):
def main():
args = parse_args()

configure_logging(level=args.log_level, file=args.log_file, syslog=args.syslog)
configure_logging(
level=args.log_level,
file=args.log_file,
syslog=args.syslog,
)

config = parse_config(args)
logger.debug(f"Using configuration: {config!r}")

elements = load_elements(config["order"], config["include"], config["settings"])
elements = load_elements(
config["order"],
config["include"],
config["settings"],
)

try:
start(elements, config["interval"], config["click_events"])
start(
elements,
config["interval"],
config["click_events"],
)
except Exception:
logger.exception("Unhandled exception in main loop")
sys.exit(1)
9 changes: 4 additions & 5 deletions src/swaystatus/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class BaseElement:
`Element` and be contained in a file named according to how its
corresponding block should be named.
For example, if there is a file named `clock.py` in the modules package, the
element class would have `self.name` set to "clock".
For example, if there is a file named `clock.py` in the modules package, an
instance of the element class would have `self.name` set to "clock".
The module file could have the following code:
from time import strftime
from swaystatus.element import BaseElement
from time import strftime
class Element(BaseElement):
def on_update(self, output):
Expand Down Expand Up @@ -156,8 +156,7 @@ def create_handler(send):
def handler(line):
message = str(line, "utf-8").strip()
send(
f"Output from module {self} "
f"button {button} handler: {message}"
f"Output from module {self} button {button} handler: {message}"
)

return handler
Expand Down
3 changes: 2 additions & 1 deletion src/swaystatus/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ def write_to_stdout():


def start_stdin_thread(updater, elements):
elements_by_key = {key: element for element in elements if (key := element.key())}
elements_by_key = {key: elem for elem in elements if (key := elem.key())}

def read_from_stdin():
logger.info("Listening for click events from stdin...")
try:
assert sys.stdin.readline().strip() == "["

for line in sys.stdin:
click_event = json.loads(line.strip().lstrip(","))
logger.debug(f"Received click event: {click_event!r}")
Expand Down

0 comments on commit 0703fc4

Please sign in to comment.