lemonbar: Move all lemonbar parsing to units.

Remove i3 workspaces dependency on lemonbar. Implement sorting and
alignment of modules
This commit is contained in:
kuben
2019-08-27 22:25:04 +02:00
parent c12d49f433
commit 0868cf101e
5 changed files with 419 additions and 322 deletions

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python
#
# Print i3 workspaces on every change.
#
# Format:
@@ -20,7 +19,7 @@ import time
from subprocess import call
import i3ipc
import i3_lemonbar_common as common
print_stdout = False
class State(object):
# workspace states
@@ -47,24 +46,20 @@ class State(object):
class i3ws(object):
ws_format = '%s%s '
end_format = 'WSP%s'
state = State()
outputs = []
focused_window = None
running = True
fifo = None
# Callback functions have argument i3ws
change_callbacks = []
focus_callbacks = []
def __init__(self, logger, state=None, fifo_file=None):
def __init__(self, logger, state=None):
self.state = State()
self.outputs = []
self.workspaces = []
self.focused_window = None
self.running = True
if state:
self.state = state
if fifo_file:
self.fifo = open(fifo_file, 'w')
self.logger = logger
common.add_callbacks(self)
# Callback functions have argument i3ws
self.change_callbacks = []
self.focus_callbacks = []
def work(self):
# While loop to restart connection as long as there is an i3 ipc socket
@@ -79,8 +74,11 @@ class i3ws(object):
self.conn = i3ipc.Connection()
# Run call backs once
#self.change(self.conn, None)
#self.win_focused(self.conn, None)
for cb in self.change_callbacks:
cb(self)
for cb in self.focus_callbacks:
cb(self)
self.conn.on('workspace::focus' , self.change)
self.conn.on('workspace::init' , self.change)
@@ -109,8 +107,8 @@ class i3ws(object):
fmt_outputs.append(output.name)
self.display(':'.join(fmt_outputs))
workspaces = i3.get_workspaces()
text = self.format(workspaces, self.outputs)
self.workspaces = i3.get_workspaces()
text = self.format(self.workspaces, self.outputs)
self.display(text)
# Callbacks
@@ -129,6 +127,7 @@ class i3ws(object):
def format(self, workspaces, outputs):
# Formats the text according to the workspace data given.
# Only important when running in free standing mode
out = ''
for workspace in workspaces:
output = None
@@ -145,10 +144,8 @@ class i3ws(object):
return self.end_format % out
def display(self, text):
if self.fifo is not None:
self.fifo.write(text + '\n')
self.fifo.flush()
else:
global print_stdout
if print_stdout:
# Displays the text in stout
print(text)
sys.stdout.flush()
@@ -157,11 +154,6 @@ class i3ws(object):
self.logger.debug('Quitting i3 workspace script')
self.running = False
self.conn.main_quit()
if self.fifo is not None:
try:
self.fifo.close()
except BrokenPipeError:
pass
def handle_exit(signal, frame):
global ws
@@ -171,6 +163,7 @@ def handle_exit(signal, frame):
if __name__ == '__main__':
# Run as stand-alone
print_stdout = True
# Setup logger to stdout with debug log level
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')