readline: various tweaks
[~bandali/configs] / .local / bin / my-i3status
CommitLineData
a92a01fe
AB
1#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4# This script is a simple wrapper which prefixes each i3status line with custom
5# information. It is based on:
6# https://github.com/i3/i3status/blob/master/contrib/wrapper.py
7#
8# In ~/.i3status.conf, add the following line:
9# output_format = "i3bar"
10# in the 'general' section.
11# Then, in ~/.config/i3/config or ~/.config/sway/config add:
12# status_command i3status | my-i3status.py
13# in the 'bar' section. Make sure my-i3status.py is in $PATH.
14#
15# © 2012 Valentin Haenel <valentin.haenel@gmx.de>
16# © 2018 Amin Bandali <bandali@gnu.org>
17#
18# This program is free software. It comes without any warranty, to the extent
19# permitted by applicable law. You can redistribute it and/or modify it under
20# the terms of the Do What The Fuck You Want To Public License (WTFPL), Version
21# 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
22# details.
23
24import sys
25import json
26import os
27
28def get_nosleep():
29 """ Return true if ~/.nosleep exists. """
30 return os.path.isfile(os.path.expanduser("~/.nosleep"))
31
32def print_line(message):
33 """ Non-buffered printing to stdout. """
34 sys.stdout.write(message + '\n')
35 sys.stdout.flush()
36
37def read_line():
38 """ Interrupted respecting reader for stdin. """
39 # try reading a line, removing any extra whitespace
40 try:
41 line = sys.stdin.readline().strip()
42 # i3status sends EOF, or an empty line
43 if not line:
44 sys.exit(3)
45 return line
46 # exit on ctrl-c
47 except KeyboardInterrupt:
48 sys.exit()
49
50if __name__ == '__main__':
51 # Skip the first line which contains the version header.
52 print_line(read_line())
53
54 # The second line contains the start of the infinite array.
55 print_line(read_line())
56
57 while True:
58 line, prefix = read_line(), ''
59 # ignore comma at start of lines
60 if line.startswith(','):
61 line, prefix = line[1:], ','
62
63 if get_nosleep():
64 j = json.loads(line)
65 # insert information into the start of the json, but could be anywhere
66 j.insert(0, {'full_text' : '•', 'name' : 'nosleep'})
67 # and echo back new encoded json
68 print_line(prefix+json.dumps(j))
69 else:
70 print_line(prefix+line)