Skip to content

Commit 7bac310

Browse files
A Dynamic Collection of Shell Scripts with Educational Purpose
1 parent 43b4e1f commit 7bac310

3 files changed

Lines changed: 212 additions & 26 deletions

File tree

scripts/fontpreview

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ elif [[ -z "$output_file" ]]; then
326326
--prompt="🔍 Select a font: " \
327327
--preview="magick -size \"$SIZE\" -background \"$BG_COLOR\" -fill \"$FG_COLOR\" -font '{}' \
328328
-pointsize \"$FONT_SIZE\" label:\"$PREVIEW_TEXT\" -geometry \"$SIZE\" ${cmd[*]}" \
329-
--preview-window="right:${FZF_MARGIN_R}%:border-left" \
329+
--preview-window="right:${FZF_MARGIN_R}%:wrap" \
330330
--height="${FZF_MARGIN_H}"%)
331331
# If a font was selected, show confirmation
332332
if [[ -n "$selected_font" ]]; then

scripts/grub_theme_selector

Lines changed: 110 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ and requires administrative authentication to apply changes to the system.
2020
- Requires administrative privileges to apply changes.
2121
DOCUMENTATION
2222

23+
clear
24+
2325
# Enable recursive directory expansion
2426
shopt -s globstar
2527

2628
theme_dirs=("/usr/share/grub/themes" "/boot/grub/themes")
2729
theme_list=()
2830

2931
declare -A theme_paths
32+
declare -A theme_previews
3033

