Last active
September 16, 2023 10:36
-
-
Save aleozlx/781dd2e79661c82aa080d99e67a703b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
You could make this more efficient and nicer by replacing the bad single-text prompt with multiple prompts that are seperated by i3status and look more natural.
Code: