-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_shell_with_rofi
More file actions
executable file
·130 lines (106 loc) · 4.17 KB
/
help_shell_with_rofi
File metadata and controls
executable file
·130 lines (106 loc) · 4.17 KB
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
#!/bin/bash
# License: GPLv3
# Credits: Felipe Facundes
: <<'DOCUMENTATION'
Imagine creating helper functions and typing whatever you want in the terminal, just creating a
simple function instead of an entire script—this is the premise of help_shell.
The help_shell script is an efficient and user-friendly tool designed to dynamically
display help information for your Bash functions.
Define your functions below. Add as many functions as you like in a similar manner
- Dynamic Help Display: Automatically lists all available functions with descriptions extracted from comments.
- Interactive Search: Search for functions by name or description using a clean and intuitive menu powered by whiptail.
- Flexible Usage: Allows for specifying functions as command-line arguments or interacting via the dynamic menu.
- Integration Ready: Seamlessly integrates with the shell_utils framework, leveraging pre-defined variables and resources.
DOCUMENTATION
shell_utils=~/.shell_utils
helps_dir="${shell_utils}/scripts/helps"
shopt -s globstar
source "${shell_utils}/variables/colors_shell.sh"
for file in "${helps_dir}"/**/*.sh; do
source "${file}"
done
show_documentation() {
awk '
BEGIN { inside_block = 0 }
# Check the beginning of the DOCUMENTATION block
/: <<'\''DOCUMENTATION'\''/ { inside_block = 1; next }
# Check the end of the DOCUMENTATION block
inside_block && $0 == "DOCUMENTATION" { inside_block = 0; exit }
# Print lines within the DOCUMENTATION block
inside_block { print }
' "$0"
}
# Display help dynamically
help() {
echo -e "$(show_documentation | head -7)\n"
echo "Usage: ${0##*/} [function1 function2 ...]"
echo -e "\n-h, --help,"
echo -e "
# Display this help message.
"
echo -e "Available functions:\n"
for func_name in $(declare -F | cut -d " " -f 3); do
# Extract the first comment line of the function
description=$(type "$func_name" | grep -m 1 '^#') # Or | awk '/^#/ {print; exit}' or | sed -n '/^#/p' | head -n 1
# Remove the '#' character from the description
description=${description/#\# /}
echo -e " ${func_name} <=> $description"
done
echo -e "\nEnter q to quit this help.\n"
}
# Check if help option was provided
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
help | less -i -R
exit 0
fi
if [[ -z "$1" ]]; then
# Initialize an empty array to hold the menu items
menu_items=()
if [[ $XDG_SESSION_TYPE = wayland ]]; then
menu_selector='wofi --show dmenu -p'
else
menu_selector='rofi -dmenu -i -p'
fi
if pidof wofi; then
exit 1
fi
for func_name in $(declare -F | cut -d " " -f 3); do
# Extract the first comment line of the function
description=$(type "$func_name" | grep -m 1 '^#') # Or | awk '/^#/ {print; exit}' or | sed -n '/^#/p' | head -n 1
# Remove the '#' character from the description
description=${description/#\# /}
# Add the function name and description to the menu items
menu_items+=("${func_name} <=> $description")
done
# Pass the menu items to Rofi or Wofi and get the selected item
selected_item=$(printf '%s\n' "${menu_items[@]}" | eval "$menu_selector" \"Select a help function:\")
# Use awk to extract the function name from the selected item
func_name=$(echo "$selected_item" | awk '{print $1}')
# Check if the selected item is a function
if declare -f "$func_name" > /dev/null; then
if [ -t 1 ]; then
clear
# Call the function
"$(printf '%s\n' "$func_name")"
read -p "Press enter to continue"
else
text='Run this script as a command directly in the terminal'
notify-send "$text"; sleep 5; notify-send "$text"
fi
else
echo "Function '$func_name' not found."
fi
exit 0
fi
# Iterate over the passed arguments
for func_name in "$@"; do
# Check if the function exists
if declare -f "$func_name" > /dev/null; then
# Call the function
"$(printf '%s\n' "$func_name")"
else
echo "Function '$func_name' not found."
fi
done
# Example script invocation:
# ./your_script.sh function1 function2