[rc/{sway,i3status}] display screen icon on the bar when nosleep
authorAmin Bandali <bandali@gnu.org>
Mon, 22 Oct 2018 18:46:35 +0000 (14:46 -0400)
committerAmin Bandali <bandali@gnu.org>
Mon, 22 Oct 2018 18:46:35 +0000 (14:46 -0400)
- since i3status doesn't support custom blocks, we have to wrap around
  it to add more info to it.  if colours aren't needed, it could be as
  simple as something like this (from the i3status manual):

          #!/bin/sh
          # shell script to prepend i3status with more stuff

          i3status | while :
          do
                  read line
                  echo "mystuff | $line" || exit 1
          done

  but since i like colours, we need a more sophisticated solution
  which involves dealing with json; and thus the output_format needs
  to be "i3bar".

- `killall -USR1 i3status` forces an update when we toggle nosleep.

rc.org

diff --git a/rc.org b/rc.org
index 51a9c97..83e4494 100644 (file)
--- a/rc.org
+++ b/rc.org
@@ -921,6 +921,7 @@ separator_width=0
 general {
         colors = true
         interval = 5
 general {
         colors = true
         interval = 5
+        output_format = "i3bar"
 }
 
 # order += "ipv6"
 }
 
 # order += "ipv6"
@@ -3985,7 +3986,7 @@ bindsym XF86PowerOff mode "$mode_system"
 # Presentation mode
 #
 
 # Presentation mode
 #
 
-bindsym $mod+$mod2+p exec [ ! -e ~/.nosleep ] && touch ~/.nosleep || rm ~/.nosleep
+bindsym $mod+$mod2+p exec ([ ! -e ~/.nosleep ] && touch ~/.nosleep || rm ~/.nosleep) && killall -USR1 i3status
 
 #
 # Notifications
 
 #
 # Notifications
@@ -4025,7 +4026,7 @@ bar {
         background #323232
         inactive_workspace #32323200 #32323200 #5c5c5c
     }
         background #323232
         inactive_workspace #32323200 #32323200 #5c5c5c
     }
-    status_command i3status
+    status_command i3status | my-i3status.py
 }
 
 # font pango: benis uushi 9
 }
 
 # font pango: benis uushi 9
@@ -5437,6 +5438,83 @@ fi
 printf '%s%%%%%2dh%02dm \n' "$perc" $((secs / 3600)) $((secs % 3600 / 60))
 #+end_src
 
 printf '%s%%%%%2dh%02dm \n' "$perc" $((secs / 3600)) $((secs % 3600 / 60))
 #+end_src
 
+** my-i3status.py
+:PROPERTIES:
+:header-args+: :tangle ~/.local/bin/my-i3status.py :shebang "#!/usr/bin/env python2"
+:END:
+
+#+begin_src python :comments none
+# -*- coding: utf-8 -*-
+
+# This script is a simple wrapper which prefixes each i3status line with custom
+# information. It is based on:
+# https://github.com/i3/i3status/blob/master/contrib/wrapper.py
+#
+# In ~/.i3status.conf, add the following line:
+#     output_format = "i3bar"
+# in the 'general' section.
+# Then, in ~/.config/i3/config or ~/.config/sway/config add:
+#     status_command i3status | my-i3status.py
+# in the 'bar' section.  Make sure my-i3status.py is in $PATH.
+#
+# © 2012 Valentin Haenel <valentin.haenel@gmx.de>
+# © 2018 Amin Bandali <bandali@gnu.org>
+#
+# This program is free software. It comes without any warranty, to the extent
+# permitted by applicable law. You can redistribute it and/or modify it under
+# the terms of the Do What The Fuck You Want To Public License (WTFPL), Version
+# 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
+# details.
+
+import sys
+import json
+import os
+
+def get_nosleep():
+    """ Return true if ~/.nosleep exists. """
+    return os.path.isfile(os.path.expanduser("~/.nosleep"))
+
+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:], ','
+
+        if get_nosleep():
+            j = json.loads(line)
+            # insert information into the start of the json, but could be anywhere
+            j.insert(0, {'full_text' : '🖵 ', 'name' : 'nosleep'})
+            # and echo back new encoded json
+            print_line(prefix+json.dumps(j))
+        else:
+            print_line(prefix+line)
+#+end_src
+
 ** rofi-light
 :PROPERTIES:
 :header-args+: :tangle ~/.local/bin/rofi-light :shebang "#!/bin/bash"
 ** rofi-light
 :PROPERTIES:
 :header-args+: :tangle ~/.local/bin/rofi-light :shebang "#!/bin/bash"