-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickshell.sh
executable file
·164 lines (147 loc) · 3.83 KB
/
quickshell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/sh
BRAND="⚡️ quickshell ⚡️"
HEADLINE="Fast select day-to-day commands, because ⏰ is 💰!"
# Initialize selected index
selected_index=0
# Read options from file and populate the array
read_commands() {
IFS=$'\n' read -d '' -r -a options < ~/.quickshell
}
# read ~/.quickshell
read_commands
quickshell_label() {
echo ""
echo $BRAND
}
# Function to clear the screen
clear_screen() {
printf "\033c"
}
# Function to display the dropdown list
display_dropdown() {
clear_screen
quickshell_label
echo "Select a command:"
for ((i=0; i<${#options[@]}; i++)); do
if [ $i -eq $selected_index ]; then
echo "> $(tput bold)${options[$i]}$(tput sgr0)"
else
echo " ${options[$i]}"
fi
done
}
remove_command() {
read -rsn1 key
case $key in
A) # Up arrow
((selected_index--))
;;
B) # Down arrow
((selected_index++))
;;
"") # Enter key
clear_screen
quickshell_label
echo "Running ${options[$selected_index]}"
echo ""
eval "${options[$selected_index]}"
exit 0
;;
esac
# Ensure selected index stays within bounds
if [ $selected_index -lt 0 ]; then
selected_index=$((${#options[@]} - 1))
elif [ $selected_index -ge ${#options[@]} ]; then
selected_index=0
fi
}
# Function to handle arrow key navigation
handle_input() {
display_dropdown
read -rsn1 key
case $key in
A) # Up arrow
((selected_index--))
;;
B) # Down arrow
((selected_index++))
;;
esac
# Ensure selected index stays within bounds
if [ $selected_index -lt 0 ]; then
selected_index=$((${#options[@]} - 1))
elif [ $selected_index -ge ${#options[@]} ]; then
selected_index=0
fi
}
execute_command() {
if [[ $key == "" ]]; then # Enter key
clear_screen
echo ""
echo "[${BRAND}] ${options[$selected_index]}"
eval "${options[$selected_index]}"
exit 0
break
fi
}
# Check for command line arguments for adding new commands
if [[ "$1" == "--add" || "$1" == "-a" ]]; then
read -p "[${BRAND}] Add your new command: " new_command
if grep -Fxq "$new_command" ~/.quickshell; then
echo "⚠️ Command is already registered."
else
echo "$new_command" >> ~/.quickshell
read_commands
echo "🚀 Command added!"
fi
exit 0
fi
# Check for command line arguments for removing commands
if [[ "$1" == "--remove" || "$1" == "-r" ]]; then
read_commands
while true; do
handle_input
if [[ $key == "" ]]; then # Enter key
command_to_remove="${options[$selected_index]}"
sed -i '' "/^$command_to_remove$/d" ~/.quickshell
echo "🗑️ Command removed!"
read_commands
break
fi
done
exit 0
fi
# Check for command line arguments for listing commands
if [[ "$1" == "--list" || "$1" == "-l" ]]; then
cat ~/.quickshell
exit 0
fi
# Edit .quickshell
if [[ "$1" == "--edit" || "$1" == "-e" ]]; then
nano ~/.quickshell
exit 0
fi
# Check for command line arguments for help
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
echo "${BRAND}"
echo ""
echo "${HEADLINE}"
echo ""
echo "Usage: quickshell | qsh"
echo ""
echo " --add, -a: Add a new command to the list."
echo ""
echo " --remove, -r: Remove a command from the list."
echo ""
echo " --list, -l: List all registered commands."
echo ""
echo " --edit, -e: Edit your command list in ~/.quickshell"
echo ""
echo " --help, -h: Show this help message."
exit 0
fi
# Main loop
while true; do
handle_input
execute_command
done