summaryrefslogtreecommitdiffstats
path: root/script/rofi-theme-selector
blob: 8dba95302ed59366aab38ad8a1b721495f77fae6 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env bash
#
# This code is released in public domain by Dave Davenport <qball@gmpclient.org> 
#


ROFI=$(which rofi)
SED=$(which sed)
MKTEMP=$(which mktemp)

if [ -z "${SED}" ]
then
    echo "Did not find 'sed', script cannot continue."
    exit 1
fi
if [ -z "${MKTEMP}" ]
then
    echo "Did not find 'mktemp', script cannot continue."
    exit 1
fi
if [ -z "${ROFI}" ]
then
    echo "Did not find rofi, there is no point to continue."
    exit 1
fi

TMP_CONFIG_FILE=$(${MKTEMP}).rasi

##
# Array with parts to the found themes.
# And array with the printable name.
##
declare -a themes
declare -a theme_names

##
# Function that tries to find all installed rofi themes.
# This fills in #themes array and formats a displayable string #theme_names
##
function find_themes()
{
    DIRS=${XDG_DATA_DIRS}
    OLDIFS=${IFS}
    IFS=:
    if [ -z "${XDG_DATA_DIRS}" ]
    then
        echo "XDG_DATA_DIRS needs to be set for this script to function correctly."
        echo -n "Using dirs from \$PATH: "
        DIRS=
        # Iterate over items in $PATH
        for p in ${PATH}; do
            # Remove trailing / if exists.
            x=${p%/}
            # remove both /bin and /sbin and /games from end
            x=${x%/bin}
            x=${x%/sbin}
            x=${x%/games}
            # Add /share
            x=${x}/share
            # Check if entry exists Prepend : so :${x}: matches nicely
            case ":${DIRS}" in
                *$x:*);;
                *) DIRS+="$x:";;
            esac
        done
        # Remove trailing :
        DIRS=${DIRS%:}
        echo "${DIRS}"
    fi
    # Add user dir.
    DIRS+=":${XDG_DATA_HOME:-${HOME}/.local/share}"
    DIRS+=":${XDG_CONFIG_HOME:-${HOME}/.config}"
    for p in ${DIRS}; do 
    	p=${p%/}
        TD=${p}/rofi/themes
        if [ -n "${p}" ] && [ -d "${TD}" ]
        then
            echo "Checking themes in: ${TD}"
            for file in ${TD}/*.rasi
            do
                if [ -f "${file}" ]
                then
                    themes+=(${file})
                    FN=$(basename ${file})
                    NAME=${FN%.*}
                    USER=$(${SED} -n 's/^.*User: \(.*\)/\1/p' ${file} | head -n 1 )
                    if [ -z "${USER}" ]
                    then
                        theme_names+=(${NAME})
                    else
                        theme_names+=("${NAME} by ${USER}")
                    fi
                fi
           done
        fi
    done
    IFS=${OLDIFS}

}

##
# Create a copy of rofi
##
function create_config_copy()
{
    ${ROFI} -dump-config > ${TMP_CONFIG_FILE}
    # remove theme entry.
    sed -i 's/^\s*theme:\s\+".*"\s*;//g' ${TMP_CONFIG_FILE}
}

###
# Print the list out so it can be displayed by rofi.
##
function create_theme_list()
{
    OLDIFS=${IFS}
    IFS='|'
    for themen in  ${theme_names[@]}
    do
        echo ${themen}
    done
    IFS=${OLDIFS}
}

##
# Thee indicate what entry is selected.
##
declare -i SELECTED

function select_theme () 
{
    local MORE_FLAGS=(-dmenu -format i -no-custom -p "Theme" -markup -config ${TMP_CONFIG_FILE} -i)
    MORE_FLAGS+=(-kb-custom-1 "Alt-a")
    MORE_FLAGS+=(-u 2,3 -a 4,5 )
    local CUR="default"
    while true
    do
        declare -i RTR
        declare -i RES
        local MESG="""You can preview themes by hitting <b>Enter</b>.
<b>Alt-a</b> to accept the new theme.
<b>Escape</b> to cancel
Current theme: <b>${CUR}</b>"""
        THEME_FLAG=
        if [ -n "${SELECTED}" ]
        then 
			THEME_FLAG="-theme ${themes[${SELECTED}]}"
        fi
		RES=$( create_theme_list | ${ROFI} ${THEME_FLAG} ${MORE_FLAGS[@]} -cycle -selected-row "${SELECTED}" -mesg "${MESG}")
        RTR=$?
        if [ ${RTR} = 10 ]
        then
            return 0;
        elif [ ${RTR} = 1 ]
        then
            return 1;
        elif [ ${RTR} = 65 ]
        then
            return 1;
        fi
        CUR=${theme_names[${RES}]}
		SELECTED=${RES}
    done
}

###
# Create if not exists, then removes #include of .theme file (if present) and add the selected theme to the end.
# Repeated calls should leave the config clean-ish
###
function set_theme()
{
    CDIR="${XDG_CONFIG_HOME:-${HOME}/.config}/rofi"
    if [ ! -d "${CDIR}" ]
    then
        mkdir -p ${CDIR}
    fi
    if [ -f "${CDIR}/config.rasi" ]
    then
    	get_link=$(readlink -f "${CDIR}/config.rasi")
        ${SED} -i "/@import.*/d" "${get_link}"
        echo "@import \"${1}\"" >> "${get_link}"
    else
        if [ ! -e "${CDIR}/config" ]
        then
            # No file.
            echo "rofi.theme: ${1}" >> "${CDIR}/config"
        elif [ -f "${CDIR}/config" ]
        then
            # Regular file
    		get_link=$(readlink -f "${CDIR}/config")
            ${SED} -i "/rofi\.theme: .*\.rasi$/d" "${get_link}"
            echo "rofi.theme: ${1}" >> "${get_link}"
        fi
	fi
}

############################################################################################################
# Actual program execution
###########################################################################################################
##
# Find all themes
##
find_themes

##
# Do check if there are themes.
##
if [ ${#themes[@]} = 0 ]
then
    ${ROFI} -e "No themes found."
    exit 0
fi

##
# Create copy of config to play with in preview
##
create_config_copy

##
# Show the themes to user.
##
if select_theme && [ -n "${SELECTED}" ]
then
    # Set theme
    set_theme "${themes[${SELECTED}]}"
fi

##
# Remove temp. config.
##
rm ${TMP_CONFIG_FILE}