3134
# Search theme.txt files in subfolders of specified directories
3235
for dir in "${theme_dirs[@]}"; do
@@ -35,6 +38,26 @@ for dir in "${theme_dirs[@]}"; do
3538
theme_name="$(basename "$(dirname "$theme_file")")"
3639
theme_list+=("$theme_name")
3740
theme_paths["$theme_name"]="$theme_file"
41+
42+
# Find preview image for the theme
43+
theme_dir=$(dirname "$theme_file")
44+
preview_file=""
45+
46+
# Check for possible preview image files
47+
for file in "Preview.png" "preview.png" "PREVIEW.png" \
48+
"Preview.jpg" "preview.jpg" "PREVIEW.jpg"; do
49+
if [[ -f "$theme_dir/$file" ]]; then
50+
preview_file="$theme_dir/$file"
51+
break
52+
fi
53+
done
54+
55+
# Fallback to background.png if no preview found
56+
if [[ -z "$preview_file" && -f "$theme_dir/background.png" ]]; then
57+
preview_file="$theme_dir/background.png"
58+
fi
59+
60+
theme_previews["$theme_name"]="$preview_file"
3861
fi
3962
done
4063
done
@@ -45,31 +68,94 @@ if [[ ${#theme_list[@]} -eq 0 ]]; then
4568
exit 1
4669
fi
4770

48-
# Create a list of options for Whiptail
49-
whiptail_options=()
50-
for theme in "${theme_list[@]}"; do
51-
# Extract only the main directory where the theme was found
52-
theme_dir=$(dirname "${theme_paths[$theme]}")
53-
if [[ "$theme_dir" == /usr/share/grub/themes/* ]]; then
54-
location="/usr/share/grub/themes"
55-
elif [[ "$theme_dir" == /boot/grub/themes/* ]]; then
56-
location="/boot/grub/themes"
71+
# Function to check sixel support
72+
check_sixel_support() {
73+
hassixel=""
74+
IFS=";?c" read -ra REPLY -s -t 1 -d "c" -p $'\e[c' >&2
75+
for code in "${REPLY[@]}"; do
76+
if [[ $code == "4" ]]; then
77+
hassixel=yup
78+
break
79+
fi
80+
done
81+
82+
# YAFT is vt102 compatible, cannot respond to vt220 escape sequence.
83+
if [[ "$TERM" == yaft* ]]; then hassixel=yeah; fi
84+
85+
if [[ -z "$hassixel" && -z "$LSIX_FORCE_SIXEL_SUPPORT" ]]; then
86+
return 1
5787
else
58-
location="Unknown directory"
88+
return 0
89+
fi
90+
}
91+
92+
# Use FZF if available
93+
if command -v fzf >/dev/null; then
94+
# Prepare the preview command
95+
if check_sixel_support; then
96+
preview_cmd="magick '{1}' -resize 800x600 sixel:- 2>/dev/null || echo 'No preview available'"
97+
elif command -v viu >/dev/null; then
98+
preview_cmd="viu '{1}' 2>/dev/null || echo 'No preview available'"
99+
elif command -v feh >/dev/null; then
100+
preview_cmd="feh '{1}' 2>/dev/null || echo 'No preview available'"
101+
else
102+
preview_cmd="echo 'No image viewer available'"
59103
fi
60-
whiptail_options+=("$theme" "$location")
61-
done
62104

63-
# Display the menu using Whiptail
64-
selected_theme=$(whiptail --title "GRUB Theme Selection" \
65-
--menu "Choose a theme for GRUB:" 20 78 10 \
66-
"${whiptail_options[@]}" \
67-
3>&1 1>&2 2>&3)
105+
# Generate data for fzf in the format: preview_file|theme_name
106+
fzf_data=()
107+
for theme in "${theme_list[@]}"; do
108+
preview_file="${theme_previews[$theme]}"
109+
if [[ -f "$preview_file" ]]; then
110+
fzf_data+=("$preview_file|$theme")
111+
else
112+
fzf_data+=("|$theme (no preview)")
113+
fi
114+
done
68115

69-
# Checks if the user has canceled the selection
70-
if [[ $? -ne 0 ]]; then
71-
echo "Operation canceled by user."
72-
exit 1
116+
# Display FZF interface
117+
selected_item=$(printf "%s\n" "${fzf_data[@]}" | \
118+
fzf --delimiter='|' --with-nth=2 \
119+
--prompt="🔍 Select a Grub Theme: " \
120+
--height=90% --info=inline \
121+
--preview "$preview_cmd" \
122+
--preview-window=right:70%:wrap)
123+
124+
# Extract the selected theme name
125+
if [[ -n "$selected_item" ]]; then
126+
selected_theme=$(echo "$selected_item" | cut -d'|' -f2 | sed 's/ (no preview)//')
127+
else
128+
echo "Operation canceled by user."
129+
exit 1
130+
fi
131+
else
132+
# Fallback to Whiptail
133+
# Create a list of options for Whiptail
134+
whiptail_options=()
135+
for theme in "${theme_list[@]}"; do
136+
# Extract only the main directory where the theme was found
137+
theme_dir=$(dirname "${theme_paths[$theme]}")
138+
if [[ "$theme_dir" == /usr/share/grub/themes/* ]]; then
139+
location="/usr/share/grub/themes"
140+
elif [[ "$theme_dir" == /boot/grub/themes/* ]]; then
141+
location="/boot/grub/themes"
142+
else
143+
location="Unknown directory"
144+
fi
145+
whiptail_options+=("$theme" "$location")
146+
done
147+
148+
# Display the menu using Whiptail
149+
selected_theme=$(whiptail --title "GRUB Theme Selection" \
150+
--menu "Choose a theme for GRUB:" 20 78 10 \
151+
"${whiptail_options[@]}" \
152+
3>&1 1>&2 2>&3)
153+
154+
# Checks if the user has canceled the selection
155+
if [[ $? -ne 0 ]]; then
156+
echo "Operation canceled by user."
157+
exit 1
158+
fi
73159
fi
74160

75161
# Gets the full path of the selected theme
@@ -78,7 +164,6 @@ selected_theme_path="${theme_paths[$selected_theme]}"
78164
# Sets the new value of GRUB_THEME
79165
new_grub_theme="GRUB_THEME=\"$selected_theme_path\""
80166

81-
82167
# Replace the theme in /etc/default/grub file (requires sudo)
83168
echo -e "\nProvide admin password for sudo.\n"
84169

@@ -91,11 +176,11 @@ sudo sed -i \
91176
-e "s|^[[:space:]]*#\{0,1\}[[:space:]]*GRUB_THEME=.*|$new_grub_theme|" \
92177
/etc/default/grub
93178

94-
if sudo grub-mkconfig -o /boot/grub/grub.cfg; then
179+
if sudo grub-mkconfig -o /boot/grub/grub.cfg; then
95180
echo -e "\n\033[1;32mGRUB theme successfully updated to $selected_theme."
96-
echo -e "\033[1;33mLocated in $(cat /etc/default/grub | grep GRUB_THEME=)"
181+
echo -e "\033[1;33mLocated in $(grep GRUB_THEME= /etc/default/grub)"
97182
echo -e "\033[1;32mRun 'update-grub' to apply the changes. If it has not been applied.\033[0m"
98183
else
99184
echo -e "\033[1;31mError updating file /etc/default/grub."
100185
exit 1
101-
fi
186+
fi

scripts/grub_theme_selector.copy

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash
2+
# License: GPLv3
3+
# Credits: Felipe Facundes
4+
5+
: <<'DOCUMENTATION'
6+
This Bash script aims to manage GRUB themes in an automated and interactive way.
7+
It searches for 'theme.txt' files in predefined directories such as
8+
'/usr/share/grub/themes' and '/boot/grub/themes',
9+
using the recursive directory expansion enabled by 'shopt -s globstar'.
10+
After identifying the available themes, it presents a friendly selection interface via Whiptail,
11+
allowing the user to choose a theme. The script safely updates the '/etc/default/grub' file,
12+
setting the new 'GRUB_THEME' variable and regenerating the GRUB configuration file with 'grub-mkconfig'.
13+
It includes robust checks to ensure the integrity of operations
14+
and requires administrative authentication to apply changes to the system.
15+
16+
## Features
17+
- Automatically detects available GRUB themes in common directories.
18+
- Presents an interactive menu using Whiptail.
19+
- Updates the GRUB configuration securely.
20+
- Requires administrative privileges to apply changes.
21+
DOCUMENTATION
22+
23+
# Enable recursive directory expansion
24+
shopt -s globstar
25+
26+
theme_dirs=("/usr/share/grub/themes" "/boot/grub/themes")
27+
theme_list=()
28+
29+
declare -A theme_paths
30+
31+
# Search theme.txt files in subfolders of specified directories
32+
for dir in "${theme_dirs[@]}"; do
33+
for theme_file in "$dir"/**/theme.txt; do
34+
if [[ -f "$theme_file" ]]; then
35+
theme_name="$(basename "$(dirname "$theme_file")")"
36+
theme_list+=("$theme_name")
37+
theme_paths["$theme_name"]="$theme_file"
38+
fi
39+
done
40+
done
41+
42+
# Check if themes were found
43+
if [[ ${#theme_list[@]} -eq 0 ]]; then
44+
echo "No themes found in directories ${theme_dirs[*]}"
45+
exit 1
46+
fi
47+
48+
# Create a list of options for Whiptail
49+
whiptail_options=()
50+
for theme in "${theme_list[@]}"; do
51+
# Extract only the main directory where the theme was found
52+
theme_dir=$(dirname "${theme_paths[$theme]}")
53+
if [[ "$theme_dir" == /usr/share/grub/themes/* ]]; then
54+
location="/usr/share/grub/themes"
55+
elif [[ "$theme_dir" == /boot/grub/themes/* ]]; then
56+
location="/boot/grub/themes"
57+
else
58+
location="Unknown directory"
59+
fi
60+
whiptail_options+=("$theme" "$location")
61+
done
62+
63+
# Display the menu using Whiptail
64+
selected_theme=$(whiptail --title "GRUB Theme Selection" \
65+
--menu "Choose a theme for GRUB:" 20 78 10 \
66+
"${whiptail_options[@]}" \
67+
3>&1 1>&2 2>&3)
68+
69+
# Checks if the user has canceled the selection
70+
if [[ $? -ne 0 ]]; then
71+
echo "Operation canceled by user."
72+
exit 1
73+
fi
74+
75+
# Gets the full path of the selected theme
76+
selected_theme_path="${theme_paths[$selected_theme]}"
77+
78+
# Sets the new value of GRUB_THEME
79+
new_grub_theme="GRUB_THEME=\"$selected_theme_path\""
80+
81+
82+
# Replace the theme in /etc/default/grub file (requires sudo)
83+
echo -e "\nProvide admin password for sudo.\n"
84+
85+
if ! sudo -v; then
86+
echo "Incorrect password or authentication failed."
87+
exit 1
88+
fi
89+
90+
sudo sed -i \
91+
-e "s|^[[:space:]]*#\{0,1\}[[:space:]]*GRUB_THEME=.*|$new_grub_theme|" \
92+
/etc/default/grub
93+
94+
if sudo grub-mkconfig -o /boot/grub/grub.cfg; then
95+
echo -e "\n\033[1;32mGRUB theme successfully updated to $selected_theme."
96+
echo -e "\033[1;33mLocated in $(cat /etc/default/grub | grep GRUB_THEME=)"
97+
echo -e "\033[1;32mRun 'update-grub' to apply the changes. If it has not been applied.\033[0m"
98+
else
99+
echo -e "\033[1;31mError updating file /etc/default/grub."
100+
exit 1
101+
fi

0 commit comments

Comments
 (0)