1
0
This commit is contained in:
dtb
2023-11-19 20:18:20 -07:00
parent 67b50e9133
commit 407ea45346
15 changed files with 5 additions and 0 deletions

5
niceties/README.md Normal file
View File

@@ -0,0 +1,5 @@
# niceties
These are scripts that have been relegated to their own folder for excessive
use of non-POSIX programs or being too simple or having too little use to
justify their having their own folder.

19
niceties/battery Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/sh
subprogram="$(command -v "$0")"
if ! str isvalue "$subprogram" && ! test -e "$0"; then
printf "%s: can't find myself!\n" "$0"
exit 70 # sysexits(3) EX_SOFTWARE
else
subprogram="$0"
fi
# battery -> battery.linux battery.netbsd etc
subprogram="$0"."$(uname | lowercase)"
if ! command -v "$subprogram" >/dev/null 2>&1 && ! test -e "$subprogram"; then
printf "%s: unsupported system\n" "$(uname)" 1>&2
exit 70 # sysexits(3) EX_SOFTWARE
fi
"$subprogram" || exit 70 # sysexits(3) EX_SOFTWARE

6
niceties/battery.linux Executable file
View File

@@ -0,0 +1,6 @@
#!/bin/sh
# this is a dirty hack.
# acpi | awk '{print $4}' will print the battery percentage,
# and s/,$// strips the trailing comma.
acpi | awk '{print $4}' | sed 's/,$//'

7
niceties/battery.netbsd Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
envstat -s acpibat0:charge \
| sed 1,2d \
| cut -d ':' -f 2 \
| awk '{print $1}' \
| cut -d '.' -f 1

33
niceties/batterymonitor Executable file
View File

@@ -0,0 +1,33 @@
#!/bin/sh
POLLING_FREQUENCY=1
# ISO 8601
alias date="date '+%Y-%m-%dT%T'"
emit() {
printf "[%s] " "$(date)"
echo "$@"
}
! battery >/dev/null 2>&1 \
&& printf "Unable to get battery status on this system.\n" 1>&2 \
&& exit 1 \
|| true
current_level=$(battery)
previous_level=$current_level
emit "$(printf "Current level: %d%%" "$current_level")"
while true; do
current_level=$(battery)
if ! streq $current_level $previous_level; then
[ $current_level -lt $previous_level ] \
&& emit "$(printf "Discharged: %d%% -> %d%%\n" $previous_level $current_level )" \
|| emit "$(printf " Charged: %d%% -> %d%%\n" $previous_level $current_level )"
previous_level=$current_level
fi
sleep $POLLING_FREQUENCY
done

108
niceties/data Executable file
View File

@@ -0,0 +1,108 @@
#!/bin/sh
set -e
# on Linux only:
# 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)
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
get_cpu_temp() {
if streq "$(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 streq "$(uname)" "NetBSD"; then
printf "%bC" "$(envstat -s coretemp0:'cpu0 temperature' \
| sed 1,2d \
| cut -d ':' -f 2 \
| awk '{print $1}' \
| cut -d '.' -f 1)"
return
else
printf "get_cpu_temp: unsupported OS"
fi
}
get_current_desktop() {
LASTDESKTOP="$DESKTOP"
case "$WM" in
bspwm)
DESKTOP="$(bspc query -D -d focused --names 2>/dev/null)" ;;
esac || DESKTOP=""
printf "[%s] " "$DESKTOP"
if str isvalue "$LASTDESKTOP" && ! str isvalue "$DESKTOP"; then
# X is kill
exit 0
fi
}
get_memory_usage() {
if streq "$(uname)" "Linux"; then
printf "%b" \
"$(free | head -n 2 | tail -n 1 \
| awk '{print $3 " used / " $2 " total"}')"
return
fi
}
PUBLIC_IP="$(ifpublic)"
printbar() {
printf "%b" "$(get_current_desktop)"
printf "%b" $($DATE)
printf "%b" "$DELIMITER"
printf "%b" "BAT: $(battery)"
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
}
# check to make sure customizeable vars are in proper format
str isdigit "$INTERVAL" \
|| INTERVAL=1
str isdigit "$CPU_TEMP_SCALE" \
&& [ "$CPU_TEMP_SCALE" -gt 0 ] \
|| CPU_TEMP_SCALE=1
str isvalue "$CPU_TEMP_FILES" \
|| alias get_cpu_temp="printf '[no temperature files specified]\n'"
# main loop
while true; do
printbar
sleep "$INTERVAL"
if streq $(date +'%S') 00; then
# every minute
PUBLIC_IP="$(ifpublic)"
fi
done

