diff --git a/i3/config b/i3/config index e85df85..8ca8322 100644 --- a/i3/config +++ b/i3/config @@ -130,5 +130,5 @@ bindsym $mod+r mode "resize" # Start i3bar to display a workspace bar (plus the system information i3status # finds out, if available) bar { - status_command i3status + status_command "i3status | python ~/.i3wrapper.py" } diff --git a/i3status.conf b/i3status.conf index 9c09c73..0574d29 100644 --- a/i3status.conf +++ b/i3status.conf @@ -3,6 +3,7 @@ general { colors = true interval = 5 + output_format = "i3bar" } order += "volume master" diff --git a/i3wrapper.py b/i3wrapper.py new file mode 100755 index 0000000..7f8a751 --- /dev/null +++ b/i3wrapper.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +import sys +import json + +_BITS = [] + + +def _bit(idx, name): + def wrapper(func): + _BITS.append([idx, name, func]) + return func + return wrapper + + +@_bit(6, 'mem') +def _mem(): + with open('/proc/meminfo') as meminfo_fp: + attrs = {} + for line in meminfo_fp.readlines(): + parts = line.split(':', 2) + attrs[parts[0].strip()] = parts[1].strip() + avail = int(attrs['MemAvailable'].split()[0]) + total = int(attrs['MemTotal'].split()[0]) + used = int(((total - avail) / total) * 100) + color = None + if used > 75: + color = '#ff0000' + if used < 25: + color = '#00ff00' + return 'M:{}%'.format(used), color + + +def _print_line(message): + sys.stdout.write(message + '\n') + sys.stdout.flush() + + +def _read_line(): + try: + line = sys.stdin.readline().strip() + if not line: + sys.exit(3) + return line + except KeyboardInterrupt: + sys.exit() + + +def main(bits=_BITS): + _print_line(_read_line()) + _print_line(_read_line()) + + while True: + line, prefix = _read_line(), '' + if line.startswith(','): + line, prefix = line[1:], ',' + + loaded = json.loads(line) + for idx, name, func in bits: + value, color = func() + record = dict(full_text=str(value), name=name) + if color is not None: + record.update(dict(color=color)) + + loaded.insert(idx, record) + + _print_line(prefix+json.dumps(loaded)) + + +if __name__ == '__main__': + main()