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-22 13:42:44 +01:00
|
|
|
def __separator(self, widget):
|
|
|
|
if not self._theme.separator():
|
|
|
|
return []
|
|
|
|
return [{
|
|
|
|
'full_text': self._theme.separator(),
|
|
|
|
'color': self._theme.bg(widget),
|
|
|
|
'background': self._theme.prev_bg(widget),
|
|
|
|
'separator': False,
|
|
|
|
'separator_block_width': self._theme.separator_block_width(),
|
|
|
|
'border_top': self._theme.border_top(),
|
|
|
|
'border_left': self._theme.border_left(),
|
|
|
|
'border_right': self._theme.border_right(),
|
|
|
|
'border_bottom': self._theme.border_bottom(),
|
|
|
|
}]
|
|
|
|
|
|
|
|
def __main(self, module, widget):
|
|
|
|
return [{
|
|
|
|
'full_text': widget.full_text(),
|
|
|
|
'instance': widget.id(),
|
|
|
|
'name': module.id(),
|
|
|
|
'color': self._theme.fg(widget),
|
|
|
|
'background': self._theme.bg(widget),
|
|
|
|
'separator': self._theme.default_separators(),
|
|
|
|
'separator_block_width': self._theme.separator_block_width(),
|
|
|
|
'border_top': self._theme.border_top(),
|
|
|
|
'border_left': self._theme.border_left(),
|
|
|
|
'border_right': self._theme.border_right(),
|
|
|
|
'border_bottom': self._theme.border_bottom(),
|
|
|
|
}]
|
|
|
|
|
2020-02-09 13:30:40 +01:00
|
|
|
def widgets(self, module):
|
|
|
|
widgets = []
|
|
|
|
for widget in module.widgets():
|
2020-02-22 13:42:44 +01:00
|
|
|
widgets += self.__separator(widget)
|
|
|
|
widgets += self.__main(module, widget)
|
2020-02-16 14:39:10 +01:00
|
|
|
core.event.trigger('next-widget')
|
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:
|
2020-02-22 13:42:44 +01:00
|
|
|
if module in self._status:
|
|
|
|
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
|