Skip to content

Instantly share code, notes, and snippets.

@aleozlx
Last active September 16, 2023 10:36
Show Gist options
  • Save aleozlx/781dd2e79661c82aa080d99e67a703b6 to your computer and use it in GitHub Desktop.
Save aleozlx/781dd2e79661c82aa080d99e67a703b6 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))
@BurntRanch
Copy link

BurntRanch commented Sep 16, 2023

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:

#!/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 | path/to/script/wrapper.py
# In the 'bar' section.

import sys, subprocess
import json

def gpu_info():
    return subprocess.getoutput('nvidia-smi --query-gpu=gpu_name,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
        csvinfo = gpu_info().strip().split(', ')
#        full_text = "GPU:" + csvinfo[0] + " | USAGE: " + csvinfo[1] + " | MEM: " + csvinfo[2] + " | T: " + csvinfo[3] + " "
        j = [{
#            'full_text': gpu_info().strip().replace(',', ' |') + ' °C',
            'full_text': "GPU: " + csvinfo[0],
            'name': 'gpu',
            'color': '#3AFF85'
        },{
	    'full_text': "USAGE: " + csvinfo[1],
            'name': 'gpu_usage',
            'color': '#3AFF85',
        },{
            'full_text': "MEM: " + csvinfo[2],
            'name': 'gpu_memory',
            'color': '#3AFF85',
        },{
            'full_text': "T: " + csvinfo[3] + " °C",
            'name': 'gpu_temperature',
            'color': '#3AFF85',
        }] + j
        # 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