Skip to content

Instantly share code, notes, and snippets.

@martianlantern
Created October 17, 2025 08:39
Show Gist options
  • Select an option

  • Save martianlantern/432dc929507ab63d23e7868e57743ddb to your computer and use it in GitHub Desktop.

Select an option

Save martianlantern/432dc929507ab63d23e7868e57743ddb to your computer and use it in GitHub Desktop.
function parse_git_dirty {
[[ $(git status --porcelain 2> /dev/null) ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/(\1$(parse_git_dirty)) /"
}
function parse_git_stat() {
local o files ins dels
o=$(git diff --shortstat HEAD 2>/dev/null) || return
[[ -z $o ]] && return
files=$(grep -oE '[0-9]+ files? changed' <<<"$o" | grep -oE '^[0-9]+')
ins=$(grep -oE '[0-9]+ insertions?\(\+\)' <<<"$o" | grep -oE '^[0-9]+'); [[ -z $ins ]] && ins=0
dels=$(grep -oE '[0-9]+ deletions?\(-\)' <<<"$o" | grep -oE '^[0-9]+'); [[ -z $dels ]] && dels=0
adds=$(git status --porcelain . 2>/dev/null | grep -E '^(A|\?\?) ' | wc -l | tr -d ' ')
local SO=$'\001' SC=$'\002'
local G="${SO}"$'\e[1;32m'"${SC}"
local RD="${SO}"$'\e[31m'"${SC}"
local GR="${SO}"$'\e[90m'"${SC}"
local R="${SO}"$'\e[0m'"${SC}"
echo " [${G}+${ins}${R} ${RD}-${dels}${R}] ${GR}${files:-0} changes ${adds:-0} added${R}"
}
function temp_segment() {
local SO=$'\001' SC=$'\002'
local R="${SO}"$'\e[0m'"${SC}"
local G="${SO}"$'\e[32m'"${SC}"
local Y="${SO}"$'\e[33m'"${SC}"
local RD="${SO}"$'\e[31m'"${SC}"
local CPU_CUR= CPU_MAX=100 GPU_CUR= GPU_MAX=90
# CPU
local d f
for d in /sys/class/hwmon/hwmon*; do
for f in "$d"/temp*_label; do
[[ -r $f ]] || continue
if grep -q "Package id 0" "$f"; then
[[ -r ${f/_label/_input} ]] && CPU_CUR=$(( $(<${f/_label/_input}) / 1000 ))
[[ -r ${f/_label/_crit} ]] && CPU_MAX=$(( $(<${f/_label/_crit}) / 1000 ))
break 2
fi
done
done
[[ -z $CPU_CUR ]] && { f=$(ls /sys/class/hwmon/hwmon*/temp*_input 2>/dev/null | head -n1); [[ $f ]] && CPU_CUR=$(( $(<"$f")/1000 )); }
[[ -z $CPU_MAX ]] && { f=$(ls /sys/class/hwmon/hwmon*/temp*_crit 2>/dev/null | head -n1); [[ $f ]] && CPU_MAX=$(( $(<"$f")/1000 )); }
: "${CPU_MAX:=100}"
# GPU
if command -v nvidia-smi &>/dev/null; then
GPU_CUR=$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits 2>/dev/null | head -n1)
local sd; sd=$(nvidia-smi -q -d TEMPERATURE 2>/dev/null | grep -m1 'Slowdown Temp' | tr -cd '0-9')
[[ $sd ]] && GPU_MAX=$sd
fi
# colors
local ccol="$R" gcol="$R" CG=$((CPU_MAX*70/100)) CY=$((CPU_MAX*85/100)) GG=$((GPU_MAX*70/100)) GY=$((GPU_MAX*85/100))
[[ $CPU_CUR ]] && { ((CPU_CUR>=CY)) && ccol="$RD" || { ((CPU_CUR>=CG)) && ccol="$Y" || ccol="$G"; }; }
[[ $GPU_CUR ]] && { ((GPU_CUR>=GY)) && gcol="$RD" || { ((GPU_CUR>=GG)) && gcol="$Y" || gcol="$G"; }; }
printf "[%sCPU:%s°C%s %sGPU:%s°C%s]" "$ccol" "${CPU_CUR:--}" "$R" "$gcol" "${GPU_CUR:--}" "$R"
}
export PS1="\n\t \$(temp_segment) \[\033[32m\]\w\[\033[0m\]\$(parse_git_stat)\n\[\033[33m\]\$(parse_git_branch)\[\033[00m\]$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment