2016-10-27 10:41:49 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2021-08-14 07:03:16 -04:00
|
|
|
# This code is released in public domain by Dave Davenport <qball@gmpclient.org>
|
2016-10-27 10:41:49 -04:00
|
|
|
#
|
|
|
|
|
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
|
|
|
|
|
2020-06-17 07:53:18 -04:00
|
|
|
TMP_CONFIG_FILE=$(${MKTEMP}).rasi
|
2016-10-27 13:02:21 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
##
|
2021-08-14 07:03:16 -04:00
|
|
|
find_themes()
|
2016-10-27 10:41:49 -04:00
|
|
|
{
|
|
|
|
DIRS=${XDG_DATA_DIRS}
|
2016-11-28 12:42:22 -05:00
|
|
|
OLDIFS=${IFS}
|
|
|
|
IFS=:
|
2016-10-27 10:41:49 -04:00
|
|
|
if [ -z "${XDG_DATA_DIRS}" ]
|
|
|
|
then
|
2016-11-28 12:42:22 -05:00
|
|
|
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}"
|
2016-10-27 10:41:49 -04:00
|
|
|
fi
|
|
|
|
# Add user dir.
|
2018-06-03 02:58:40 -04:00
|
|
|
DIRS+=":${XDG_DATA_HOME:-${HOME}/.local/share}"
|
|
|
|
DIRS+=":${XDG_CONFIG_HOME:-${HOME}/.config}"
|
2021-08-14 07:03:16 -04:00
|
|
|
for p in ${DIRS}; do
|
|
|
|
p=${p%/}
|
2017-10-19 15:40:33 -04:00
|
|
|
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
|
2016-11-28 13:47:06 -05:00
|
|
|
echo "Checking themes in: ${TD}"
|
2017-01-19 03:19:14 -05:00
|
|
|
for file in ${TD}/*.rasi
|
2016-10-27 10:41:49 -04:00
|
|
|
do
|
2016-10-27 13:02:21 -04:00
|
|
|
if [ -f "${file}" ]
|
2016-10-27 10:41:49 -04:00
|
|
|
then
|
2021-08-14 07:03:16 -04:00
|
|
|
themes+=("${file}")
|
|
|
|
FN=$(basename "${file}")
|
2016-10-27 13:02:21 -04:00
|
|
|
NAME=${FN%.*}
|
2021-08-14 07:03:16 -04:00
|
|
|
USER=$(${SED} -n 's/^.*User: \(.*\)/\1/p' "${file}" | head -n 1 )
|
2016-10-27 13:02:21 -04:00
|
|
|
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
|
|
|
|
##
|
2021-08-14 07:03:16 -04:00
|
|
|
create_config_copy()
|
2016-10-27 10:41:49 -04:00
|
|
|
{
|
2021-08-14 07:03:16 -04:00
|
|
|
${ROFI} -dump-config > "${TMP_CONFIG_FILE}"
|
2020-06-17 07:53:18 -04:00
|
|
|
# remove theme entry.
|
2021-08-14 07:03:16 -04:00
|
|
|
sed -i 's/^\s*theme:\s\+".*"\s*;//g' "${TMP_CONFIG_FILE}"
|
2016-10-27 10:41:49 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:02:21 -04:00
|
|
|
###
|
|
|
|
# Print the list out so it can be displayed by rofi.
|
|
|
|
##
|
2021-08-14 07:03:16 -04:00
|
|
|
create_theme_list()
|
2016-10-27 10:41:49 -04:00
|
|
|
{
|
|
|
|
OLDIFS=${IFS}
|
2016-10-27 13:02:21 -04:00
|
|
|
IFS='|'
|
2021-08-14 07:03:16 -04:00
|
|
|
for themen in ${theme_names[@]}
|
2016-10-27 10:41:49 -04:00
|
|
|
do
|
2021-08-14 07:03:16 -04:00
|
|
|
echo "${themen}"
|
2016-10-27 10:41:49 -04:00
|
|
|
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
|
|
|
|
|
2021-08-14 07:03:16 -04:00
|
|
|
select_theme()
|
2016-10-27 10:41:49 -04:00
|
|
|
{
|
2021-08-14 07:03:16 -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
|
2021-08-25 10:32:06 -04:00
|
|
|
Current theme: <b>${CUR}</b>
|
|
|
|
<span weight=\"bold\" size=\"xx-small\">When setting a new theme this will override previous theme settings.
|
|
|
|
Please update your config file if you have local modifications.</span>"""
|
2017-01-19 03:19:14 -05:00
|
|
|
THEME_FLAG=
|
|
|
|
if [ -n "${SELECTED}" ]
|
2021-08-14 07:03:16 -04:00
|
|
|
then
|
|
|
|
THEME_FLAG="-theme ${themes[${SELECTED}]}"
|
2017-01-19 03:19:14 -05:00
|
|
|
fi
|
2021-08-14 07:03:16 -04:00
|
|
|
RES=$( create_theme_list | ${ROFI} ${THEME_FLAG} ${MORE_FLAGS[@]} -cycle -selected-row "${SELECTED}" -mesg "${MESG}")
|
2016-10-27 10:41:49 -04:00
|
|
|
RTR=$?
|
2021-08-14 07:03:16 -04:00
|
|
|
if [ "${RTR}" = 10 ]
|
2016-10-27 10:41:49 -04:00
|
|
|
then
|
|
|
|
return 0;
|
2021-08-14 07:03:16 -04:00
|
|
|
elif [ "${RTR}" = 1 ]
|
2019-01-07 11:19:33 -05:00
|
|
|
then
|
|
|
|
return 1;
|
2021-08-14 07:03:16 -04:00
|
|
|
elif [ "${RTR}" = 65 ]
|
2016-10-27 10:41:49 -04:00
|
|
|
then
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
CUR=${theme_names[${RES}]}
|
2021-08-14 07:03:16 -04:00
|
|
|
SELECTED=${RES}
|
2016-10-27 10:41:49 -04:00
|
|
|
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
|
|
|
|
###
|
2021-08-14 07:03:16 -04:00
|
|
|
set_theme()
|
2016-10-27 10:41:49 -04:00
|
|
|
{
|
2018-06-03 02:58:40 -04:00
|
|
|
CDIR="${XDG_CONFIG_HOME:-${HOME}/.config}/rofi"
|
2016-10-27 10:41:49 -04:00
|
|
|
if [ ! -d "${CDIR}" ]
|
|
|
|
then
|
2021-08-14 07:03:16 -04:00
|
|
|
mkdir -p "${CDIR}"
|
2016-10-27 10:41:49 -04:00
|
|
|
fi
|
2021-06-14 09:14:41 -04:00
|
|
|
get_link=$(readlink -f "${CDIR}/config.rasi")
|
2021-08-26 10:40:19 -04:00
|
|
|
${SED} -i 's/^\s*\(@theme\s\+".*"\)/\/\/\1/' "${get_link}"
|
2021-08-25 10:32:06 -04:00
|
|
|
echo "@theme \"${1}\"" >> "${get_link}"
|
2016-10-27 10:41:49 -04:00
|
|
|
}
|
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.
|
|
|
|
##
|
2021-08-14 07:03:16 -04:00
|
|
|
rm -- "${TMP_CONFIG_FILE}"
|