linutil/tabs/utils/monitor-control/manage_arrangement.sh

79 lines
2.6 KiB
Bash
Raw Normal View History

2024-08-15 03:41:41 +01:00
#!/bin/sh -e
2024-08-21 08:18:34 +01:00
. ./utility_functions.sh
2024-09-12 21:14:50 +01:00
. ../../common-script.sh
# Function to manage monitor arrangement
manage_arrangement() {
monitor_list=$(detect_connected_monitors)
2024-09-12 21:14:50 +01:00
monitor_array=$(echo "$monitor_list" | tr '\n' ' ')
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW} Manage Monitor Arrangement${RC}"
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW}Choose the monitor to arrange:${RC}"
i=1
for monitor in $monitor_array; do
printf "%b\n" "$i. ${YELLOW}$monitor${RC}"
i=$((i + 1))
done
printf "Enter the number of the monitor to arrange: "
read -r monitor_choice
2024-09-12 21:14:50 +01:00
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$' || [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$((i - 1))" ]; then
printf "%b\n" "${RED}Invalid selection.${RC}"
return
fi
2024-09-12 21:14:50 +01:00
monitor_name=$(echo "$monitor_array" | cut -d' ' -f"$monitor_choice")
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}Choose position relative to other monitors:${RC}"
printf "%b\n" "1. ${YELLOW}Left of${RC}"
printf "%b\n" "2. ${YELLOW}Right of${RC}"
printf "%b\n" "3. ${YELLOW}Above${RC}"
printf "%b\n" "4. ${YELLOW}Below${RC}"
printf "Enter the number of the position: "
read -r position_choice
case $position_choice in
1) position="--left-of" ;;
2) position="--right-of" ;;
3) position="--above" ;;
4) position="--below" ;;
2024-09-12 21:14:50 +01:00
*) printf "%b\n" "${RED}Invalid selection.${RC}"; return ;;
esac
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}Choose the reference monitor:${RC}"
for i in $monitor_array; do
if [ "$i" != "$monitor_name" ]; then
printf "%b\n" "$((i + 1)). ${YELLOW}$i${RC}"
fi
done
printf "Enter the number of the reference monitor: "
read -r ref_choice
2024-09-12 21:14:50 +01:00
if ! echo "$ref_choice" | grep -qE '^[0-9]+$' || [ "$ref_choice" -lt 1 ] || [ "$ref_choice" -gt "$((i - 1))" ] || [ "$ref_choice" -eq "$monitor_choice" ]; then
printf "%b\n" "${RED}Invalid selection.${RC}"
return
fi
2024-09-12 21:14:50 +01:00
ref_monitor=$(echo "$monitor_array" | cut -d' ' -f"$ref_choice")
2024-09-12 21:14:50 +01:00
if confirm_action "Arrange ${YELLOW}$monitor_name${RC} ${position} ${YELLOW}$ref_monitor${RC}?"; then
printf "%b\n" "${GREEN}Arranging $monitor_name ${position} $ref_monitor${RC}"
execute_command "xrandr --output $monitor_name $position $ref_monitor"
2024-09-12 21:14:50 +01:00
printf "%b\n" "${GREEN}Arrangement updated successfully.${RC}"
else
2024-09-12 21:14:50 +01:00
printf "%b\n" "${RED}Action canceled.${RC}"
fi
}
# Call the manage_arrangement function
manage_arrangement