mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-12-23 20:09:44 +00:00
Replace echos with printf in monitor-control (#481)
Co-authored-by: nnyyxxxx <nnyyxxxx@users.noreply.github.com>
This commit is contained in:
parent
f4eddd551d
commit
a151a15f1d
|
@ -18,8 +18,8 @@ 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")
|
||||
|
||||
|
@ -27,14 +27,13 @@ auto_detect_displays() {
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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}"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user