Skip to content

Instantly share code, notes, and snippets.

@atztao
Forked from aleozlx/i3status-gpu.py
Created April 14, 2021 03:31
Show Gist options
  • Select an option

  • Save atztao/8a7273ba3366b8f26df57754e388c4b1 to your computer and use it in GitHub Desktop.

Select an option

Save atztao/8a7273ba3366b8f26df57754e388c4b1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ref: https://github.com/i3/i3status/blob/master/contrib/wrapper.py
#
# 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.
import sys, subprocess
import json
def gpu_info():
return subprocess.getoutput('nvidia-smi --query-gpu=gpu_name,pstate,utilization.gpu,utilization.memory,temperature.gpu --format=csv,noheader').strip()
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': gpu_info().strip().replace(',', '') + ' °C',
'name': 'gpu',
'color': '#3A85FF'
})
# 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