6
6
adjust_monitor_brightness () {
7
7
while true ; do
8
8
monitor_list=$( detect_connected_monitors)
9
- monitor_array=$( echo " $monitor_list " | tr ' \n' ' ' )
10
- set -- " $monitor_array "
11
9
count=1
12
10
13
11
clear
14
12
printf " %b\n" " ${YELLOW} =========================================${RC} "
15
13
printf " %b\n" " ${YELLOW} Adjust Monitor Brightness${RC} "
16
14
printf " %b\n" " ${YELLOW} =========================================${RC} "
17
15
printf " %b\n" " ${YELLOW} Choose a monitor to adjust brightness:${RC} "
18
- for monitor in " $@ " ; do
16
+ echo " $monitor_list " | while IFS= read -r monitor ; do
19
17
echo " $count . $monitor "
20
18
count=$(( count + 1 ))
21
19
done
@@ -35,18 +33,18 @@ adjust_monitor_brightness() {
35
33
continue
36
34
fi
37
35
38
- if [ " $monitor_choice " -lt 1 ] || [ " $monitor_choice " -gt " $# " ]; then
36
+ monitor_count=$( echo " $monitor_list " | wc -l)
37
+ if [ " $monitor_choice " -lt 1 ] || [ " $monitor_choice " -gt " $monitor_count " ]; then
39
38
printf " %b\n" " ${RED} Invalid selection. Please try again.${RC} "
40
39
printf " Press [Enter] to continue..."
41
40
read -r dummy
42
41
continue
43
42
fi
44
43
45
- monitor_name=$( eval echo " \$ { $ monitor_choice }" )
44
+ monitor_name=$( echo " $monitor_list " | sed -n " ${ monitor_choice}p " )
46
45
current_brightness=$( get_current_brightness " $monitor_name " )
47
46
48
- # Correctly calculate the brightness percentage
49
- current_brightness_percentage=$( awk " BEGIN {printf \" %.0f\" , $current_brightness * 100}" )
47
+ current_brightness_percentage=$( awk -v brightness=" $current_brightness " ' BEGIN {printf "%.0f", brightness * 100}' )
50
48
printf " %b\n" " ${YELLOW} Current brightness for $monitor_name ${RC} : ${GREEN} $current_brightness_percentage %${RC} "
51
49
52
50
while true ; do
0 commit comments