#!/bin/sh set -e # depends on https://git.sr.ht/~trinity/utilities (particularly fdivide(1), # gt(1), and stris(1)) unless POSIXLY_CORRECT is set (not tested) # these files (separated by newlines) will have the current CPU temperature # multiplied by $CPU_TEMP_SCALE. this script divides the file contents by scale # and displays that temperature (assumed to be in Celsius units) CPU_TEMP_FILES="/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp2_input /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp3_input" CPU_TEMP_SCALE=1000 # format for the date (by default in this script, ISO 8601) alias date="date '+%Y-%m-%dT%T'" DELIMITER=" || " # how fast to refresh the display (in seconds, will be substituted with 1 if # not an integer) INTERVAL=1 # enable if you have issues with missing `stris` etc. will save negligible time # with shells that don't spawn new processes for test(1) # fdivide_posix() may not be fully working yet, so it's not enabled by default #POSIXLY_CORRECT=1 # this URL will have in plain text the public IP address from which it was # accessed PUBLIC_IP_FETCH_URL="http://ifconfig.io/" get_battery_status() { if [ "$(uname)" = "Linux" ]; then # this is a dirty hack. # acpi | awk '{print $4}' will print the battery percentage, # and s/,$// strips the trailing comma. # acpi -a will just print the power adapter status. printf "%b" "$(acpi | awk '{print $4}' | sed 's/,$//') ($(acpi -a))" return fi } get_cpu_temp() { if [ "$(uname)" = "Linux" ]; then RETVAL="" # iterate over files, adjust, print out "%dC " for file in $CPU_TEMP_FILES; do RETVAL="$RETVAL$(fdivide $(cat $file) $CPU_TEMP_SCALE)C " done # in-line python (bet you've never seen that before) that # strips off trailing whitespace. could be done with sed but # this was more fun printf "%b" "$RETVAL" \ | python -c "from sys import stdin; print(stdin.read().rstrip() + '\n')" return elif [ "$(uname)" = "NetBSD" ]; then printf "%bC" "$(envstat -s coretemp0:'cpu0 temperature' \ | sed 1,2d \ | cut -d ':' -f 2 \ | awk '{print $1}' \ | cut -d '.' -f 1)" fi } get_current_desktop() { case "$WM" in (bspwm) printf "[%b] " "$(bspc query -D -d focused --names)" ;; esac } get_memory_usage() { if [ "$(uname)" = "Linux" ]; then printf "%b" \ "$(free | head -n 2 | tail -n 1 \ | awk '{print $3 " used / " $2 " total"}')" return fi } get_public_ip() { curl "$PUBLIC_IP_FETCH_URL" --no-progress-meter 2>/dev/null \ || printf "[error fetching IP]\n" return } # slow, so fetch it in advance PUBLIC_IP="$(get_public_ip)" printbar() { printf "%b" "$(get_current_desktop)" printf "%b" "$(date)" #printf "%b" "$DELIMITER" #printf "%b" "BAT: $(get_battery_status)" printf "%b" "$DELIMITER" printf "%b" "CPU: $(get_cpu_temp)" printf "%b" "$DELIMITER" printf "%b" "PuIP: $PUBLIC_IP" #printf "%b" "$DELIMITER" #printf "%b" "MEM: $(get_memory_usage)" printf "\n" return } ############################################################################### # the following is for error prevention and doesn't need to be changed # ############################################################################### # POSIX shell version of fdivide fdivide_posix() { [ -n "$2" ] && [ -n "$1" ] && [ -z "$3" ] \ || return 1 printf "%b\n" "$(($1/$2))" return } # [ -gt ] with better syntax gt_posix() { while [ -n "$2" ]; do [ "$1" -gt "$2" ] \ || return 1 shift 1 done return 0 } # limited POSIX shell version of stris # BROKEN REGEX. MUSTFIX. stris_posix() { [ -n "$2" ] || return 1 case "$1" in (int) # https://stackoverflow.com/questions/2210349/test-whether-string-is-a-valid-integer printf "$2" | grep '^-?[0-9]+$' >/dev/null \ && return 0 \ || return 1 ;; (uint) printf "$2" | grep '^[0-9]+$' >/dev/null \ && return 0 \ || return 1 ;; (*) return 1 ;; esac return } # check to make sure we have non-standard utilities [ -n "$POSIXLY_CORRECT" ] || ! which fdivide >/dev/null 2>&1 \ && alias fdivide="fdivide_posix" \ || true [ -n "$POSIXLY_CORRECT" ] || ! which gt >/dev/null 2>&1 \ && alias gt="gt_posix" \ || true [ -n "$POSIXLY_CORRECT" ] || ! which stris >/dev/null 2>&1 \ && alias stris="stris_posix" \ || true # check to make sure customizeable vars are in proper format [ -n "$INTERVAL" ] && stris int "$INTERVAL" \ || INTERVAL=1 [ -n "$CPU_TEMP_SCALE" ] && stris int "$CPU_TEMP_SCALE" \ && gt "$CPU_TEMP_SCALE" 0 \ || CPU_TEMP_SCALE=1 [ -n "$CPU_TEMP_FILES" ] \ || alias get_cpu_temp="printf '[no temperature files specified]\n'" # main loop while true; do printbar sleep "$INTERVAL" done