#!/usr/bin/env python import json import subprocess import sys def get_focused_win_title(): """ Get the current focused window title. """ return subprocess.check_output("~/.i3/get_focused_win_title.sh", shell=True).strip('\n') def print_line(message): """ Non-buffered printing to stdout. """ sys.stdout.write(message + '\n') sys.stdout.flush() def read_line(): """ Interrupted respecting reader for stdin. """ # try reading a line, removing any extra whitespace try: line = sys.stdin.readline().strip() # i3status sends EOF, or an empty line if not line: sys.exit(3) return line # exit on ctrl-c except KeyboardInterrupt: sys.exit() if __name__ == '__main__': # Skip the first line which contains the version header. print_line(read_line()) # The second line contains the start of the infinite array. print_line(read_line()) while True: line, prefix = read_line(), '' # ignore comma at start of lines if line.startswith(','): line, prefix = line[1:], ',' j = json.loads(line) # insert information into the start of the json, but could be anywhere # CHANGE THIS LINE TO INSERT SOMETHING ELSE j.insert(0, {'full_text' : '%s' % get_focused_win_title(), 'name' : 'wtitle', 'color' : '#cb4b16'}) # and echo back new encoded json print_line(prefix+json.dumps(j))