blob: db723b6f7b508ad4d4e0f564563c2b4434bc2b27 (
plain) (
blame)
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
|
#!/usr/bin/env bash
# ==================================================
# KoolDots (2026)
# Project URL: https://github.com/LinuxBeginnings
# License: GNU GPLv3
# SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# Rofi Themes - Script to preview and apply themes by live-reloading the config.
# --- Configuration ---
ROFI_THEMES_DIR_CONFIG="$HOME/.config/rofi/themes"
ROFI_THEMES_DIR_LOCAL="$HOME/.local/share/rofi/themes"
ROFI_CONFIG_FILE="$HOME/.config/rofi/config.rasi"
ROFI_THEME_FOR_THIS_SCRIPT="$HOME/.config/rofi/config-rofi-theme.rasi" # A separate rofi theme for the picker itself
IDIR="$HOME/.config/swaync/images" # For notifications
# --- Helper Functions ---
# Function to send a notification
notify_user() {
notify-send -u low -i "$1" "$2" "$3"
}
# Function to apply the selected rofi theme to the main config file
apply_rofi_theme_to_config() {
local theme_name_to_apply="$1"
# Find the full path of the theme file
local theme_path
if [[ -f "$ROFI_THEMES_DIR_CONFIG/$theme_name_to_apply" ]]; then
theme_path="$ROFI_THEMES_DIR_CONFIG/$theme_name_to_apply"
elif [[ -f "$ROFI_THEMES_DIR_LOCAL/$theme_name_to_apply" ]]; then
theme_path="$ROFI_THEMES_DIR_LOCAL/$theme_name_to_apply"
else
notify_user "$IDIR/error.png" "Error" "Theme file not found: $theme_name_to_apply"
return 1
fi
# Use ~ for the home directory in the config path
local theme_path_with_tilde="~${theme_path#$HOME}"
# Create a temporary file to safely edit the config
local temp_rofi_config_file
temp_rofi_config_file=$(mktemp)
cp "$ROFI_CONFIG_FILE" "$temp_rofi_config_file"
# Comment out any existing @theme entry
sed -i -E 's/^(\s*@theme)/\\/\\/\1/' "$temp_rofi_config_file"
# Add the new @theme entry at the end of the file
echo "@theme \"$theme_path_with_tilde\"" >>"$temp_rofi_config_file"
# Overwrite the original config file
cp "$temp_rofi_config_file" "$ROFI_CONFIG_FILE"
rm "$temp_rofi_config_file"
# Prune old commented-out theme lines to prevent clutter
local max_lines=10
local total_lines=$(grep -c '^//\s*@theme' "$ROFI_CONFIG_FILE")
if [ "$total_lines" -gt "$max_lines" ]; then
local excess=$((total_lines - max_lines))
for ((i = 1; i <= excess; i++)); do
sed -i '0,/^\s*\/\/@theme/s///' "$ROFI_CONFIG_FILE"
done
fi
return 0
}
# --- Main Script Execution ---
# Check for required directories and files
if [ ! -d "$ROFI_THEMES_DIR_CONFIG" ] && [ ! -d "$ROFI_THEMES_DIR_LOCAL" ]; then
notify_user "$IDIR/error.png" "E-R-R-O-R" "No Rofi themes directory found."
exit 1
fi
if [ ! -f "$ROFI_CONFIG_FILE" ]; then
notify_user "$IDIR/error.png" "E-R-R-O-R" "Rofi config file not found: $ROFI_CONFIG_FILE"
exit 1
fi
# Backup the original config content
original_rofi_config_content_backup=$(cat "$ROFI_CONFIG_FILE")
# Generate a sorted list of available theme file names
mapfile -t available_theme_names < <((
find "$ROFI_THEMES_DIR_CONFIG" -maxdepth 1 -name "*.rasi" -type f -printf "%f\n" 2>/dev/null
find "$ROFI_THEMES_DIR_LOCAL" -maxdepth 1 -name "*.rasi" -type f -printf "%f\n" 2>/dev/null
) | sort -V -u)
if [ ${#available_theme_names[@]} -eq 0 ]; then
notify_user "$IDIR/error.png" "No Rofi Themes" "No .rasi files found in theme directories."
exit 1
fi
# Find the currently active theme to set as the initial selection
current_selection_index=0
current_active_theme_path=$(grep -oP '^\s*@theme\s*"\K[^"]+' "$ROFI_CONFIG_FILE" | tail -n 1)
if [ -n "$current_active_theme_path" ]; then
current_active_theme_name=$(basename "$current_active_theme_path")
for i in "${!available_theme_names[@]}"; do
if [[ "${available_theme_names[$i]}" == "$current_active_theme_name" ]]; then
current_selection_index=$i
break
fi
done
fi
# Main preview loop
while true; do
theme_to_preview_now="${available_theme_names[$current_selection_index]}"
# Apply the theme for preview
if ! apply_rofi_theme_to_config "$theme_to_preview_now"; then
echo "$original_rofi_config_content_backup" >"$ROFI_CONFIG_FILE"
notify_user "$IDIR/error.png" "Preview Error" "Failed to apply $theme_to_preview_now. Reverted."
exit 1
fi
# Prepare theme list for Rofi
rofi_input_list=""
for theme_name_in_list in "${available_theme_names[@]}"; do
rofi_input_list+="$(basename "$theme_name_in_list" .rasi)\n"
done
rofi_input_list_trimmed="${rofi_input_list%\\n}"
# Launch Rofi and get user's choice
chosen_index_from_rofi=$(echo -e "$rofi_input_list_trimmed" |
rofi -dmenu -i \
-format 'i' \
-p "Rofi Theme" \
-mesg "‼️ **note** ‼️ Enter: Preview || Ctrl+S: Apply & Exit || Esc: Cancel" \
-config "$ROFI_THEME_FOR_THIS_SCRIPT" \
-selected-row "$current_selection_index" \
-kb-custom-1 "Control+s")
rofi_exit_code=$?
# Handle Rofi's exit code
if [ $rofi_exit_code -eq 0 ]; then # Enter
if [[ "$chosen_index_from_rofi" =~ ^[0-9]+$ ]] && [ "$chosen_index_from_rofi" -lt "${#available_theme_names[@]}" ]; then
current_selection_index="$chosen_index_from_rofi"
fi
elif [ $rofi_exit_code -eq 1 ]; then # Escape
notify_user "$IDIR/note.png" "Rofi Theme" "Selection cancelled. Reverting to original theme."
echo "$original_rofi_config_content_backup" >"$ROFI_CONFIG_FILE"
break
elif [ $rofi_exit_code -eq 10 ]; then # Custom bind 1 (Ctrl+S)
notify_user "$IDIR/ja.png" "Rofi Theme Applied" "$(basename "$theme_to_preview_now" .rasi)"
break
else # Error or unexpected exit code
notify_user "$IDIR/error.png" "Rofi Error" "Unexpected Rofi exit ($rofi_exit_code). Reverting."
echo "$original_rofi_config_content_backup" >"$ROFI_CONFIG_FILE"
break
fi
done
exit 0
|