mirror of
https://github.com/davatorium/rofi.git
synced 2025-02-24 15:56:25 -05:00
Make theme selector script a bit more robust
This commit is contained in:
parent
e53e9b73ba
commit
62434f0e8c
1 changed files with 84 additions and 23 deletions
|
@ -3,19 +3,47 @@
|
||||||
# This code is released in public domain by Dave Davenport <qball@gmpclient.org>
|
# This code is released in public domain by Dave Davenport <qball@gmpclient.org>
|
||||||
#
|
#
|
||||||
|
|
||||||
ROFI=rofi
|
|
||||||
ROFI_FLAGS=
|
|
||||||
TMP_CONFIG_FILE=$(mktemp)
|
|
||||||
|
|
||||||
|
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})
|
||||||
|
|
||||||
|
##
|
||||||
|
# Array with parts to the found themes.
|
||||||
|
# And array with the printable name.
|
||||||
|
##
|
||||||
declare -a themes
|
declare -a themes
|
||||||
declare -a theme_names
|
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()
|
function find_themes()
|
||||||
{
|
{
|
||||||
DIRS=${XDG_DATA_DIRS}
|
DIRS=${XDG_DATA_DIRS}
|
||||||
if [ -z "${XDG_DATA_DIRS}" ]
|
if [ -z "${XDG_DATA_DIRS}" ]
|
||||||
then
|
then
|
||||||
echo "XDG_DATA_DIRS needs to be set for this script to function."
|
echo "XDG_DATA_DIRS needs to be set for this script to function."
|
||||||
|
exit 1;
|
||||||
fi
|
fi
|
||||||
# Add user dir.
|
# Add user dir.
|
||||||
DIRS+="${HOME}/.local/share/"
|
DIRS+="${HOME}/.local/share/"
|
||||||
|
@ -27,16 +55,19 @@ function find_themes()
|
||||||
then
|
then
|
||||||
for file in ${TD}/*.theme
|
for file in ${TD}/*.theme
|
||||||
do
|
do
|
||||||
|
if [ -f "${file}" ]
|
||||||
|
then
|
||||||
themes+=(${file})
|
themes+=(${file})
|
||||||
FN=$(basename ${file})
|
FN=$(basename ${file})
|
||||||
NAME=${FN%.*}
|
NAME=${FN%.*}
|
||||||
USER=$(sed -n 's/^! User: \(.*\)/\1/p' ${file} )
|
USER=$(${SED} -n 's/^! User: \(.*\)/\1/p' ${file} )
|
||||||
if [ -z "${USER}" ]
|
if [ -z "${USER}" ]
|
||||||
then
|
then
|
||||||
theme_names+=(${NAME})
|
theme_names+=(${NAME})
|
||||||
else
|
else
|
||||||
theme_names+=("${NAME} by ${USER}")
|
theme_names+=("${NAME} by ${USER}")
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
@ -44,32 +75,43 @@ function find_themes()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
# Create a copy of rofi
|
||||||
|
##
|
||||||
function create_config_copy()
|
function create_config_copy()
|
||||||
{
|
{
|
||||||
${ROFI} -dump-xresources > ${TMP_CONFIG_FILE}
|
${ROFI} -dump-xresources > ${TMP_CONFIG_FILE}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
# Print the list out so it can be displayed by rofi.
|
||||||
|
##
|
||||||
function create_theme_list()
|
function create_theme_list()
|
||||||
{
|
{
|
||||||
OLDIFS=${IFS}
|
OLDIFS=${IFS}
|
||||||
IFS="""
|
IFS='|'
|
||||||
"""
|
|
||||||
for themen in ${theme_names[@]}
|
for themen in ${theme_names[@]}
|
||||||
do
|
do
|
||||||
echo ${themen}
|
echo ${themen}
|
||||||
done
|
done
|
||||||
IFS=${OLDIFS}
|
IFS=${OLDIFS}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
# Thee indicate what entry is selected.
|
||||||
|
##
|
||||||
declare -i SELECTED
|
declare -i SELECTED
|
||||||
CUR="default"
|
|
||||||
|
|
||||||
function select_theme ()
|
function select_theme ()
|
||||||
{
|
{
|
||||||
MORE_FLAGS=(-dmenu -format i -no-custom -p \"Theme\" -markup -config ${TMP_CONFIG_FILE})
|
local MORE_FLAGS=(-dmenu -format i -no-custom -p Theme -markup -config ${TMP_CONFIG_FILE})
|
||||||
MORE_FLAGS+=(-kb-custom-1 "Alt-a")
|
MORE_FLAGS+=(-kb-custom-1 "Alt-a")
|
||||||
|
local CUR="default"
|
||||||
while true
|
while true
|
||||||
do
|
do
|
||||||
MESG="""You can preview themes by hitting <b>Enter</b>.
|
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>Alt-a</b> to accept the new theme.
|
||||||
<b>Escape</b> to cancel
|
<b>Escape</b> to cancel
|
||||||
Current theme: <b>${CUR}</b>"""
|
Current theme: <b>${CUR}</b>"""
|
||||||
|
@ -82,13 +124,17 @@ Current theme: <b>${CUR}</b>"""
|
||||||
then
|
then
|
||||||
return 1;
|
return 1;
|
||||||
fi
|
fi
|
||||||
sed -i '/#include .*/d' ${TMP_CONFIG_FILE}
|
${SED} -i '/#include .*/d' ${TMP_CONFIG_FILE}
|
||||||
echo "#include \"${themes[${RES}]}\"" >> ${TMP_CONFIG_FILE}
|
echo "#include \"${themes[${RES}]}\"" >> ${TMP_CONFIG_FILE}
|
||||||
CUR=${theme_names[${RES}]}
|
CUR=${theme_names[${RES}]}
|
||||||
SELECTED=${RES}
|
SELECTED=${RES}
|
||||||
done
|
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()
|
function set_theme()
|
||||||
{
|
{
|
||||||
CDIR="${HOME}/.config/rofi/"
|
CDIR="${HOME}/.config/rofi/"
|
||||||
|
@ -98,11 +144,15 @@ function set_theme()
|
||||||
fi
|
fi
|
||||||
if [ -f "${CDIR}/config" ]
|
if [ -f "${CDIR}/config" ]
|
||||||
then
|
then
|
||||||
sed -i "/#include \".*\.theme\"$/d" "${CDIR}/config"
|
${SED} -i "/#include \".*\.theme\"$/d" "${CDIR}/config"
|
||||||
fi
|
fi
|
||||||
echo "#include \"${1}\"" >> "${CDIR}/config"
|
echo "#include \"${1}\"" >> "${CDIR}/config"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
############################################################################################################
|
||||||
|
# Actual program execution
|
||||||
|
###########################################################################################################
|
||||||
##
|
##
|
||||||
# Find all themes
|
# Find all themes
|
||||||
##
|
##
|
||||||
|
@ -111,16 +161,27 @@ find_themes
|
||||||
##
|
##
|
||||||
# Do check if there are 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
|
create_config_copy
|
||||||
|
|
||||||
##
|
##
|
||||||
# Show them to user.
|
# Show the themes to user.
|
||||||
##
|
##
|
||||||
if select_theme && [ -n "${SELECTED}" ]
|
if select_theme && [ -n "${SELECTED}" ]
|
||||||
then
|
then
|
||||||
echo "Selected: ${themes[${SELECTED}]}"
|
# Set theme
|
||||||
set_theme "${themes[${SELECTED}]}"
|
set_theme "${themes[${SELECTED}]}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
##
|
||||||
|
# Remove temp. config.
|
||||||
|
##
|
||||||
rm ${TMP_CONFIG_FILE}
|
rm ${TMP_CONFIG_FILE}
|
||||||
|
|
Loading…
Add table
Reference in a new issue