Skip to content

Instantly share code, notes, and snippets.

@buldezir
Created November 18, 2024 07:16
Show Gist options
  • Save buldezir/c80a3e805fb9dc730def9a2acfc316ea to your computer and use it in GitHub Desktop.
Save buldezir/c80a3e805fb9dc730def9a2acfc316ea to your computer and use it in GitHub Desktop.
Switch external display input with m1ddc
#!/bin/bash
function choose_from_menu() {
local prompt="$1" outvar="$2"
shift
shift
local options=("$@") cur=0 count=${#options[@]} index=0
local esc=$(echo -en "\033") # cache ESC as test doesn't allow esc codes
printf "$prompt\n"
while true
do
# list all options (option list is zero-based)
index=0
for o in "${options[@]}"
do
if [ "$index" == "$cur" ]
then echo -e " >\033[7m$o\033[0m" # mark & highlight the current option
else echo " $o"
fi
(( index++ ))
done
read -s -n3 key # wait for user to key in arrows or ENTER
if [[ $key == $esc[A ]] # up arrow
then (( cur-- )); (( cur < 0 )) && (( cur = 0 ))
elif [[ $key == $esc[B ]] # down arrow
then (( cur++ )); (( cur >= count )) && (( cur = count - 1 ))
elif [[ $key == "" ]] # nothing, i.e the read delimiter - ENTER
then break
fi
echo -en "\033[${count}A" # go up to the beginning to re-render
done
# export the selection to the requested output variable
printf -v $outvar "${options[$cur]}"
}
selections=(
"hdmi"
"display port"
"usb-c"
)
choose_from_menu "Please make a choice:" selected_choice "${selections[@]}"
echo "Using m1ddc to select: $selected_choice"
case $selected_choice in
"hdmi")
m1ddc display 2 set input 17
;;
"display port")
m1ddc display 2 set input 15
;;
"usb-c")
m1ddc display 2 set input 27
;;
*)
echo -n "unknown"
;;
esac
@buldezir
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment