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-04-05 10:53:17 +02:00
|
|
|
def dump_json(obj):
|
|
|
|
return obj.__dict__
|
|
|
|
|
|
|
|
class block(object):
|
|
|
|
__COMMON_THEME_FIELDS = [
|
|
|
|
'separator', 'separator_block_width',
|
|
|
|
'border_top', 'border_left', 'border_right', 'border_bottom',
|
|
|
|
'pango', 'fg', 'bg'
|
|
|
|
]
|
|
|
|
def __init__(self, theme, module, widget):
|
|
|
|
self.__attributes = {}
|
|
|
|
for key in self.__COMMON_THEME_FIELDS:
|
|
|
|
tmp = theme.get(key, widget)
|
|
|
|
if tmp:
|
|
|
|
self.__attributes[key] = tmp
|
|
|
|
|
|
|
|
self.__attributes['name'] = module.id
|
|
|
|
self.__attributes['instance'] = widget.id
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
self.__attributes[key] = value
|
|
|
|
|
|
|
|
def __dict__(self):
|
|
|
|
return {}
|
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
class i3(object):
|
2020-03-15 14:02:48 +01:00
|
|
|
def __init__(self, theme=core.theme.Theme(), config=core.config.Config([])):
|
2020-04-04 13:57:42 +02:00
|
|
|
self.__modules = []
|
2020-04-05 10:53:17 +02:00
|
|
|
self.__content = {}
|
2020-04-04 13:57:42 +02:00
|
|
|
self.__theme = theme
|
|
|
|
self.__config = config
|
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-03-01 14:08:16 +01:00
|
|
|
def theme(self, new_theme=None):
|
|
|
|
if new_theme:
|
2020-04-04 13:57:42 +02:00
|
|
|
self.__theme = new_theme
|
|
|
|
return self.__theme
|
2020-03-01 14:08:16 +01:00
|
|
|
|
2020-02-02 14:18:13 +01:00
|
|
|
def modules(self, modules=None):
|
|
|
|
if not modules:
|
2020-04-04 13:57:42 +02:00
|
|
|
return self.__modules
|
|
|
|
self.__modules = modules if isinstance(modules, list) else [ modules ]
|
2020-02-02 14:18:13 +01:00
|
|
|
|
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-04-05 10:53:17 +02:00
|
|
|
if 'blocks' in data:
|
|
|
|
sys.stdout.write(json.dumps(data['blocks'], default=dump_json))
|
2020-02-01 21:37:38 +01:00
|
|
|
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 {
|
2020-04-05 10:53:17 +02:00
|
|
|
'blocks': { 'version': 1, 'click_events': True },
|
2020-02-01 21:37:38 +01:00
|
|
|
'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-24 14:54:11 +01:00
|
|
|
def __pad(self, module, widget, full_text):
|
2020-04-04 13:57:42 +02:00
|
|
|
padding = self.__theme.padding()
|
2020-02-24 14:54:11 +01:00
|
|
|
if not full_text: return padding
|
|
|
|
return '{}{}{}'.format(padding, full_text, padding)
|
|
|
|
|
|
|
|
def __decorate(self, module, widget, full_text):
|
2020-03-29 14:13:21 +02:00
|
|
|
if full_text is None: return None
|
2020-02-24 14:54:11 +01:00
|
|
|
return '{}{}{}'.format(
|
2020-04-04 13:57:42 +02:00
|
|
|
self.__pad(module, widget, self.__theme.prefix(widget)),
|
2020-02-24 14:54:11 +01:00
|
|
|
full_text,
|
2020-04-04 13:57:42 +02:00
|
|
|
self.__pad(module, widget, self.__theme.suffix(widget))
|
2020-02-24 14:54:11 +01:00
|
|
|
)
|
|
|
|
|
2020-04-05 10:53:17 +02:00
|
|
|
def __separator_block(self, module, widget):
|
|
|
|
blk = block(self.__theme, module, widget)
|
|
|
|
blk.set('_decorator', True)
|
|
|
|
return blk
|
|
|
|
|
|
|
|
def __content_block(self, module, widget):
|
|
|
|
text = self.__content[widget]
|
|
|
|
blk = block(self.__theme, module, widget)
|
|
|
|
blk.set('min_width', self.__decorate(module, widget, widget.get('theme.minwidth')))
|
|
|
|
blk.set('full_text', self.__decorate(module, widget, text))
|
|
|
|
if self.__config.debug():
|
|
|
|
blk.set('__state', ', '.join(module.state(widget)))
|
|
|
|
return blk
|
|
|
|
|
|
|
|
def blocks(self, module):
|
|
|
|
blocks = []
|
2020-02-09 13:30:40 +01:00
|
|
|
for widget in module.widgets():
|
2020-04-04 13:57:42 +02:00
|
|
|
if widget.module() and self.__config.autohide(widget.module().name()):
|
2020-03-15 14:01:09 +01:00
|
|
|
if not any(state in widget.state() for state in [ 'warning', 'critical']):
|
|
|
|
continue
|
2020-04-05 10:53:17 +02:00
|
|
|
blocks.append(self.__separator_block(module, widget))
|
|
|
|
blocks.append(self.__content_block(module, widget))
|
2020-02-16 14:39:10 +01:00
|
|
|
core.event.trigger('next-widget')
|
2020-04-05 10:53:17 +02:00
|
|
|
return blocks
|
2020-02-09 13:30:40 +01:00
|
|
|
|
2020-02-09 13:25:34 +01:00
|
|
|
def update(self, affected_modules=None):
|
2020-03-23 15:32:06 +01:00
|
|
|
now = time.time()
|
2020-04-04 13:57:42 +02:00
|
|
|
for module in self.__modules:
|
2020-03-29 14:43:04 +02:00
|
|
|
if affected_modules and not module.id in affected_modules:
|
2020-02-23 21:13:49 +01:00
|
|
|
continue
|
2020-03-23 15:32:06 +01:00
|
|
|
if not affected_modules and module.next_update:
|
|
|
|
if now < module.next_update:
|
|
|
|
continue
|
2020-03-01 14:08:16 +01:00
|
|
|
module.update_wrapper()
|
2020-04-04 13:57:42 +02:00
|
|
|
module.next_update = now + float(module.parameter('interval', self.__config.interval()))
|
2020-02-24 15:05:58 +01:00
|
|
|
for widget in module.widgets():
|
2020-04-05 10:53:17 +02:00
|
|
|
self.__content[widget] = widget.full_text()
|
2020-02-09 13:25:34 +01:00
|
|
|
|
|
|
|
def statusline(self):
|
2020-04-05 10:53:17 +02:00
|
|
|
blocks = []
|
2020-04-04 13:57:42 +02:00
|
|
|
for module in self.__modules:
|
2020-04-05 10:53:17 +02:00
|
|
|
blocks.extend(self.blocks(module))
|
2020-02-01 21:37:38 +01:00
|
|
|
return {
|
2020-04-05 10:53:17 +02:00
|
|
|
'blocks': blocks,
|
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
|