aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/scripts/RofiThemeSelector.sh
blob: 6fd8a6f8fd5bff6ae558b9653481bff723c3aa89 (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
#!/bin/bash
# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */  ##
# Script for adding a selected theme to the Rofi config

IFS=$'\n\t'

# Define directories and variables
rofi_theme_dir="$HOME/.config/rofi/themes"
rofi_config_file="$HOME/.config/rofi/config.rasi"
SED=$(which sed)
iDIR="$HOME/.config/swaync/images"
rofi_theme="$HOME/.config/rofi/config-rofi-theme.rasi"

# Function to display menu options
menu() {
    options=()
    while IFS= read -r file; do
        options+=("$file")
    done < <(find -L "$rofi_theme_dir" -maxdepth 1 -type f -exec basename {} \; | sort -V)

    printf '%s\n' "${options[@]}"
}

# Function to add or update theme in the config.rasi
add_theme_to_config() {
    local theme_name="$1"
    local theme_path="$rofi_theme_dir/$theme_name"
        
    # if config in $HOME to write as $HOME 
    if [[ "$theme_path" == $HOME/* ]]; then
        theme_path_with_tilde="~${theme_path#$HOME}"
    else
        theme_path_with_tilde="$theme_path"
    fi

    # If no @theme is in the file, add it
    if ! grep -q '^\s*@theme' "$rofi_config_file"; then
        echo -e "\n\n@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
        echo "Added @theme \"$theme_path_with_tilde\" to $rofi_config_file"
    else
        $SED -i "s/^\(\s*@theme.*\)/\/\/\1/" "$rofi_config_file"
        echo -e "@theme \"$theme_path_with_tilde\"" >> "$rofi_config_file"
        echo "Updated @theme line to $theme_path_with_tilde"
    fi

    # Ensure no more than max # of lines with //@theme lines
    max_line="9"
    total_lines=$(grep -c '^\s*//@theme' "$rofi_config_file")

    if [ "$total_lines" -gt "$max_line" ]; then
        excess=$((total_lines - max_line))
        # Remove the oldest or the very top //@theme lines
        for i in $(seq 1 "$excess"); do
            $SED -i '0,/^\s*\/\/@theme/ { /^\s*\/\/@theme/ {d; q; }}' "$rofi_config_file"
        done
        echo "Removed excess //@theme lines"
    fi
}

# Main function
main() {
    choice=$(menu | rofi rofi -dmenu -i -config $rofi_theme)

    if [[ -z "$choice" ]]; then
        exit 0
    fi
    add_theme_to_config "$choice"
    notify-send -i "$iDIR/ja.png" -u low 'Rofi Theme applied:' "$choice"
}

if pgrep -x "rofi" >/dev/null; then
    pkill rofi
fi

main
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage