#!/usr/bin/bash
# ABOUTME: Waybar module that outputs the CPU governor as JSON.
# ABOUTME: Referenced by the Waybar custom/cpugov config.

CPU_GOV=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null)

case $CPU_GOV in
  performance)
    CPU_GOV_SHORT=󰓅
    ;;
  balanced)
    CPU_GOV_SHORT=󰾅
    ;;
  powersave)
    CPU_GOV_SHORT=󰌪
    ;;
  userspace)
    CPU_GOV_SHORT=uspace
    ;;
  ondemand)
    CPU_GOV_SHORT=ondmnd
    ;;
  conservative)
    CPU_GOV_SHORT=cons
    ;;
  schedutil)
    CPU_GOV_SHORT=sutil
    ;;
  *)
    CPU_GOV_SHORT="?"
    ;;
esac

CPU_GOV_FULL="${CPU_GOV^}"

jq --compact-output -n \
  --arg text "$CPU_GOV_SHORT" \
  --arg alt "$CPU_GOV_FULL" \
  --arg tooltip "CPU Mode: $CPU_GOV_FULL" \
  --arg class "cpugov" \
  '{text: $text, alt: $alt, tooltip: $tooltip, class: $class}'
