#!/usr/bin/env python
# -*- coding: utf-8 -*-

# to be used with i3blocks
# gets the gpu_temp and colors it nicely

import subprocess

command = 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader'
warn_color = "#FF000"
crit_color = "#00ff00"

def coloroutput(value):
    value = int(value)
    if value > 50:
        color = warn_color
    elif value > 40:
        color = crit_color
    else:
        color = "#ffffff"
    """
    add two linebreaks for i3blocks format: 
    full_text \n 
    short_text \n 
    color
    """
    coloredvalue = str(value)+'°C\n\n' + color
    return coloredvalue


def gpu(command):
    #get the gpu temp
    gputemp = subprocess.check_output(command, shell=True)
    #convert the temp into a string for formatting
    gputemp = str(gputemp)
    #get the numbers because the command adds extra characters
    gputemp = gputemp[2:4]
    #add some color...
    gputemp = coloroutput(gputemp)
    return gputemp

print(gpu(command))