45
niceties/displaym Executable file
View File

@@ -0,0 +1,45 @@
#!/bin/sh
set -e
argv0="$0"
error(){
printf "%s: %s\n" "$argv0" "$1" 1>&2
exit $2
}
usage(){
set +x
printf "\
Usage:
%s [window manager] [action]\n" "$argv0" 1>&2
exit 64 # sysexits(3) EX_USAGE
}
[ -n "$2" ] \
|| usage
# sysexits(3) EX_OSERR
[ -n "$HOME" ] \
|| error "No \$HOME environment variable set. This is very bad." 71
if [ -z "$DISPLAYM_CONFIG" ]; then
[ -z "$XDG_CONFIG_HOME" ] \
&& DISPLAYM_CONFIG="$HOME/.displaym" \
|| DISPLAYM_CONFIG="$XDG_CONFIG_HOME/displaym"
fi
export DISPLAYM_CONFIG
[ -e "$DISPLAYM_CONFIG" ] && ! [ -d "$DISPLAYM_CONFIG" ] \
&& bak "$DISPLAYM_CONFIG" \
|| true
[ -d "$DISPLAYM_CONFIG" ] \
|| mkdir -p "$DISPLAYM_CONFIG"
WM="$1"; export WM
SCRIPT="$2"
# with exported DISPLAYM_CONFIG and WM
sh <"$DISPLAYM_CONFIG/$WM/$SCRIPT.sh"

25
niceties/displaym.1 Normal file
View File

@@ -0,0 +1,25 @@
.TH DISPLAYM 1
.SH NAME
displaym \(en a window manager manager
.SH SYNOPSIS
displaym
.RB [ noun ]
.RB [ verb ]
.SH DESCRIPTION
Displaym runs the script verb.sh, where verb is supplied by the user to displaym, in the folder noun, where noun is supplied by the user to displaym.
.SH ENVIRONMENT VARIABLES
$DISPLAYM\_CONFIG refers to the location of the displaym configuration directory; this is the directory that contains noun/verb.sh.
If $DISPLAYM\_CONFIG is already set, it's not overriden.
The default location, if $XDG\_CONFIG\_HOME is set, is $XDG\_CONFIG\_HOME/displaym, otherwise $HOME/.displaym.
.SH DEPENDENCIES
mkdir(1), printf(1), sh(1), test(1)

20
niceties/ifpublic Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/sh
: HTTP APIs that return your public IP address [both v4 and v6] "\
https://icanhazip.com
https://ifconfig.io
http://icanhazip.com
http://ifconfig.io/
"
PUBLIC_IP_FETCH_URL="https://icanhazip.com"
PUBLIC_IP6_FETCH_URL="$PUBLIC_IP_FETCH_URL"
printf "%s / %s\n" \
"$(curl -4 --connect-timeout 5 --silent "$PUBLIC_IP_FETCH_URL")" \
"$(curl -6 --connect-timeout 5 --silent "$PUBLIC_IP6_FETCH_URL")" \
| sed -e 's/ \/ $//' \
-e 's/^ \/ //' \
-e 's/^$/[error fetching address]/'
exit 0

10
niceties/uwu Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/sh
# Simple script of substitutions to uwuify English.
# Not a great use of disk...
dd 2>/dev/null \
| sed \
-e 's/l/w/g' \
-e 's/r/w/g' \
-e 's/smaww/smol/g' \
-e 's/wove/wuv/g'

113
niceties/volume Executable file
View File

@@ -0,0 +1,113 @@
#!/bin/sh
set -e
argv0="$0"
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
# `volume a $2` must set the volume to $2, an integer value. This should be a
# percentage, 100% being the loudest possible volume that can be output by
# hardware without quality loss. Percentages above 100% will be allowable in
# software if using percentage units. If percentage units are implausible to
# implement, system units are okay (e.g. 0-$arbitrary scale versus 0-100).
# `volume s` must output the current volume in integer form in whatever units
# are being used by `volume a` on the given system.
VOLUME_TMP_FILE="$HOME/.volume-previous"
if streq "$(uname)" NetBSD; then
VOLUME_SYSTEM=netbsd
elif command -v pactl >/dev/null 2>&1; then
VOLUME_SYSTEM=pulseaudio
else
printf "%s: Unknown system.\n" "$0"
exit 1
fi
usage(){
printf "\
Usage: %b {function} (operand)\n" "$0"
printf "\
Functions:
'+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit
debug - show debugging information
help,'' - print this help output
mute - toggle mute/unmute
relative - change sound output relative to current status
status - print current sound output in system unit \
"
printf "\
The options are matched with [char]*. So \"%b help\"
works the same as \"%b h\".
" "$argv0" "$argv0"
exit 1
}
case "$1" in
+) "$0" r "$2" ;;
-) "$0" r -"$2" ;;
a*)
str isvalue $2 && ! str isvalue $3 && str isdigit "$2" \
|| usage
"$0".$VOLUME_SYSTEM "$@"
;;
s*)
! str isvalue $2 \
|| usage
"$0".$VOLUME_SYSTEM s
;;
d*)
printf '$0: %s\nAudio system detected: %s\n' \
"$0" \
$VOLUME_SYSTEM
;;
m*) ! str isvalue "$2" || usage
# restore previous volume if there is one
if test -e "$VOLUME_TMP_FILE"; then
xargs "$0" a <"$VOLUME_TMP_FILE" \
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
|| printf "Error restoring previous volume.\n" \
1>&2 \
&& exit 1
fi
# otherwise, make new file with previous volume
# dd used rather than shell redirect so it's easy to determine
# whether or not the file write worked
if ! printf "%b" "$("$0" s)" \
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
then
printf "Error writing to file.\n" 1>&2
exit 1
fi
# and then of course mute
"$0" a 0
printf "Muted.\n"
exit 0 ;;
r*)
str isvalue $2 && ! str isvalue $3 \
&& str isdigit "$(printf "%s\n" "$2" | sed 's/^\-//')" \
|| usage
"$0" a \
"$(printf "%s %s + p\n" \
$("$0" s) \
"$(printf "%s\n" $2 | tr - _)" \
| dc)"
exit $? ;;
*) usage ;;
esac

54
niceties/volume.1 Normal file
View File

@@ -0,0 +1,54 @@
.TH VOLUME 1
.SH NAME
volume \(en change the current audio volume
.SH SYNOPSIS
volume {function} [operand]
.SH DESCRIPTION
Volume provides a common interface for elementary configuration of the
current system audio volume.
.PP
Volume is not a replacement for the system-specific audio interface.
.SH FUNCTIONS
.SS +
Shortcut to "volume r ".
.SS -
Shortcut to "volume r -".
.SS a
Change sound output to absolute unit.
.SS h
Print help output.
.SS m
Toggle audio mute.
.SS r
Change sound output relative to current status.
.SS s
Print current sound output level in system unit.
.SH BUGS
Many.
.SH COPYRIGHT
Public domain.

18
niceties/volume.netbsd Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/sh -e
# NetBSD 9.2 STABLE 2021-07
case "$1" in
a*)
audioctl -w play.gain=$2 >/dev/null
exit $?
;;
s*) # hacky
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
exit $?
;;
esac
exit 1

20
niceties/volume.pulseaudio Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/sh -e
# pactl 15.0 compiled+linked with libpulse 15.0.0
case "$argv1" in
a*)
pactl set-sink-volume @DEFAULT_SINK@ $2%
exit $?
;;
s*) # really hacky, gets the job done
# gets the volume % of Lchan specifically
pactl get-sink-volume @DEFAULT_SINK@ \
| sed q \
| cut -d '/' -f 2 \
| xargs echo \
| sed s/'%'//
;;
esac
exit 1

116
niceties/xjiggler Executable file
View File

@@ -0,0 +1,116 @@
#!/bin/sh
set -e
# based on https://github.com/carrot69/keep-presence/
test -n "$XJIGGLER_DELAY_ACTION" \
|| XJIGGLER_DELAY_ACTION=300 # seconds
test -n "$XJIGGLER_DELAY_DETECTION" \
|| XJIGGLER_DELAY_DETECTION=60 # seconds
test -n "$XJIGGLER_KEYBOARD_ENABLED" \
|| XJIGGLER_KEYBOARD_ENABLED='' # zero-len nonzero-len
test -n "$XJIGGLER_MOUSE_DIRECTION" \
|| XJIGGER_MOUSE_DIRECTION=DIAGONAL # "CIRCULAR" "DIAGONAL"
test -n "$XJIGGLER_MOUSE_DISTANCE" \
|| XJIGGLER_MOUSE_DISTANCE=1 # pixels
test -n "$XJIGGLER_MOUSE_ENABLED" \
|| XJIGGLER_MOUSE_ENABLED=yes # zero-len nonzero-len
# eval is scary but this use was mentioned in the man page
getmouselocation(){
eval $(xdotool getmouselocation --shell | sed 's/^/XJIGGLER_/g')
XJIGGLER_T="$(date '+%s')"
}
jiggle(){
test -z "$XJIGGLER_MOUSE_ENABLED" \
|| xdotool mousemove_relative "$XJIGGLER_MOUSE_dX" \
"$XJIGGLER_MOUSE_dY"
}
pushmouselocation(){
XJIGGLER_lastT="$XJIGGLER_T"
XJIGGLER_lastX="$XJIGGLER_X"
XJIGGLER_lastY="$XJIGGLER_Y"
}
printdebug(){
printf "XJIGGLER_T=%s\n" "$XJIGGLER_T" # seconds since epoch
printf "XJIGGLER_X=%s\n" "$XJIGGLER_X" # pixels
printf "XJIGGLER_Y=%s\n" "$XJIGGLER_Y" # pixels
printf "XJIGGLER_lastT=%s\n" "$XJIGGLER_lastT"
printf "XJIGGLER_lastX=%s\n" "$XJIGGLER_lastX"
printf "XJIGGLER_lastY=%s\n" "$XJIGGLER_lastY"
}
rotatemousedirection(){
case "$XJIGGLER_MOUSE_DIRECTION" in
up) XJIGGLER_MOUSE_DIRECTION=right
XJIGGLER_MOUSE_dX="$XJIGGLER_MOUSE_DISTANCE"
XJIGGLER_MOUSE_dY=0
return; ;;
right) XJIGGLER_MOUSE_DIRECTION=down
XJIGGLER_MOUSE_dX=0
XJIGGLER_MOUSE_dY="$XJIGGLER_MOUSE_DISTANCE"
return; ;;
down) XJIGGLER_MOUSE_DIRECTION=left
XJIGGLER_MOUSE_dX=-"$XJIGGLER_MOUSE_DISTANCE"
XJIGGLER_MOUSE_dY=0
return; ;;
CIRCULAR | left)
XJIGGLER_MOUSE_DIRECTION=up
XJIGGLER_MOUSE_dX=0
XJIGGLER_MOUSE_dY=-"$XJIGGLER_MOUSE_DISTANCE"
DIAGONAL | *)
XJIGGLER_MOUSE_dX="$XJIGGLER_MOUSE_DISTANCE"
XJIGGLER_MOUSE_dY="$XJIGGLER_MOUSE_DISTANCE"
return; ;;
return; ;;
esac
}
usage(){
printf 'Usage: %s (-chm) (-d [distance]) %s\n' \
"$0" '(-s [action delay seconds])'>&2
exit 64 # sysexits(3) EX_USAGE
}
while getopts :cd:hms: OPTION
do
case "$OPTION" in
c) XJIGGLER_MOUSE_DIRECTION=DIAGONAL ;;
d) XJIGGLER_MOUSE_DISTANCE="$OPTARG)" ;;
m) XJIGGLER_MOUSE_ENABLED='' ;;
s) XJIGGLER_DELAY_ACTION="$(OPTARG)" ;;
*) usage ;;
esac
done
getmouselocation; pushmouselocation
while true
do
sleep "$XJIGGLER_DELAY_DETECTION"
getmouselocation
if test "$XJIGGLER_lastX" = "$XJIGGLER_X" \
&& test "$XJIGGLER_lastY" = "$XJIGGLER_Y"
then # no movement
command -v xprintidle \
&& test "$(xprintidle \
| sed -e 's/^/0/' -e 's/...$//')" \
-gt "$XJIGGLER_DELAY_ACTION" \
|| test "$(printf '%s\n%s\n-\np\n' "$XJIGGLER_T" \
"$XJIGGLER_lastT" \
| dc)" -gt "$XJIGGLER_DELAY_ACTION" \
|| continue # hasn't been long enough
rotatemousedirection; jiggle
printf '%s: Jiggled:\n' "$0"
printdebug | sed 's/^/\t/g'
getmouselocation
else # movement
printf '%s: Movement detected:\n' "$0"
printdebug | sed 's/^/\t/g'
fi
pushmouselocation
done >&2