Replace echos with printf in monitor-control (#481)

Co-authored-by: nnyyxxxx <nnyyxxxx@users.noreply.github.com>
This commit is contained in:
Nyx 2024-09-18 20:37:59 -04:00 committed by GitHub
parent f4eddd551d
commit a151a15f1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 27 deletions

View File

@ -18,8 +18,8 @@ auto_detect_displays() {
temp_common_resolutions=$(mktemp) temp_common_resolutions=$(mktemp)
temp_resolutions=$(mktemp) temp_resolutions=$(mktemp)
echo "$common_resolutions" > "$temp_common_resolutions" printf "%s" "$common_resolutions" > "$temp_common_resolutions"
echo "$resolutions" > "$temp_resolutions" printf "%s" "$resolutions" > "$temp_resolutions"
common_resolutions=$(comm -12 "$temp_common_resolutions" "$temp_resolutions") common_resolutions=$(comm -12 "$temp_common_resolutions" "$temp_resolutions")
@ -27,14 +27,13 @@ auto_detect_displays() {
done done
if [ -z "$common_resolutions" ]; then 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 return
fi
highest_resolution=$(echo "$common_resolutions" | sort -n -t'x' -k1,1 -k2,2 | tail -n 1) highest_resolution=$(echo "$common_resolutions" | sort -n -t'x' -k1,1 -k2,2 | tail -n 1)
for monitor in $monitors; do 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" execute_command "xrandr --output $monitor --mode $highest_resolution"
done done
fi fi

View File

@ -20,7 +20,7 @@ adjust_monitor_brightness() {
count=$((count + 1)) count=$((count + 1))
done 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 read monitor_choice
if [ "$monitor_choice" = "q" ]; then if [ "$monitor_choice" = "q" ]; then
@ -29,15 +29,15 @@ adjust_monitor_brightness() {
fi fi
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then
echo "Invalid selection. Please try again." printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
echo "Press [Enter] to continue..." printf "Press [Enter] to continue..."
read dummy read dummy
continue continue
fi fi
if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$#" ]; then if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$#" ]; then
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}" printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
echo "Press [Enter] to continue..." printf "Press [Enter] to continue..."
read dummy read dummy
continue continue
fi fi
@ -50,7 +50,7 @@ adjust_monitor_brightness() {
printf "%b\n" "${YELLOW}Current brightness for $monitor_name${RC}: ${GREEN}$current_brightness_percentage%${RC}" printf "%b\n" "${YELLOW}Current brightness for $monitor_name${RC}: ${GREEN}$current_brightness_percentage%${RC}"
while true; do 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 read new_brightness_percentage
if [ "$new_brightness_percentage" = "q" ]; then if [ "$new_brightness_percentage" = "q" ]; then
@ -67,7 +67,7 @@ adjust_monitor_brightness() {
# Convert percentage to xrandr brightness value (10% to 0.10) # Convert percentage to xrandr brightness value (10% to 0.10)
new_brightness=$(awk "BEGIN {printf \"%.2f\", $new_brightness_percentage / 100}") 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 read confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
printf "%b\n" "${GREEN}Setting brightness for $monitor_name to $new_brightness_percentage%${RC}" printf "%b\n" "${GREEN}Setting brightness for $monitor_name to $new_brightness_percentage%${RC}"

View File

@ -41,7 +41,7 @@ set_resolutions() {
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2) resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)
temp_res_file=$(mktemp) 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 i=1
while read -r resolution; do while read -r resolution; do

View File

@ -4,7 +4,7 @@
# Function to check xrandr is installed # Function to check xrandr is installed
setup_xrandr() { setup_xrandr() {
echo "Install xrandr if not already installed..." printf "%b\n" "${YELLOW}Installing xrandr...${RC}"
if ! command_exists xrandr; then if ! command_exists xrandr; then
case "$PACKAGER" in case "$PACKAGER" in
pacman) pacman)
@ -18,24 +18,24 @@ setup_xrandr() {
;; ;;
esac esac
else else
echo "xrandr is already installed." printf "%b\n" "${GREEN}xrandr is already installed.${RC}"
fi fi
} }
# Function to execute xrandr commands and handle errors # Function to execute xrandr commands and handle errors
execute_command() { execute_command() {
command="$1" command="$1"
echo "Executing: $command" printf "Executing: %s\n" "$command"
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20 eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
if [ $? -ne 0 ]; then 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 fi
} }
# Function to detect connected monitors # Function to detect connected monitors
detect_connected_monitors() { detect_connected_monitors() {
xrandr_output=$(xrandr) 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 # Function to get the current brightness for a monitor
@ -48,31 +48,26 @@ get_current_brightness() {
get_unique_resolutions() { get_unique_resolutions() {
monitor="$1" monitor="$1"
xrandr_output=$(xrandr) xrandr_output=$(xrandr)
# Get available resolutions from xrandr without line limit available_resolutions=$(printf "%s" "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)
available_resolutions=$(echo "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)
# Define standard resolutions
standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160" standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"
temp_file=$(mktemp) 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=$(printf "%s" "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
filtered_standard_resolutions=$(echo "$standard_resolutions" | tr ' ' '\n' | grep -xF -f "$temp_file")
rm "$temp_file" rm "$temp_file"
available_res_file=$(mktemp) available_res_file=$(mktemp)
filtered_standard_res_file=$(mktemp) filtered_standard_res_file=$(mktemp)
echo "$available_resolutions" | sort > "$available_res_file" printf "%s" "$available_resolutions" | sort > "$available_res_file"
echo "$filtered_standard_resolutions" | sort > "$filtered_standard_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") remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")
rm "$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 printf "%b\n" "$filtered_standard_resolutions\n$remaining_resolutions" | head -n 10
} }