1
0

thrust into 2022

This commit is contained in:
dtb 2022-08-21 20:23:31 -04:00
parent daca2efe30
commit d99ecf1094

106
bin/data
View File

@ -2,11 +2,6 @@
set -e set -e
# depends on https://git.sr.ht/~trinity/utilities (particularly fdivide(1),
# gt(1), and stris(1))
# there are issues with the POSIX shell implementations included in this script
# and particularly with stris
# on Linux only: # on Linux only:
# these files (separated by newlines) will have the current CPU temperature # these files (separated by newlines) will have the current CPU temperature
# multiplied by $CPU_TEMP_SCALE. this script divides the file contents by scale # multiplied by $CPU_TEMP_SCALE. this script divides the file contents by scale
@ -16,7 +11,7 @@ CPU_TEMP_FILES="/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp2_input
CPU_TEMP_SCALE=1000 CPU_TEMP_SCALE=1000
# format for the date (by default in this script, ISO 8601) # format for the date (by default in this script, ISO 8601)
alias date="date '+%Y-%m-%dT%T'" DATE="date '+%Y-%m-%dT%T'"
DELIMITER=" || " DELIMITER=" || "
@ -24,11 +19,6 @@ DELIMITER=" || "
# not an integer) # not an integer)
INTERVAL=1 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 # this URL will have in plain text the public IP address from which it was
# accessed # accessed
#PUBLIC_IP_FETCH_URL="http://ifconfig.io/" #PUBLIC_IP_FETCH_URL="http://ifconfig.io/"
@ -37,26 +27,6 @@ INTERVAL=1
PUBLIC_IP_FETCH_URL="https://icanhazip.com" PUBLIC_IP_FETCH_URL="https://icanhazip.com"
PUBLIC_IP6_FETCH_URL="$PUBLIC_IP_FETCH_URL" PUBLIC_IP6_FETCH_URL="$PUBLIC_IP_FETCH_URL"
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
elif [ "$(uname)" = "NetBSD" ]; then
printf "%b%%" "$(envstat -s acpibat0:charge \
| sed 1,2d \
| cut -d ':' -f 2 \
| awk '{print $1}' \
| cut -d '.' -f 1)"
return
else
printf "get_battery_status: unsupported OS"
fi
}
get_cpu_temp() { get_cpu_temp() {
if [ "$(uname)" = "Linux" ]; then if [ "$(uname)" = "Linux" ]; then
RETVAL="" RETVAL=""
@ -114,14 +84,13 @@ get_public_ip() {
return return
} }
# slow, so fetch it in advance
PUBLIC_IP="$(get_public_ip)" PUBLIC_IP="$(get_public_ip)"
printbar() { printbar() {
printf "%b" "$(get_current_desktop)" printf "%b" "$(get_current_desktop)"
printf "%b" "$(date)" printf "%b" $("$DATE")
printf "%b" "$DELIMITER" printf "%b" "$DELIMITER"
printf "%b" "BAT: $(get_battery_status)" printf "%b" "BAT: $(battery)"
printf "%b" "$DELIMITER" printf "%b" "$DELIMITER"
printf "%b" "CPU: $(get_cpu_temp)" printf "%b" "CPU: $(get_cpu_temp)"
printf "%b" "$DELIMITER" printf "%b" "$DELIMITER"
@ -132,72 +101,13 @@ printbar() {
return 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 # check to make sure customizeable vars are in proper format
[ -n "$INTERVAL" ] && stris int "$INTERVAL" \ [ -n "$INTERVAL" ] && str isdigit "$INTERVAL" \
|| INTERVAL=1 || INTERVAL=1
[ -n "$CPU_TEMP_SCALE" ] && stris int "$CPU_TEMP_SCALE" \ [ -n "$CPU_TEMP_SCALE" ] && str isdigit "$CPU_TEMP_SCALE" \
&& gt "$CPU_TEMP_SCALE" 0 \ && [ "$CPU_TEMP_SCALE" -gt 0 ] \
|| CPU_TEMP_SCALE=1 || CPU_TEMP_SCALE=1
[ -n "$CPU_TEMP_FILES" ] \ [ -n "$CPU_TEMP_FILES" ] \
@ -208,4 +118,8 @@ stris_posix() {
while true; do while true; do
printbar printbar
sleep "$INTERVAL" sleep "$INTERVAL"
if streq $(date +'%S') 00; then
# every minute
PUBLIC_IP="$(get_public_ip)"
fi
done done