@@ -20,13 +20,16 @@ and requires administrative authentication to apply changes to the system.
2020- Requires administrative privileges to apply changes.
2121DOCUMENTATION
2222
23+ clear
24+
2325# Enable recursive directory expansion
2426shopt -s globstar
2527
2628theme_dirs=(" /usr/share/grub/themes" " /boot/grub/themes" )
2729theme_list=()
2830
2931declare -A theme_paths
32+ declare -A theme_previews
3033
3134# Search theme.txt files in subfolders of specified directories
3235for 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
4063done
@@ -45,31 +68,94 @@ if [[ ${#theme_list[@]} -eq 0 ]]; then
4568 exit 1
4669fi
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
73159fi
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
79165new_grub_theme=" GRUB_THEME=\" $selected_theme_path \" "
80166
81-
82167# Replace the theme in /etc/default/grub file (requires sudo)
83168echo -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"
98183else
99184 echo -e " \033[1;31mError updating file /etc/default/grub."
100185 exit 1
101- fi
186+ fi
0 commit comments