Skip to content

Instantly share code, notes, and snippets.

@dlozeve
Created January 18, 2019 07:48
Show Gist options
  • Save dlozeve/2528360803bcdb993ffd90ddcb7565d6 to your computer and use it in GitHub Desktop.
Save dlozeve/2528360803bcdb993ffd90ddcb7565d6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This script is a simple wrapper which prefixes each i3status line with custom
# information. It is a python reimplementation of:
# http://code.stapelberg.de/git/i3status/tree/contrib/wrapper.pl
#
# To use it, ensure your ~/.i3status.conf contains this line:
# output_format = "i3bar"
# in the 'general' section.
# Then, in your ~/.i3/config, use:
# status_command i3status | ~/i3status/contrib/wrapper.py
# In the 'bar' section.
#
# In its current version it will display the cpu frequency governor, but you
# are free to change it to display whatever you like, see the comment in the
# source code below.
#
# © 2012 Valentin Haenel <valentin.haenel@gmx.de>
#
# 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
from subprocess import run, PIPE
import re
def get_governor():
""" Get the current governor for cpu0, assuming all CPUs use the same. """
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor') as fp:
return fp.readlines()[0].strip()
def mail_count():
""" Get the state of the mu mailbox. """
unread = run(["mu", "find", "flag:unread"], stdout=PIPE)
unread = len(unread.stdout.splitlines())
if unread > 0:
return {'full_text': f"{unread} new mail",
'name': 'mail',
'color': "#00ff00"}
else:
return None
def spotify():
""" Get the current artist and song name. """
status = run(["playerctl", "-p", "spotify", "status"],
stdout=PIPE).stdout.strip().decode("utf-8")
artist = run(["playerctl", "-p", "spotify", "metadata", "artist"],
stdout=PIPE).stdout.strip().decode("utf-8")
# album = run(["playerctl", "-p", "spotify", "metadata", "album"],
# stdout=PIPE).stdout.strip().decode("utf-8")
title = run(["playerctl", "-p", "spotify", "metadata", "title"],
stdout=PIPE).stdout.strip().decode("utf-8")
if status != "Not available":
return {'full_text': f"[{status}] {artist} — {title}",
'name': 'spotify'}
else:
return None
def volume():
pulse = run(["pactl", "list", "sinks"], stdout=PIPE)
pulse = pulse.stdout.strip().decode("utf-8")
vol = re.search("\\d+%", pulse).group(0)
mute = re.search("Mute: \\w+", pulse).group(0)
if "yes" in mute:
return None
else:
return {'full_text': f"Vol: {vol}", 'name': 'vol'}
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_governor(), 'name': 'gov'})
j.insert(0, mail_count())
j.insert(0, volume())
j.insert(0, spotify())
# and echo back new encoded json
print_line(prefix+json.dumps(j))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment