| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | IFS=$'\n\t' |
| 6 | |
| 7 | |
| 8 | rofi_theme_dir="$HOME/.config/rofi/themes" |
| 9 | rofi_config_file="$HOME/.config/rofi/config.rasi" |
| 10 | SED=$(which sed) |
| 11 | iDIR="$HOME/.config/swaync/images" |
| 12 | rofi_theme="$HOME/.config/rofi/config-rofi-theme.rasi" |
| 13 | |
| 14 | |
| 15 | menu() { |
| 16 | options=() |
| 17 | while IFS= read -r file; do |
| 18 | options+=("$file") |
| 19 | done < <(find -L "$rofi_theme_dir" -maxdepth 1 -type f -exec basename {} \; | sort -V) |
| 20 | |
| 21 | printf '%s\n' "${options[@]}" |
| 22 | } |
| 23 | |
| 24 | |
| 25 | add_theme_to_config() { |
| 26 | local theme_name="$1" |
| 27 | local theme_path="$rofi_theme_dir/$theme_name" |
| 28 | |
| 29 | |
| 30 | if [[ "$theme_path" == $HOME/* ]]; then |
| 31 | theme_path_with_tilde="~${theme_path#$HOME}" |
| 32 | else |
| 33 | theme_path_with_tilde="$theme_path" |
| 34 | fi |
| 35 | |
| 36 | |
| 37 | if ! grep -q '^\s*@theme' "$rofi_config_file"; then |
| 38 | echo -e "\n\n@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file" |
| 39 | echo "Added @theme \"$theme_path_with_tilde\" to $rofi_config_file" |
| 40 | else |
| 41 | $SED -i "s/^\(\s*@theme.*\)/\/\/\1/" "$rofi_config_file" |
| 42 | echo -e "@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file" |
| 43 | echo "Updated @theme line to $theme_path_with_tilde" |
| 44 | fi |
| 45 | |
| 46 | |
| 47 | max_line="9" |
| 48 | total_lines=$(grep -c '^\s*//@theme' "$rofi_config_file") |
| 49 | |
| 50 | if [ "$total_lines" -gt "$max_line" ]; then |
| 51 | excess=$((total_lines - max_line)) |
| 52 | |
| 53 | for i in $(seq 1 "$excess"); do |
| 54 | $SED -i '0,/^\s*\/\/@theme/ { /^\s*\/\/@theme/ {d; q; }}' "$rofi_config_file" |
| 55 | done |
| 56 | echo "Removed excess //@theme lines" |
| 57 | fi |
| 58 | } |
| 59 | |
| 60 | |
| 61 | main() { |
| 62 | choice=$(menu | rofi rofi -dmenu -i -config $rofi_theme) |
| 63 | |
| 64 | if [[ -z "$choice" ]]; then |
| 65 | exit 0 |
| 66 | fi |
| 67 | add_theme_to_config "$choice" |
| 68 | notify-send -i "$iDIR/ja.png" -u low 'Rofi Theme applied:' "$choice" |
| 69 | } |
| 70 | |
| 71 | if pgrep -x "rofi" >/dev/null; then |
| 72 | pkill rofi |
| 73 | fi |
| 74 | |
| 75 | main |