[core/output] Rewrite to hide sys.stout
Add generic "draw()" method that redirects internally to the actual calls. These can now produce JSON, which is nicer because: 1. Easier to use during testing 2. More flexible 3. Centralizes printing (somewhat) Still, the "suffix" concept isn't really nice, but so far, I have no better approach.
This commit is contained in:
parent
b7ca5eb3a5
commit
66537fbe05
4 changed files with 44 additions and 20 deletions
|
@ -1,18 +1,42 @@
|
|||
import sys
|
||||
import json
|
||||
import time
|
||||
|
||||
class i3(object):
|
||||
def __init__(self):
|
||||
self.clear()
|
||||
|
||||
def draw(self, what):
|
||||
data = getattr(self, what)()
|
||||
if 'data' in data:
|
||||
sys.stdout.write(json.dumps(data['data']))
|
||||
if 'suffix' in data:
|
||||
sys.stdout.write(data['suffix'])
|
||||
sys.stdout.write('\n')
|
||||
|
||||
def start(self):
|
||||
return '{}\n'.format(json.dumps({ 'version': 1, 'click_events': True }))
|
||||
return {
|
||||
'data': { 'version': 1, 'click_events': True },
|
||||
'suffix': '\n[',
|
||||
}
|
||||
|
||||
def stop(self):
|
||||
return ']\n'
|
||||
return { 'suffix': '\n]' }
|
||||
|
||||
def begin_status_line(self):
|
||||
return '['
|
||||
def clear(self):
|
||||
self._statusline = []
|
||||
|
||||
def end_status_line(self):
|
||||
return '],\n'
|
||||
def append(self, module):
|
||||
for widget in module.widgets():
|
||||
self._statusline.append({
|
||||
'full_text': widget.full_text()
|
||||
})
|
||||
|
||||
def statusline(self):
|
||||
return {
|
||||
'data': self._statusline,
|
||||
'suffix': ','
|
||||
}
|
||||
|
||||
def wait(self, interval):
|
||||
time.sleep(interval)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue