Compare commits

...

2 Commits

Author SHA1 Message Date
Epic NaN 5cc5009bb0 added pactl-next-output-dev.sh 2024-05-10 19:58:07 +02:00
Epic NaN b77a4e9d65 adjusted README 2024-05-10 19:57:54 +02:00
2 changed files with 86 additions and 1 deletions

View File

@ -1 +1,5 @@
this repo has some misc bash scripts i wrote
this repo has some misc scripts i wrote
they all worked on my machine at the point of upload, but don't expect me to fix it for anyone else
all scripts are to be run on a full Linux systems, but they *might* work on WSL / UNIX / BSD / whatevs

81
sound/pactl-next-output-dev.sh Executable file
View File

@ -0,0 +1,81 @@
#/bin/bash
#
# author: arian
# date: 2024-05-06
#
# purpose:
# use this script on a keyboard shortcut to automatically
# change your output device to the next one using 'pactl'
#
# usage:
# ./pactl-next-output-dev.sh /path/to/log
#
#region run in debug if $DEBUG
[ "$DEBUG" = 'true' ] && set -x
#endregion
#region globals
CYAN='\e[0;36m'
PURPLE='\e[0;35m'
NORMAL='\e[0m'
LOGFILE=${1:-'/var/log/arian/pactl-next-output-dev.sh.log'}
LOGDIR="$(dirname "${LOGFILE}")"
#endregion
#region functions
function log() {
# logs strings to both the terminal and a file
# usage: log [1-3] 'String'
# 1 => INFORMATION
# 2 => ERROR
# 3 => DEBUG
case $1 in
1)
msg_type="INFORMATION"
;;
2)
msg_type="ERROR"
;;
3)
msg_type="DEBUG"
;;
*)
echo "Wrong input; exiting"
exit 1
esac
msg="[${PURPLE}$(date -Ins)${NORMAL}] [${CYAN}${msg_type}${NORMAL}]: $2"
# tee command from https://unix.stackexchange.com/questions/694671/leave-color-in-stdout-but-remove-from-tee
# (omits coloring in logfile)
echo -e "${msg}" | tee >(sed $'s/\033[[][^A-Za-z]*[A-Za-z]//g' >> ${LOGFILE})
}
#endregion
#region main
[ -d "${LOGDIR}" ] || mkdir -p "${LOGDIR}"
ALL_SINKS=($(pactl list short sinks | awk '{ print $2 }'))
CURR_SINK=$(pactl get-default-sink)
CURR_INDEX=-1
log 1 "Using current sink [${CURR_SINK}]"
for i in "${!ALL_SINKS[@]}"; do
if [[ "${ALL_SINKS[$i]}" = "${CURR_SINK}" ]]; then
CURR_INDEX="$i"
fi
done
NEXT_INDEX=$(($((CURR_INDEX + 1))%(${#ALL_SINKS[@]})))
NEXT_SINK="${ALL_SINKS[$NEXT_INDEX]}"
log 1 "Switching to sink [${NEXT_SINK}]"
pactl set-default-sink "${ALL_SINKS[$NEXT_INDEX]}"
#endregion