irongit
hygoland/scripts/RofiThemeSelector-modified.sh
229 lines6.0 KBBourne Again Shell (bash)
1#!/bin/bash
2# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
3# A modified version of Rofi-Theme-Selector, concentrating only on ~/.local and also, applying only 10 @themes in ~/.config/rofi/config.rasi
4# as opposed to continous adding of //@theme
5
6# This code is released in public domain by Dave Davenport <qball@gmpclient.org>
7
8iDIR="$HOME/.config/swaync/images"
9
10
11OS="linux"
12
13ROFI=$(command -v rofi)
14SED=$(command -v sed)
15MKTEMP=$(command -v mktemp)
16NOTIFY_SEND=$(command -v notify-send)
17
18if [ -z "${SED}" ]
19then
20 echo "Did not find 'sed', script cannot continue."
21 exit 1
22fi
23if [ -z "${MKTEMP}" ]
24then
25 echo "Did not find 'mktemp', script cannot continue."
26 exit 1
27fi
28if [ -z "${ROFI}" ]
29then
30 echo "Did not find rofi, there is no point to continue."
31 exit 1
32fi
33if [ -z "${NOTIFY_SEND}" ]
34then
35 echo "Did not find 'notify-send', notifications won't work."
36fi
37
38TMP_CONFIG_FILE=$(${MKTEMP}).rasi
39#rofi_theme_dir="${HOME}/.local/share/rofi/themes"
40rofi_config_file="${XDG_CONFIG_HOME:-${HOME}/.config}/rofi/config.rasi"
41
42##
43# Array with parts to the found themes.
44# And array with the printable name.
45##
46declare -a themes
47declare -a theme_names
48
49##
50# Function that tries to find all installed rofi themes.
51# This fills in #themes array and formats a displayable string #theme_names
52##
53# Find themes in defined directories
54find_themes() {
55 directories=("$HOME/.local/share/rofi/themes" "$HOME/.config/rofi/themes")
56
57 for TD in "${directories[@]}"; do
58 if [ -d "$TD" ]; then
59 echo "Checking themes in: $TD"
60 for file in "$TD"/*.rasi; do
61 if [ -f "$file" ] && [ ! -L "$file" ]; then
62 themes+=("$file")
63 theme_names+=("$(basename "${file%.*}")")
64 else
65 echo "Skipping symlink: $file"
66 fi
67 done
68 else
69 echo "Directory does not exist: $TD"
70 fi
71 done
72}
73
74##
75# Function to add or update theme in the config.rasi
76##
77add_theme_to_config() {
78 local theme_name="$1"
79 local theme_path
80
81 # Determine the correct path for the theme
82 if [[ -f "$HOME/.local/share/rofi/themes/$theme_name.rasi" ]]; then
83 theme_path="$HOME/.local/share/rofi/themes/$theme_name.rasi"
84 elif [[ -f "$HOME/.config/rofi/themes/$theme_name.rasi" ]]; then
85 theme_path="$HOME/.config/rofi/themes/$theme_name.rasi"
86 else
87 echo "Theme not found: $theme_name"
88 return 1
89 fi
90
91 # Resolve symlinks if present
92 if [[ -L "$theme_path" ]]; then
93 theme_path=$(readlink -f "$theme_path")
94 fi
95
96 # Convert path to use ~ for home directory
97 theme_path_with_tilde="~${theme_path#$HOME}"
98
99 # Add or update @theme line in config
100 if ! grep -q '^\s*@theme' "$rofi_config_file"; then
101 echo -e "\n\n@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
102 echo "Added @theme \"$theme_path_with_tilde\" to $rofi_config_file"
103 else
104 $SED -i "s/^\(\s*@theme.*\)/\/\/\1/" "$rofi_config_file"
105 echo -e "@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
106 echo "Updated @theme line to $theme_path_with_tilde"
107 fi
108
109 # Limit the number of @theme lines to a maximum of 9
110 max_lines=9
111 total_lines=$(grep -c '^\s*//@theme' "$rofi_config_file")
112
113 if [ "$total_lines" -gt "$max_lines" ]; then
114 excess=$((total_lines - max_lines))
115 for i in $(seq 1 "$excess"); do
116 $SED -i '0,/^\s*\/\/@theme/ { /^\s*\/\/@theme/ {d; q; }}' "$rofi_config_file"
117 done
118 echo "Removed excess //@theme lines"
119 fi
120}
121
122##
123# Create a copy of rofi config
124##
125create_config_copy()
126{
127 ${ROFI} -dump-config > "${TMP_CONFIG_FILE}"
128 # remove theme entry.
129 ${SED} -i 's/^\s*theme:\s\+".*"\s*;//g' "${TMP_CONFIG_FILE}"
130}
131
132###
133# Print the list out so it can be displayed by rofi.
134##
135create_theme_list()
136{
137 OLDIFS=${IFS}
138 IFS='|'
139 for themen in ${theme_names[@]}
140 do
141 echo "${themen}"
142 done
143 IFS=${OLDIFS}
144}
145
146##
147# Thee indicate what entry is selected.
148##
149declare -i SELECTED
150
151select_theme()
152{
153 local MORE_FLAGS=(-dmenu -format i -no-custom -p "Theme" -markup -config "${TMP_CONFIG_FILE}" -i)
154 MORE_FLAGS+=(-kb-custom-1 "Alt-a")
155 MORE_FLAGS+=(-u 2,3 -a 4,5 )
156 local CUR="default"
157 while true
158 do
159 declare -i RTR
160 declare -i RES
161 local MESG="""You can preview themes by hitting <b>Enter</b>.
162<b>Alt-a</b> to accept the new theme.
163<b>Escape</b> to cancel
164Current theme: <b>${CUR}</b>
165<span weight=\"bold\" size=\"xx-small\">When setting a new theme this will override previous theme settings.
166Please update your config file if you have local modifications.</span>"""
167 THEME_FLAG=
168 if [ -n "${SELECTED}" ]
169 then
170 THEME_FLAG="-theme ${themes[${SELECTED}]}"
171 fi
172 RES=$( create_theme_list | ${ROFI} ${THEME_FLAG} ${MORE_FLAGS[@]} -cycle -selected-row "${SELECTED}" -mesg "${MESG}")
173 RTR=$?
174 if [ "${RTR}" = 10 ]
175 then
176 return 0;
177 elif [ "${RTR}" = 1 ]
178 then
179 return 1;
180 elif [ "${RTR}" = 65 ]
181 then
182 return 1;
183 fi
184 CUR=${theme_names[${RES}]}
185 SELECTED=${RES}
186 done
187}
188
189############################################################################################################
190# Actual program execution
191###########################################################################################################
192##
193# Find all themes
194##
195find_themes
196
197##
198# Do check if there are themes.
199##
200if [ ${#themes[@]} = 0 ]
201then
202 ${ROFI} -e "No themes found."
203 exit 0
204fi
205
206##
207# Create copy of config to play with in preview
208##
209create_config_copy
210
211##
212# Show the themes to user.
213##
214if select_theme && [ -n "${SELECTED}" ]
215then
216 # Apply the selected theme
217 add_theme_to_config "${theme_names[${SELECTED}]}"
218
219 # Send notification with the selected theme name
220 selection="${theme_names[${SELECTED}]}"
221 if [ -n "$NOTIFY_SEND" ]; then
222 notify-send -u low -i "$iDIR/ja.png" "Rofi Theme applied:" "$selection"
223 fi
224fi
225
226##
227# Remove temp. config.
228##
229rm -- "${TMP_CONFIG_FILE}"