2020-02-01 21:37:38 +01:00
|
|
|
import sys
|
2020-01-19 13:29:34 +01:00
|
|
|
import json
|
2020-01-26 13:31:20 +01:00
|
|
|
import time
|
2020-01-19 13:29:34 +01:00
|
|
|
|
2020-02-09 13:46:56 +01:00
|
|
|
import core.theme
|
2020-02-16 14:30:45 +01:00
|
|
|
import core.event
|
2020-02-09 13:46:56 +01:00
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
class i3(object):
|
2020-02-09 13:46:56 +01:00
|
|
|
def __init__(self, theme=core.theme.Theme()):
|
2020-02-02 14:18:13 +01:00
|
|
|
self._modules = []
|
2020-02-09 13:25:34 +01:00
|
|
|
self._status = {}
|
2020-02-15 14:05:27 +01:00
|
|
|
self._theme = theme
|
2020-02-16 14:30:45 +01:00
|
|
|
core.event.register('start', self.draw, 'start')
|
|
|
|
core.event.register('update', self.draw, 'statusline')
|
|
|
|
core.event.register('stop', self.draw, 'stop')
|
2020-02-01 21:37:38 +01:00
|
|
|
|
2020-02-02 14:18:13 +01:00
|
|
|
def modules(self, modules=None):
|
|
|
|
if not modules:
|
|
|
|
return self._modules
|
|
|
|
self._modules = modules if isinstance(modules, list) else [ modules ]
|
|
|
|
|
2020-02-08 14:22:43 +01:00
|
|
|
def draw(self, what, args=None):
|
|
|
|
cb = getattr(self, what)
|
|
|
|
data = cb(args) if args else cb()
|
2020-02-01 21:37:38 +01:00
|
|
|
if 'data' in data:
|
|
|
|
sys.stdout.write(json.dumps(data['data']))
|
|
|
|
if 'suffix' in data:
|
|
|
|
sys.stdout.write(data['suffix'])
|
|
|
|
sys.stdout.write('\n')
|
2020-02-02 21:21:24 +01:00
|
|
|
sys.stdout.flush()
|
2020-02-01 21:37:38 +01:00
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
def start(self):
|
2020-02-01 21:37:38 +01:00
|
|
|
return {
|
|
|
|
'data': { 'version': 1, 'click_events': True },
|
|
|
|
'suffix': '\n[',
|
|
|
|
}
|
2020-01-19 13:29:34 +01:00
|
|
|
|
|
|
|
def stop(self):
|
2020-02-01 21:37:38 +01:00
|
|
|
return { 'suffix': '\n]' }
|
|
|
|
|
2020-02-09 13:30:40 +01:00
|
|
|
def widgets(self, module):
|
|
|
|
widgets = []
|
|
|
|
for widget in module.widgets():
|
|
|
|
widgets.append({
|
|
|
|
'full_text': widget.full_text(),
|
|
|
|
'instance': widget.id(),
|
|
|
|
'name': module.id(),
|
2020-02-15 14:05:27 +01:00
|
|
|
'color': self._theme.fg(widget),
|
|
|
|
'background': self._theme.bg(widget),
|
2020-02-16 14:02:21 +01:00
|
|
|
'separator': self._theme.default_separators(),
|
2020-02-09 13:30:40 +01:00
|
|
|
})
|
|
|
|
return widgets
|
|
|
|
|
2020-02-09 13:25:34 +01:00
|
|
|
def update(self, affected_modules=None):
|
2020-02-02 14:18:13 +01:00
|
|
|
for module in self._modules:
|
2020-02-09 13:25:34 +01:00
|
|
|
module.update()
|
2020-02-09 13:30:40 +01:00
|
|
|
self._status[module] = self.widgets(module)
|
2020-02-09 13:25:34 +01:00
|
|
|
|
|
|
|
def statusline(self):
|
|
|
|
widgets = []
|
|
|
|
for module in self._modules:
|
|
|
|
widgets += self._status[module]
|
2020-02-01 21:37:38 +01:00
|
|
|
return {
|
2020-02-09 13:25:34 +01:00
|
|
|
'data': widgets,
|
2020-02-01 21:37:38 +01:00
|
|
|
'suffix': ','
|
|
|
|
}
|
2020-01-19 13:29:34 +01:00
|
|
|
|
2020-01-26 13:31:20 +01:00
|
|
|
def wait(self, interval):
|
|
|
|
time.sleep(interval)
|
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|