1
0

data display

This commit is contained in:
Deven Blake 2021-06-16 22:22:38 -04:00
parent 2105c0aebf
commit 980be4e945

138
dotfiles-old/bin/data Executable file
View File

@ -0,0 +1,138 @@
#!/bin/sh
set -e
# 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'"
# 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. enabled by default.
# will save negligible time with shells that don't spawn new processes for
# test(1)
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() {
# 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
}
get_cpu_temp() {
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
}
get_memory_usage() {
printf "%b" "$(free | head -n 2 | tail -n 1 | awk '{print $3 " used / " $2 " total"}')"
return
}
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 || %b || %b || %b || %b %b\n" \
"$(date)" \
"BAT: $(get_battery_status)" \
"CPU: $(get_cpu_temp)" \
"PuIP: $PUBLIC_IP" \
"MEM: $(get_memory_usage)" \
""
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" "$(($2/$1))"
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
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
}
if [ -n "$POSIXLY_CORRECT" ]; then
alias fdivide="fdivide_posix"
alias gt="gt_posix"
alias stris="stris_posix"
fi
[ -n "$INTERVAL" ] && stris int "$INTERVAL" \
|| INTERVAL=1 \
&& true
[ -n "$CPU_TEMP_SCALE" ] && stris int "$CPU_TEMP_SCALE" && gt "$CPU_TEMP_SCALE" 0 \
|| CPU_TEMP_SCALE=1 \
&& true
[ -n "$CPU_TEMP_FILES" ] \
|| alias get_cpu_temp="printf '[no temperature files specified]\n'" \
&& true
while true; do
printbar
sleep "$INTERVAL"
done