Skip to content

Commit

Permalink
Replace echos with printf in monitor-control (#481)
Browse files Browse the repository at this point in the history
Co-authored-by: nnyyxxxx <[email protected]>
  • Loading branch information
nnyyxxxx and nnyyxxxx authored Sep 19, 2024
1 parent f4eddd5 commit a151a15
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
9 changes: 4 additions & 5 deletions tabs/utils/monitor-control/auto_detect_displays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ auto_detect_displays() {
temp_common_resolutions=$(mktemp)
temp_resolutions=$(mktemp)

echo "$common_resolutions" > "$temp_common_resolutions"
echo "$resolutions" > "$temp_resolutions"
printf "%s" "$common_resolutions" > "$temp_common_resolutions"
printf "%s" "$resolutions" > "$temp_resolutions"

common_resolutions=$(comm -12 "$temp_common_resolutions" "$temp_resolutions")

rm -f "$temp_common_resolutions" "$temp_resolutions"
done

if [ -z "$common_resolutions" ]; then
echo "No common resolution found among connected monitors."
printf "%b\n" "${RED}No common resolution found among connected monitors.${RC}"
return
fi

highest_resolution=$(echo "$common_resolutions" | sort -n -t'x' -k1,1 -k2,2 | tail -n 1)

for monitor in $monitors; do
echo "Setting resolution for $monitor to $highest_resolution"
printf "%b\n" "${YELLOW}Setting resolution for $monitor to $highest_resolution...${RC}"
execute_command "xrandr --output $monitor --mode $highest_resolution"
done
fi
Expand Down
12 changes: 6 additions & 6 deletions tabs/utils/monitor-control/set_brightness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ adjust_monitor_brightness() {
count=$((count + 1))
done

echo "Enter the number of the monitor (or 'q' to quit): "
printf "Enter the number of the monitor (or 'q' to quit): "
read monitor_choice

if [ "$monitor_choice" = "q" ]; then
Expand All @@ -29,15 +29,15 @@ adjust_monitor_brightness() {
fi

if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then
echo "Invalid selection. Please try again."
echo "Press [Enter] to continue..."
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
printf "Press [Enter] to continue..."
read dummy
continue
fi

if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$#" ]; then
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
echo "Press [Enter] to continue..."
printf "Press [Enter] to continue..."
read dummy
continue
fi
Expand All @@ -50,7 +50,7 @@ adjust_monitor_brightness() {
printf "%b\n" "${YELLOW}Current brightness for $monitor_name${RC}: ${GREEN}$current_brightness_percentage%${RC}"

while true; do
echo "Enter the new brightness value as a percentage (10 to 100, or 'q' to quit): "
printf "Enter the new brightness value as a percentage (10 to 100, or 'q' to quit): "
read new_brightness_percentage

if [ "$new_brightness_percentage" = "q" ]; then
Expand All @@ -67,7 +67,7 @@ adjust_monitor_brightness() {
# Convert percentage to xrandr brightness value (10% to 0.10)
new_brightness=$(awk "BEGIN {printf \"%.2f\", $new_brightness_percentage / 100}")

echo "Set brightness for $monitor_name to $new_brightness_percentage%? (y/n): "
printf "Set brightness for $monitor_name to $new_brightness_percentage%? (y/n): "
read confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
printf "%b\n" "${GREEN}Setting brightness for $monitor_name to $new_brightness_percentage%${RC}"
Expand Down
2 changes: 1 addition & 1 deletion tabs/utils/monitor-control/set_resolutions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ set_resolutions() {
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)

temp_res_file=$(mktemp)
echo "$resolutions" | awk '{print NR " " $0}' > "$temp_res_file"
printf "%b\n" "$resolutions" | awk '{print NR " " $0}' > "$temp_res_file"

i=1
while read -r resolution; do
Expand Down
25 changes: 10 additions & 15 deletions tabs/utils/monitor-control/utility_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Function to check xrandr is installed
setup_xrandr() {
echo "Install xrandr if not already installed..."
printf "%b\n" "${YELLOW}Installing xrandr...${RC}"
if ! command_exists xrandr; then
case "$PACKAGER" in
pacman)
Expand All @@ -18,24 +18,24 @@ setup_xrandr() {
;;
esac
else
echo "xrandr is already installed."
printf "%b\n" "${GREEN}xrandr is already installed.${RC}"
fi
}

# Function to execute xrandr commands and handle errors
execute_command() {
command="$1"
echo "Executing: $command"
printf "Executing: %s\n" "$command"
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
if [ $? -ne 0 ]; then
echo "An error occurred while executing the command. Check /tmp/xrandr.log for details."
printf "%b\n" "${RED}An error occurred while executing the command. Check /tmp/xrandr.log for details.${RC}"
fi
}

# Function to detect connected monitors
detect_connected_monitors() {
xrandr_output=$(xrandr)
echo "$xrandr_output" | grep " connected" | awk '{print $1}'
printf "%s\n" "$xrandr_output" | grep " connected" | awk '{print $1}'
}

# Function to get the current brightness for a monitor
Expand All @@ -48,31 +48,26 @@ get_current_brightness() {
get_unique_resolutions() {
monitor="$1"
xrandr_output=$(xrandr)
# Get available resolutions from xrandr without line limit
available_resolutions=$(echo "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)
available_resolutions=$(printf "%s" "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)

# Define standard resolutions
standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"

temp_file=$(mktemp)
echo "$available_resolutions" > "$temp_file"
printf "%s" "$available_resolutions" > "$temp_file"

# Filter standard resolutions to include only those available for the monitor
filtered_standard_resolutions=$(echo "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
filtered_standard_resolutions=$(printf "%s" "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")

rm "$temp_file"

available_res_file=$(mktemp)
filtered_standard_res_file=$(mktemp)
echo "$available_resolutions" | sort > "$available_res_file"
echo "$filtered_standard_resolutions" | sort > "$filtered_standard_res_file"
printf "%s" "$available_resolutions" | sort > "$available_res_file"
printf "%s" "$filtered_standard_resolutions" | sort > "$filtered_standard_res_file"

# Get remaining available resolutions (excluding standard ones)
remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")

rm "$available_res_file" "$filtered_standard_res_file"

# Combine filtered standard resolutions and remaining resolutions, and limit to 10 results
printf "%b\n" "$filtered_standard_resolutions\n$remaining_resolutions" | head -n 10
}

Expand Down

0 comments on commit a151a15

Please sign in to comment.