2016-10-27 10:41:49 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# This code is released in public domain by Dave Davenport <qball@gmpclient.org>
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
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.
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
declare -a themes
|
|
|
|
declare -a theme_names
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
##
|
|
|
|
# Function that tries to find all installed rofi themes.
|
|
|
|
# This fills in #themes array and formats a displayable string #theme_names
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
function find_themes()
|
|
|
|
{
|
|
|
|
DIRS=${XDG_DATA_DIRS}
|
|
|
|
if [ -z "${XDG_DATA_DIRS}" ]
|
|
|
|
then
|
|
|
|
echo "XDG_DATA_DIRS needs to be set for this script to function."
|
2016-10-27 13:02:21 -04:00
|
|
|
exit 1;
|
2016-10-27 10:41:49 -04:00
|
|
|
fi
|
|
|
|
# Add user dir.
|
2016-10-29 05:13:28 -04:00
|
|
|
DIRS+=":${HOME}/.local/share/"
|
2016-10-27 10:41:49 -04:00
|
|
|
OLDIFS=${IFS}
|
|
|
|
IFS=:
|
|
|
|
for p in ${DIRS}; do
|
|
|
|
TD=${p}rofi/themes/
|
2016-10-29 05:13:28 -04:00
|
|
|
if [ -n "${p}" ] && [ -d "${TD}" ]
|
2016-10-27 10:41:49 -04:00
|
|
|
then
|
|
|
|
for file in ${TD}/*.theme
|
|
|
|
do
|
2016-10-27 13:02:21 -04:00
|
|
|
if [ -f "${file}" ]
|
2016-10-27 10:41:49 -04:00
|
|
|
then
|
2016-10-27 13:02:21 -04:00
|
|
|
themes+=(${file})
|
|
|
|
FN=$(basename ${file})
|
|
|
|
NAME=${FN%.*}
|
|
|
|
USER=$(${SED} -n 's/^! User: \(.*\)/\1/p' ${file} )
|
|
|
|
if [ -z "${USER}" ]
|
|
|
|
then
|
|
|
|
theme_names+=(${NAME})
|
|
|
|
else
|
|
|
|
theme_names+=("${NAME} by ${USER}")
|
|
|
|
fi
|
2016-10-27 10:41:49 -04:00
|
|
|
fi
|
2016-10-27 13:02:21 -04:00
|
|
|
done
|
|
|
|
fi
|
2016-10-27 10:41:49 -04:00
|
|
|
done
|
|
|
|
IFS=${OLDIFS}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
##
|
|
|
|
# Create a copy of rofi
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
function create_config_copy()
|
|
|
|
{
|
|
|
|
${ROFI} -dump-xresources > ${TMP_CONFIG_FILE}
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
###
|
|
|
|
# Print the list out so it can be displayed by rofi.
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
function create_theme_list()
|
|
|
|
{
|
|
|
|
OLDIFS=${IFS}
|
2016-10-27 13:02:21 -04:00
|
|
|
IFS='|'
|
2016-10-27 10:41:49 -04:00
|
|
|
for themen in ${theme_names[@]}
|
|
|
|
do
|
|
|
|
echo ${themen}
|
|
|
|
done
|
|
|
|
IFS=${OLDIFS}
|
|
|
|
}
|
2016-10-27 13:02:21 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Thee indicate what entry is selected.
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
declare -i SELECTED
|
|
|
|
|
|
|
|
function select_theme ()
|
|
|
|
{
|
2016-10-27 15:44:13 -04:00
|
|
|
local MORE_FLAGS=(-dmenu -format i -no-custom -p Theme -markup -config ${TMP_CONFIG_FILE} -i)
|
2016-10-27 10:41:49 -04:00
|
|
|
MORE_FLAGS+=(-kb-custom-1 "Alt-a")
|
2016-11-16 03:22:41 -05:00
|
|
|
MORE_FLAGS+=(-u 2,3 -a 4,5 )
|
2016-10-27 13:02:21 -04:00
|
|
|
local CUR="default"
|
2016-10-27 10:41:49 -04:00
|
|
|
while true
|
|
|
|
do
|
2016-10-27 13:02:21 -04:00
|
|
|
declare -i RTR
|
|
|
|
declare -i RES
|
|
|
|
local MESG="""You can preview themes by hitting <b>Enter</b>.
|
2016-10-27 10:41:49 -04:00
|
|
|
<b>Alt-a</b> to accept the new theme.
|
|
|
|
<b>Escape</b> to cancel
|
|
|
|
Current theme: <b>${CUR}</b>"""
|
2016-10-29 09:43:26 -04:00
|
|
|
RES=$( create_theme_list | ${ROFI} ${MORE_FLAGS[@]} -selected-row "${SELECTED}" -mesg "${MESG}")
|
2016-10-27 10:41:49 -04:00
|
|
|
RTR=$?
|
|
|
|
if [ ${RTR} = 10 ]
|
|
|
|
then
|
|
|
|
return 0;
|
|
|
|
elif [ ${RTR} = 1 ]
|
|
|
|
then
|
|
|
|
return 1;
|
|
|
|
fi
|
2016-10-27 13:02:21 -04:00
|
|
|
${SED} -i '/#include .*/d' ${TMP_CONFIG_FILE}
|
2016-10-27 10:41:49 -04:00
|
|
|
echo "#include \"${themes[${RES}]}\"" >> ${TMP_CONFIG_FILE}
|
|
|
|
CUR=${theme_names[${RES}]}
|
|
|
|
SELECTED=${RES}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
###
|
|
|
|
# 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
|
|
|
|
###
|
2016-10-27 10:41:49 -04:00
|
|
|
function set_theme()
|
|
|
|
{
|
|
|
|
CDIR="${HOME}/.config/rofi/"
|
|
|
|
if [ ! -d "${CDIR}" ]
|
|
|
|
then
|
|
|
|
mkdir -p ${CDIR}
|
|
|
|
fi
|
|
|
|
if [ -f "${CDIR}/config" ]
|
|
|
|
then
|
2016-10-27 13:02:21 -04:00
|
|
|
${SED} -i "/#include \".*\.theme\"$/d" "${CDIR}/config"
|
2016-10-27 10:41:49 -04:00
|
|
|
fi
|
|
|
|
echo "#include \"${1}\"" >> "${CDIR}/config"
|
|
|
|
|
|
|
|
}
|
2016-10-27 13:02:21 -04:00
|
|
|
|
|
|
|
############################################################################################################
|
|
|
|
# Actual program execution
|
|
|
|
###########################################################################################################
|
2016-10-27 10:41:49 -04:00
|
|
|
##
|
|
|
|
# Find all themes
|
|
|
|
##
|
|
|
|
find_themes
|
|
|
|
|
|
|
|
##
|
|
|
|
# Do check if there are themes.
|
|
|
|
##
|
2016-10-27 13:02:21 -04:00
|
|
|
if [ ${#themes[@]} = 0 ]
|
|
|
|
then
|
|
|
|
${ROFI} -e "No themes found."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
##
|
|
|
|
# Create copy of config to play with in preview
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
create_config_copy
|
|
|
|
|
|
|
|
##
|
2016-10-27 13:02:21 -04:00
|
|
|
# Show the themes to user.
|
2016-10-27 10:41:49 -04:00
|
|
|
##
|
|
|
|
if select_theme && [ -n "${SELECTED}" ]
|
|
|
|
then
|
2016-10-27 13:02:21 -04:00
|
|
|
# Set theme
|
2016-10-27 10:41:49 -04:00
|
|
|
set_theme "${themes[${SELECTED}]}"
|
|
|
|
fi
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
##
|
|
|
|
# Remove temp. config.
|
|
|
|
##
|
2016-10-27 10:41:49 -04:00
|
|
|
rm ${TMP_CONFIG_FILE}
|