Skip to content

Commit

Permalink
Refactor utility functions
Browse files Browse the repository at this point in the history
Separate utitility functions into user manager functions and monitor control functions, disable somechecks and formatting
  • Loading branch information
jeevithakannan2 committed Nov 21, 2024
1 parent ab7a670 commit 7533606
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 126 deletions.
13 changes: 6 additions & 7 deletions core/tabs/utils/monitor-control/auto_detect_displays.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to auto-detect displays and set common resolution
auto_detect_displays() {
if confirm_action "Auto-detect displays and set common resolution?"; then
execute_command "xrandr --auto"

monitors=$(detect_connected_monitors)
first_monitor=$(echo "$monitors" | head -n 1)
common_resolutions=$(get_unique_resolutions "$first_monitor")
Expand All @@ -18,14 +17,14 @@ auto_detect_displays() {
temp_common_resolutions=$(mktemp)
temp_resolutions=$(mktemp)

printf "%b\n" "$common_resolutions" > "$temp_common_resolutions"
printf "%b\n" "$resolutions" > "$temp_resolutions"
printf "%b\n" "$common_resolutions" >"$temp_common_resolutions"
printf "%b\n" "$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
printf "%b\n" "${RED}No common resolution found among connected monitors.${RC}"
return
Expand Down
10 changes: 6 additions & 4 deletions core/tabs/utils/monitor-control/change_orientation.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to change monitor orientation
change_orientation() {
Expand Down Expand Up @@ -47,7 +46,10 @@ change_orientation() {
2) orientation="left" ;;
3) orientation="right" ;;
4) orientation="inverted" ;;
*) printf "%b\n" "${RED}Invalid selection.${RC}"; return ;;
*)
printf "%b\n" "${RED}Invalid selection.${RC}"
return
;;
esac

if confirm_action "Change orientation of $monitor_name to $orientation?"; then
Expand Down
7 changes: 3 additions & 4 deletions core/tabs/utils/monitor-control/disable_monitor.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to disable a monitor
disable_monitor() {
Expand Down Expand Up @@ -31,7 +30,7 @@ disable_monitor() {
monitor_name=$(echo "$monitor_array" | cut -d' ' -f"$monitor_choice")

printf "%b\n" "${RED}Warning: Disabling the monitor will turn it off and may affect your display setup.${RC}"

if confirm_action "Do you really want to disable ${GREEN}$monitor_name${RC}?"; then
printf "%b\n" "${GREEN}Disabling $monitor_name${RC}"
execute_command "xrandr --output $monitor_name --off"
Expand Down
5 changes: 2 additions & 3 deletions core/tabs/utils/monitor-control/duplicate_displays.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to duplicate displays
duplicate_displays() {
Expand Down
7 changes: 3 additions & 4 deletions core/tabs/utils/monitor-control/enable_monitor.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to enable a monitor
enable_monitor() {
Expand All @@ -14,7 +13,7 @@ enable_monitor() {
printf "%b\n" "${YELLOW} Enable Monitor${RC}"
printf "%b\n" "${YELLOW}=========================================${RC}"
printf "%b\n" "${YELLOW}Choose a monitor to enable: ${RC}"

i=1
for monitor in $monitor_array; do
printf "%b\n" "$i. ${GREEN}$monitor${RC}"
Expand Down
7 changes: 3 additions & 4 deletions core/tabs/utils/monitor-control/extend_displays.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to extend displays
extend_displays() {
Expand All @@ -11,7 +10,7 @@ extend_displays() {
i=1
for monitor in $monitor_array; do
if [ "$i" -gt 1 ]; then
prev_monitor=$(echo "$monitor_array" | cut -d' ' -f$((i-1)))
prev_monitor=$(echo "$monitor_array" | cut -d' ' -f$((i - 1)))
if confirm_action "Extend $monitor to the right of $prev_monitor?"; then
printf "%b\n" "${GREEN}Extending $monitor to the right of $prev_monitor${RC}"
execute_command "xrandr --output $monitor --right-of $prev_monitor"
Expand Down
10 changes: 6 additions & 4 deletions core/tabs/utils/monitor-control/manage_arrangement.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to manage monitor arrangement
manage_arrangement() {
Expand Down Expand Up @@ -45,7 +44,10 @@ manage_arrangement() {
2) position="--right-of" ;;
3) position="--above" ;;
4) position="--below" ;;
*) printf "%b\n" "${RED}Invalid selection.${RC}"; return ;;
*)
printf "%b\n" "${RED}Invalid selection.${RC}"
return
;;
esac

printf "%b\n" "${YELLOW}Choose the reference monitor:${RC}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ setup_xrandr() {
pacman)
"$ESCALATION_TOOL" "$PACKAGER" -S --noconfirm xorg-xrandr
;;
apt-get|nala)
apt-get | nala)
"$ESCALATION_TOOL" "$PACKAGER" install -y x11-xserver-utils
;;
apk)
Expand All @@ -30,6 +30,7 @@ execute_command() {
command="$1"
printf "Executing: %s\n" "$command"
eval "$command" 2>&1 | tee /tmp/xrandr.log | tail -n 20
#shellcheck disable=SC2181
if [ $? -ne 0 ]; then
printf "%b\n" "${RED}An error occurred while executing the command. Check /tmp/xrandr.log for details.${RC}"
fi
Expand All @@ -52,25 +53,25 @@ get_unique_resolutions() {
monitor="$1"
xrandr_output=$(xrandr)
available_resolutions=$(printf "%s" "$xrandr_output" | sed -n "/$monitor connected/,/^[^ ]/p" | grep -oP '\d+x\d+' | sort -u)

standard_resolutions="1920x1080 1280x720 1600x900 2560x1440 3840x2160"

temp_file=$(mktemp)
printf "%s" "$available_resolutions" > "$temp_file"
printf "%s" "$available_resolutions" >"$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)
printf "%s" "$available_resolutions" | sort > "$available_res_file"
printf "%s" "$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"

remaining_resolutions=$(comm -23 "$available_res_file" "$filtered_standard_res_file")

rm "$available_res_file" "$filtered_standard_res_file"

printf "%b\n" "$filtered_standard_resolutions\n$remaining_resolutions" | head -n 10
}

Expand All @@ -87,31 +88,6 @@ confirm_action() {
fi
}

checkEmpty() {
if [ -z "$1" ]; then
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
exit 1
fi
}

checkGroups() {
groups="$1"
available_groups="$2"
for group in $groups; do
if ! echo "$available_groups" | grep -q -w "$group"; then
return 1
fi
done
return 0
}

confirmAction() {
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
exit 1
fi
}

checkEnv
checkEscalationTool
setup_xrandr
5 changes: 2 additions & 3 deletions core/tabs/utils/monitor-control/reset_scaling.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to Reset scaling back to 1 (native resolution) for all monitors
Reset_scaling() {
Expand Down
5 changes: 2 additions & 3 deletions core/tabs/utils/monitor-control/scale_monitor.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to scale smaller monitors to the highest resolution of a bigger monitor
scale_monitors() {
Expand Down
7 changes: 4 additions & 3 deletions core/tabs/utils/monitor-control/set_brightness.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to adjust brightness for a selected monitor
adjust_monitor_brightness() {
Expand Down Expand Up @@ -29,15 +30,15 @@ adjust_monitor_brightness() {
if ! echo "$monitor_choice" | grep -qE '^[0-9]+$'; then
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
printf "Press [Enter] to continue..."
read -r dummy
read -r _
continue
fi

monitor_count=$(echo "$monitor_list" | wc -l)
if [ "$monitor_choice" -lt 1 ] || [ "$monitor_choice" -gt "$monitor_count" ]; then
printf "%b\n" "${RED}Invalid selection. Please try again.${RC}"
printf "Press [Enter] to continue..."
read -r dummy
read -r _
continue
fi

Expand Down
5 changes: 2 additions & 3 deletions core/tabs/utils/monitor-control/set_primary_monitor.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to set a monitor as primary
set_primary_monitor() {
Expand Down
13 changes: 6 additions & 7 deletions core/tabs/utils/monitor-control/set_resolutions.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../utility_functions.sh

. ../../common-script.sh
# shellcheck disable=SC1091
. ./monitor-control-functions.sh

# Function to set resolutions
set_resolutions() {
Expand Down Expand Up @@ -33,21 +32,21 @@ set_resolutions() {
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}"
printf "%b\n" "Press [Enter] to continue..."
read -r dummy
read -r _
continue
fi

monitor_name=$(echo "$monitor_array" | cut -d' ' -f"$monitor_choice")
resolutions=$(get_unique_resolutions "$monitor_name" | sort -rn -t'x' -k1,1 -k2,2)

temp_res_file=$(mktemp)
printf "%b\n" "$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
echo "$resolution" >> "$temp_res_file"
echo "$resolution" >>"$temp_res_file"
i=$((i + 1))
done < "$temp_res_file"
done <"$temp_res_file"

clear
printf "%b\n" "${YELLOW}=========================================${RC}"
Expand Down
14 changes: 5 additions & 9 deletions core/tabs/utils/user-account-manager/add_to_group.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh -e

. ../../common-script.sh

. ../utility_functions.sh
# shellcheck disable=SC1091
. ./user-manager-functions.sh

addToGroup() {
clear
Expand All @@ -24,7 +23,7 @@ addToGroup() {
printf "%b" "${YELLOW}Enter the groups you want to add user $username to (space-separated): ${RC}"
read -r groups

checkEmpty "$groups" || exit 1
checkEmpty "$groups"
if ! checkGroups "$groups" "$available_groups"; then
printf "%b\n" "${RED}One or more groups are not available.${RC}"
exit 1
Expand All @@ -34,14 +33,11 @@ addToGroup() {

printf "%b" "${YELLOW}Are you sure you want to add user $username to $groups_to_add? [Y/n]: ${RC}"
read -r confirm
confirmAction || exit 1
confirmAction "$confirm"

"$ESCALATION_TOOL" usermod -aG "$groups_to_add" "$username"

printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}"
}

checkEnv
checkEscalationTool
checkGroups
addToGroup
addToGroup
Loading

0 comments on commit 7533606

Please sign in to comment.