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

88 lines
3.1 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 set resolutions
set_resolutions() {
monitor_list=$(detect_connected_monitors)
2024-09-12 21:14:50 +01:00
monitor_array=$(echo "$monitor_list" | tr '\n' ' ')
while true; do
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW} Monitor Configuration${RC}"
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW}Choose a monitor to configure:${RC}"
i=1
for monitor in $monitor_array; do
printf "%b\n" "$i. ${YELLOW}$monitor${RC}"
i=$((i + 1))
done
read -p "Enter the choice (or 'q' to quit): " monitor_choice
2024-09-12 21:14:50 +01:00
if [ "$monitor_choice" = "q" ]; then
printf "%b\n" "${RED}Exiting...${RC}"
return
fi
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. Please try again.${RC}"
read -p "Press [Enter] to continue..."
continue
fi
2024-09-12 21:14:50 +01:00
monitor_name=$(echo "$monitor_array" | cut -d' ' -f"$monitor_choice")
2024-08-29 22:04:21 +01:00
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)
temp_res_file=$(mktemp)
2024-08-29 21:59:23 +01:00
echo "$resolutions" | awk '{print NR " " $0}' > "$temp_res_file"
2024-09-12 21:14:50 +01:00
i=1
while read -r resolution; do
resolution_map[$i]="$resolution"
i=$((i + 1))
done < "$temp_res_file"
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW} Resolution Configuration for ${YELLOW}$monitor_name${RC}"
printf "%b\n" "${YELLOW}=========================================${RC}"
awk '{print $1 ". " $2}' "$temp_res_file"
while true; do
read -p "Enter the choice (or 'q' to quit): " resolution_choice
2024-09-12 21:14:50 +01:00
if [ "$resolution_choice" = "q" ]; then
printf "%b\n" "${RED}Exiting...${RC}"
rm "$temp_res_file"
return
fi
2024-09-12 21:14:50 +01:00
if ! echo "$resolution_choice" | grep -qE '^[0-9]+$' || [ "$resolution_choice" -lt 1 ] || [ "$resolution_choice" -gt "$((i - 1))" ]; then
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
continue
fi
# Map the index to the actual resolution
selected_resolution=${resolution_map[$resolution_choice]}
read -p "Set resolution for $monitor_name to $selected_resolution? (y/n): " confirm
2024-09-12 21:14:50 +01:00
if echo "$confirm" | grep -qE '^[Yy]$'; then
printf "%b\n" "${GREEN}Setting resolution for $monitor_name to $selected_resolution${RC}"
execute_command "xrandr --output $monitor_name --mode $selected_resolution"
break
else
2024-09-12 21:14:50 +01:00
printf "%b\n" "${RED}Action canceled. Please choose a different resolution.${RC}"
fi
done
rm "$temp_res_file"
done
}
set_resolutions