1
0
This commit is contained in:
dtb
2022-05-18 17:44:50 -04:00
parent 63748f0746
commit e0540495a0
54 changed files with 1895 additions and 0 deletions

11
bin/autotranslate Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/sh -x
[ -n "$dmenu" ] || dmenu=dmenu
lang="$LANG"
# lots of potentially wrong assumptions here
printf "%s\n" "$lang" | grep "_" >/dev/null 2>&1 \
&& lang="$(printf "%s\n" "$lang" | cut -d '_' -f 1)" \
|| lang=en
printf "%s\n" "$(xclip -o)" | translate auto $lang | "$($dmenu -p "Press Escape to dismiss.")"

54
bin/batterymonitor Executable file
View File

@@ -0,0 +1,54 @@
#!/bin/sh
POLLING_FREQUENCY=1
# ISO 8601
alias date="date '+%Y-%m-%dT%T'"
emit() {
printf "[%s] " "$(date)"
echo "$@"
}
get_current_level() {
# adapted from other script i wrote, also in the public domain
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/,$//')"
return 0
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 0
else
printf "get_battery_status: unsupported OS" >>/dev/stderr
return 1
fi
}
! get_current_level >/dev/null \
&& printf "Unable to get battery status on this system.\n" >>/dev/stderr \
&& exit 1 \
|| true
current_level=$(get_current_level)
previous_level=$current_level
emit "$(printf "Current level: %d%%" "$current_level")"
while true; do
current_level=$(get_current_level)
if ! [ $current_level -eq $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

211
bin/data Executable file
View File

@@ -0,0 +1,211 @@
#!/bin/sh
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:
# 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/"
#PUBLIC_IP_FETCH_URL="https://ifconfig.io"
#PUBLIC_IP_FETCH_URL="http://icanhazip.com"
PUBLIC_IP_FETCH_URL="https://icanhazip.com"
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() {
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)"
return
else
printf "get_cpu_temp: unsupported OS"
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() {
PUBLIC_IP4="$(curl -4 "$PUBLIC_IP_FETCH_URL" --no-progress-meter 2>/dev/null \
|| printf "-1")"
PUBLIC_IP6="$(curl -6 "$PUBLIC_IP6_FETCH_URL" --no-progress-meter 2>/dev/null \
|| printf "-1")"
if [ "$PUBLIC_IP4" = "-1" ] && [ "$PUBLIC_IP6" = "-1" ]; then
printf "[error fetching address]"
elif [ "$PUBLIC_IP4" = "-1" ]; then
printf "%b" "$PUBLIC_IP6"
elif [ "$PUBLIC_IP6" = "-1" ]; then
printf "%b" "$PUBLIC_IP4"
else
printf "%b / %b" "$PUBLIC_IP4" "$PUBLIC_IP6"
fi
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

36
bin/mnt Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/sh
set -e
DEV="/dev"
MNT="/mnt"
# will not be split when used
SUDO="doas"
# preliminary checks
[ -n "$2" ] || [ -z "$1" ] \
&& printf "Usage: %b [device in %b/]\n" "$0" "$DEV" >>/dev/stderr \
&& exit 1 \
|| true
# no need to priviledge escalate if we're already root
! [ "$(id -u)" = 0 ] \
|| SUDO=""
# exhaustive checks to ensure $DEV/$1 exists and that $MNT/$1 can be safely used as a mountpoint
! [ -d "$MNT/$1" ] && [ -a "$MNT/$1" ] \
&& printf "%b: '%b/%b' already exists, but isn't a directory.\n" "$0" "$MNT" "$1" >>/dev/stderr \
&& exit 1 \
|| true
[ -d "$MNT/$1" ] \
|| mkdir "$MNT/$1"
! [ "$(ls -A "$MNT/$1" | wc -l | xargs echo)" = 0 ] \
&& printf "%b: '%b/%b' is a directory, but isn't empty.\n" "$0" "$MNT" "$1" >>/dev/stderr \
&& exit 1 \
|| true
! [ -e "/dev/$1" ] \
&& printf "%b: '%b/%b' doesn't exist.\n" "$0" "$DEV" "$1" >>/dev/stderr \
&& exit 1 \
|| true
# ok, we're safe
"$SUDO" mount "$DEV/$1" "$MNT/$1"

39
bin/music Executable file
View File

@@ -0,0 +1,39 @@
#!/bin/sh
set -e
# DistroTube's dm-radio fixed and in POSIX shell
alias nonzero="test -n"
nonzero "$MUSIC_PLAYER" || MUSIC_PLAYER=mpv
nonzero "$DMENU" || DMENU=dmenu
# tab delimits name from URL
STATIONS="\
50s Rewind https://zeno.fm/radio/50s-rewind/
60s Rewind https://zeno.fm/radio/60s-rewind/
70s Rewind https://zeno.fm/radio/70s-rewind/
80s Rewind https://zeno.fm/radio/80s-rewind/
90s Rock https://zeno.fm/radio/90s-rock/
The 2000s https://zeno.fm/radio/the-2000s/
Classical Radio https://zeno.fm/radio/classical-radio/
Classical Relaxation https://zeno.fm/radio/radio-christmas-non-stop-classical/
Classic Rock https://zeno.fm/radio/classic-rockdnb2sav8qs8uv/
Gangsta49 https://zeno.fm/radio/gangsta49/
HipHop49 https://zeno.fm/radio/hiphop49/
Madhouse Country Radio https://zeno.fm/radio/madhouse-country-radio/
PopMusic https://zeno.fm/radio/popmusic74vyurvmug0uv/
PopStars https://zeno.fm/radio/popstars/
RadioMetal https://zeno.fm/radio/radio-metal/
RocknRoll Radio https://zeno.fm/radio/rocknroll-radio994c7517qs8uv/
Cancel nop://
"
selection="$(printf "%s\n" "$STATIONS" | grep -F "$(printf "%s\n" "$STATIONS" | cut -f 1 | dmenu) ")"
if [ "$selection" = "Cancel nop://" ]
then exit 0
fi
pkill -f http || printf "%s: mpv not running.\n" "$0" 1>&2
printf "%s\n" "$selection" | cut -f 2 | xargs mpv

33
bin/rotate Executable file
View File

@@ -0,0 +1,33 @@
#!/bin/sh
set -e
ROTATION_FILE="$HOME/.rotation"
SCREEN="$1"
_core(){
case "$1" in
1) xrandr --output "$SCREEN" --rotate normal ;;
2) xrandr --output "$SCREEN" --rotate left ;;
3) xrandr --output "$SCREEN" --rotate inverted ;;
4) xrandr --output "$SCREEN" --rotate right ;;
*) return 1
esac
return 0
}
if [ -e "$ROTATION_FILE" ]
then ROTATION="$(dd <"$ROTATION_FILE" 2>/dev/null)"
if ! _core "$ROTATION"
then _core 1
ROTATION=2
else ROTATION=$(printf "%s + 1\n" "$ROTATION" | bc)
! [ "$ROTATION" -gt 4 ] \
|| ROTATION=1
fi
else _core 1
ROTATION=2
fi
printf "%s\n" "$ROTATION" >"$ROTATION_FILE"
exit 0

12
bin/transform Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/sh
argv0="$0"
usage(){
printf "Usage: %s (un) [XRandR output]\n" "$argv0" 1>&2
exit 64 # sysexits(3) EX_USAGE
}
if [ -n "$2" ] && [ "$1" = un ]
then xrandr --output "$2" --transform none
elif [ -n "$1" ]
then "$DISPLAYM_CONFIG/xrandr_transform.sh" "$1"
else usage
fi

85
bin/translate Executable file
View File

@@ -0,0 +1,85 @@
#!/usr/bin/env python3
# googletrans comes with a translate(1) command line application but I don't
# like its UI.
import sys;
stdin = sys.stdin;
stdout = sys.stdout;
stderr = sys.stderr;
_exit = exit;
exit = sys.exit;
def printf(s, *args):
return fprintf(stdout, s, *args);
def fprintf(f, s, *args):
printing = s if len(args) == 0 else (s % args);
return f.write(printing);
try:
import googletrans;
except:
printf(
"%s: This Python script requires the \"googletrans\" library.\n",
sys.argv[0]
);
def usage(name):
fprintf(
stderr, "Usage: %s [source language] [destination language]\n",
name
);
exit(1);
def main(argc, argv):
accepted_languages = set(list(googletrans.LANGCODES)
+ list(googletrans.LANGUAGES) + [ "auto" ]);
good = True;
isatty = stdout.isatty();
translator = googletrans.Translator();
if argc != 3:
usage(argv[0]);
src = argv[1].lower();
dest = argv[2].lower();
for arg in [ src, dest ]:
if not(arg in accepted_languages):
good = False;
fprintf(stderr,
"%s: %s: Language not recognized.\n",
argv[0], arg);
if(not(good)):
fprintf(stderr, "The following languages and language codes are"
+ " recognized:\n");
print(googletrans.LANGCODES);
exit(1);
try:
text = stdin.read().split('\n')[:-1]
except KeyboardInterrupt:
fprintf(stderr, "%s: Cancelled (keyboard interrupt).\n", argv[0]);
exit(1);
for line in text:
try:
translation = translator.translate(line, src=src, dest=dest);
except:
fprintf(stderr, "%s: %s ->%s\n\t%s\n",
argv[0], line, dest, "Error trying to translate.");
if isatty:
fprintf(stdout,
"%s -> %s\n"
+ "\t%s -> %s\n"
+ "\tGiven pronunciation: %s\n",
translation.origin, translation.text,
translation.src, translation.dest,
translation.pronunciation
);
else:
fprintf(stdout, "%s\n", translation.text);
return 0;
exit(main(len(sys.argv), sys.argv));

158
bin/volume Executable file
View File

@@ -0,0 +1,158 @@
#!/bin/sh
argv0="$0"
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
# `volume a` and `volume s` are the system dependent functions.
# `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 [ "$(uname)" = "NetBSD" ]; then
backend="NetBSD"
elif command -v pactl >/dev/null; then
backend="pulseaudio"
fi
unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2
exit 1
}
usage(){
printf "\
Usage: %b {function} (operand)\n" "$argv0"
printf "\
Functions:
'+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit
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"
printf "\
The backend detected is %b.
" "$backend"
exit 1
}
# $1 becomes 'help' if previously empty
[ -n "$1" ] \
&& argv1="$1" \
|| argv1=help
case "$argv1" in
#
# SYSTEM DEPENDENT
#
(a*)
[ -n "$2" ] && [ -z "$3" ] || usage
case "$backend" in
NetBSD)
# NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null
;;
pulseaudio)
# pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2%
;;
*) unknown_system
esac
exit 0 ;;
(s*)
[ -z "$2" ] || usage
case "$backend" in
NetBSD)
# hacky
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
;;
pulseaudio)
# 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/'%'//
;;
*) unknown_system ;;
esac
exit 0 ;;
#
# SYSTEM independent
#
(+)
[ -n "$2" ] && [ -z "$3" ] \
&& "$argv0" r "$2" \
|| usage
;;
(-)
[ -n "$2" ] && [ -z "$3" ] \
&& "$argv0" r -"$2" \
|| usage
;;
(m*)
[ -z "$2" ] || usage
# restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then
"$argv0" a "$(cat "$VOLUME_TMP_FILE")" \
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
|| printf "Error restoring previous volume.\n" \
>/dev/stderr \
&& 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" "$("$argv0" s)" \
| dd >"$VOLUME_TMP_FILE" 2>/dev/null
then
printf "Error writing to file.\n" >/dev/stderr
false
fi
# and then of course mute
"$argv0" a 0
printf "Muted.\n"
exit 0 ;;
(r*)
[ -n "$2" ] && [ -z "$3" ] \
|| usage
if ! newval="$(printf "%s %s + p\n" "$("$argv0" s)" "$(printf "%s\n" "$2" | sed 's/^-/_/')" | dc)"; then
printf "%b: Error finding new value for volume.\n" "$argv0"
exit 1
fi
"$argv0" a "$newval"
exit $? ;;
(h*) usage ;;
(*) usage ;;
esac