spmenuify/spmenuify
2023-08-08 22:38:01 +02:00

328 lines
15 KiB
Bash
Executable file

#!/bin/sh
# spmenuify
# Theme manager for spmenu
# https://git.speedie.site/spmenuify
#
# Copyright (C) 2023 speedie
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
VERSION="${VERSION:-1.0}"
THEME_DIR="${THEME_DIR:-$HOME/.config/spmenu/themes}"
REPO_DIR="${REPO_DIR:-${XDG_CACHE_DIR:-$HOME/.cache}/spmenuify/repo}"
CONFIG_FILE="${CONFIG_FILE:-$HOME/.config/spmenu/spmenuify/spmenuify.conf}"
SEND_NOTIFICATION="${SEND_NOTIFICATION:-true}"
ENABLE_IMAGE_PREVIEW="${ENABLE_IMAGE_PREVIEW:-true}"
REPOSITORY="${REPOSITORY:-https://git.speedie.site/speedie/spmenu-themes}"
load_config() { [ -f "$CONFIG_FILE" ] && . "$CONFIG_FILE"; }
die() { printf "%s\n" "${@}" > /dev/stderr && exit 1; }
print_list() {
[ ! -d "$THEME_DIR" ] && mkdir -p "$THEME_DIR"
print_theme_list
cat << EOF
$(printf "%b%s" "\033[0;34m" "Install")
$(printf "%b%s" "\033[0;34m" "Download")
$(printf "%b%s" "\033[0;34m" "Clear")
$(printf "%b%s" "\033[0;34m" "About")
EOF
}
print_theme_list() {
print_theme_list_t | sort ${SORT_ARGS}
}
print_theme_list_t() {
find "$THEME_DIR" -type f > /tmp/list
while read -r l; do
file="$(basename "$l")"
printf "%s" "$l" | grep -qE "[.]desc|[.]png" && continue
if [ -f "${l}.png" ] && [ "$ENABLE_IMAGE_PREVIEW" = "true" ]; then
printf "img://%s\t%s\n" "${l}.png" "${file}"
set=true
else
printf "%s\n" "${file}"
set=true
fi
done < "/tmp/list"; rm -f /tmp/list
[ "$set" != "true" ] && printf "%b%s\n" "\033[0;31m" "No themes installed."
}
check_spmenu_ver() {
case "$(spmenu -v)" in
spmenu-1.0|spmenu-0.*) printf "spmenu version too old, please update.\n" && exit 1 ;;
esac
}
list_files() {
[ ! -x "$(command -v spmenu_run)" ] && printf "spmenu_run not found, can't continue.\n" && exit 1
export LS_ARGS="--color=always -a"
spmenu_run -fm "$HOME" --stdout -a "--require-match --insert --columns 1 --lines 30 --prompt Select:" | sed -e 's/\x1b\[[0-9;]*m//g' > /tmp/spmenuify_out
if [ -z "$(cat /tmp/spmenuify_out)" ]; then
main
return
fi
[ "$(printf "Yes\nNo\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Install themes?" --no-allow-typing --hide-mode --hide-input --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --hide-caps --normal)" != "Yes" ] && main
while read -r theme; do
[ ! -f "$theme" ] && continue
command -v spmenuify_entry_list_func > /dev/null && spmenuify_entry_list_func "$theme"
grep -q "theme" "$theme" && cp "$theme" "$THEME_DIR/" || exit 1
[ -f "$theme.desc" ] && cp "$theme.desc" "$THEME_DIR"
[ -f "$theme.png" ] && cp "$theme.png" "$THEME_DIR"
done < /tmp/spmenuify_out; rm -f /tmp/spmenuify_out
command -v spmenuify_post_list_func > /dev/null && spmenuify_post_list_func
main
}
opt_theme() {
desc_text="No description provided."
[ -f "$THEME_DIR/$output.desc" ] && desc_text="$(cat "$THEME_DIR/$output.desc")"
if [ -f "$THEME_DIR/$output.png" ] && [ -f "$THEME_DIR/$output" ]; then
o="$(printf "%s\t%b%s\n\n%b%s\n%b%s\n%b%s\n%b%s\n" "img://${THEME_DIR}/$output.png" "\033[0m" "${desc_text}" "\033[0;34m" "Enable" "\033[0;34m" "Preview" "\033[0;34m" "View" "\033[0;34m" "Remove" | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting | sed -e 's/\x1b\[[0-9;]*m//g')"
[ -z "$o" ] && main && exit 0
elif [ -f "$THEME_DIR/$output" ]; then
o="$(printf "%b%s\n\n%b%s\n%b%s\n%b%s\n%b%s\n" "\033[0m" "${desc_text}" "\033[0;34m" "Enable" "\033[0;34m" "Preview" "\033[0;34m" "View" "\033[0;34m" "Remove" | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting | sed -e 's/\x1b\[[0-9;]*m//g')"
[ -z "$o" ] && main && exit 0
else
main && exit 0
fi
case "$o" in
"Remove")
[ "$(printf "Yes\nNo\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Remove themes?" --no-allow-typing --hide-mode --hide-input --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --hide-caps --normal)" != "Yes" ] && main && exit 0
rm -f "$THEME_DIR/$output" "$THEME_DIR/$output*"
main && exit 0
;;
"View")
[ -f "$THEME_DIR/$output" ] && spmenu --require-match --lines 40 --columns 1 --prompt "Viewing theme '$output'" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --normal < "$THEME_DIR/$output"
main && exit 0
;;
"Preview")
[ -f "$HOME/.config/spmenu/theme.conf" ] && cp "$HOME/.config/spmenu/theme.conf" "/tmp/theme.conf"
[ -f "$THEME_DIR/$output" ] && cp -f "$THEME_DIR/$output" "$HOME/.config/spmenu/theme.conf"
command -v spmenuify_pre_preview_func > /dev/null && spmenuify_pre_preview_func
printf "\033[0;31mItem 1\n\033[0;32mItem 2\n\033[0;33mItem 3\n\033[0;34mItem 4\n\033[0;35mItem 5\n\033[0;36mItem 6\n\033[0;37mItem 7\n\033[0;38mItem 8\n\033[1;31mItem 9\n\033[1;32mItem 10\n\033[1;33mItem 11\n\033[1;34mItem 12\n\033[1;35mItem 13\n\033[1;36mItem 14\n\033[1;37mItem 15\n\033[1;38mItem 16\n\033[0;41mItem 1\n\033[0;42mItem 2\n\033[0;43mItem 3\n\033[0;44mItem 4\n\033[0;45mItem 5\n\033[0;46mItem 6\n\033[0;47mItem 7\n\033[0;48mItem 8\n\033[1;41mItem 9\n\033[1;42mItem 10\n\033[1;43mItem 11\n\033[1;44mItem 12\n\033[1;45mItem 13\n\033[1;46mItem 14\n\033[1;47mItem 15\n\033[1;48mItem 16\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Preview" --no-allow-typing
if [ -f "/tmp/theme.conf" ]; then
mv "/tmp/theme.conf" "$HOME/.config/spmenu/theme.conf"
else
rm -f "$HOME/.config/spmenu/theme.conf"
fi
command -v spmenuify_post_preview_func > /dev/null && spmenuify_post_preview_func
main && exit 0
;;
"Enable")
[ "$(printf "Yes\nNo\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Enable theme ${output}?" --no-allow-typing --hide-mode --hide-input --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --hide-caps --normal)" != "Yes" ] && main
command -v spmenuify_pre_enable_func > /dev/null && spmenuify_pre_enable_func
[ -f "$THEME_DIR/$output" ] && cp -f "$THEME_DIR/$output" "$HOME/.config/spmenu/theme.conf" && \
command -v notify-send > /dev/null && [ "$SEND_NOTIFICATION" != "false" ] && notify-send "Enabled" "Enabled theme $output"
command -v spmenuify_post_enable_func > /dev/null && spmenuify_post_enable_func
main && exit 0
;;
esac
}
print_about() {
printf "\033[0;31ms" > /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;32mp" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;33mm" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;34me" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;35mn" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;36mu" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;37mi" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[0;38mf" >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[1;31my " >> /tmp/spmenuify_about_out
sleep 0.1; printf "update" > /tmp/spmenu.fifo
printf "\033[1;32m%s\n" "$VERSION" >> /tmp/spmenuify_about_out
sleep 0.1
printf "update" > /tmp/spmenu.fifo
sleep 1
printf "Written by speedie & contributors\n" >> /tmp/spmenuify_about_out
printf "update" > /tmp/spmenu.fifo
sleep 1
printf "\033[0;34mLicensed under the GNU General Public License version 3.0.\n\n" >> /tmp/spmenuify_about_out
printf "update" > /tmp/spmenu.fifo
}
about() {
rm -f /tmp/spmenuify_about_out
print_about &
spmenu --hide-mode --hide-match-count --hide-left-arrow --hide-right-arrow --hide-prompt --hide-input --hide-caret --hide-caps --no-color-items --no-allow-typing --columns 1 --lines 5 --list-file /tmp/spmenuify_about_out > /dev/null
rm -f /tmp/spmenuify_about_out
pkill print_about
main && exit 0
}
show_note() {
mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}/spmenuify"
touch "${XDG_CACHE_HOME:-$HOME/.cache}/spmenuify/.has-shown-note"
cat << EOF | spmenu --lines 30 --columns 1 --normal --hide-cursor --no-allow-typing --no-color-items --hide-prompt --hide-powerline --hide-input --hide-right-arrow --hide-left-arrow --hide-mode --hide-match-count --hide-caps > /dev/null
Note: This feature downloads themes from the internet. Please make sure you trust the repository before using.
Use downloaded themes at your own risk. Contributors to spmenu and/or spmenuify have not necessarily
tested or verified these themes. If you do not want to download themes from the internet, quit spmenuify now.
It should not be possible to create malicious spmenu themes. If this is possible, please file a bug report.
EOF
}
get_repo() {
[ ! -f "${XDG_CACHE_HOME:-$HOME/.cache}/spmenuify/.has-shown-note" ] && show_note
tx="$(get_repo_theme_list | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --no-mark-items | sed -e 's/\x1b\[[0-9;]*m//g')"
if [ -z "$(cat /tmp/spmenuify_out)" ]; then
main
return
fi
get_l_repo
}
get_l_repo() {
cd "${REPO_DIR}" || exit 1
title_text="$(basename "$tx")"
desc_text="No description provided."
author_text="No author provided."
[ -f "$tx.title" ] && title_text="$(cat "$tx.title")"
[ -f "$tx.desc" ] && desc_text="$(cat "$tx.desc")"
[ -f "$tx.author" ] && author_text="$(cat "$tx.author")"
if [ -f "$tx.png" ]; then
o="$(printf "%s\t%b%s\n%b%s\n%b%s\n\n%b%s\n%b%s\n%b%s\n" "img://$tx.png" "\033[0m" "${title_text}" "\033[0m" "${author_text}" "\033[0m" "${desc_text}" "\033[0;34m" "Install" "\033[0;34m" "Preview" "\033[0;34m" "View" | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting | sed -e 's/\x1b\[[0-9;]*m//g')"
elif [ -f "$tx" ]; then
o="$(printf "%b%s\n%b%s\n%b%s\n\n%b%s\n%b%s\n%b%s\n" "\033[0m" "${title_text}" "\033[0m" "${author_text}" "\033[0m" "${desc_text}" "\033[0;34m" "Install" "\033[0;34m" "Preview" "\033[0;34m" "View" | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting | sed -e 's/\x1b\[[0-9;]*m//g')"
else
main && exit 0
fi
case "$o" in
"View")
[ -f "$tx" ] && spmenu --require-match --lines 40 --columns 1 --prompt "Viewing theme '$tx'" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --normal < "$tx"
get_l_repo && exit 0
;;
"Preview")
[ -f "$HOME/.config/spmenu/theme.conf" ] && cp "$HOME/.config/spmenu/theme.conf" "/tmp/theme.conf"
[ -f "$tx" ] && cp -f "$tx" "$HOME/.config/spmenu/theme.conf"
printf "\033[0;31mItem 1\n\033[0;32mItem 2\n\033[0;33mItem 3\n\033[0;34mItem 4\n\033[0;35mItem 5\n\033[0;36mItem 6\n\033[0;37mItem 7\n\033[0;38mItem 8\n\033[1;31mItem 9\n\033[1;32mItem 10\n\033[1;33mItem 11\n\033[1;34mItem 12\n\033[1;35mItem 13\n\033[1;36mItem 14\n\033[1;37mItem 15\n\033[1;38mItem 16\n\033[0;41mItem 1\n\033[0;42mItem 2\n\033[0;43mItem 3\n\033[0;44mItem 4\n\033[0;45mItem 5\n\033[0;46mItem 6\n\033[0;47mItem 7\n\033[0;48mItem 8\n\033[1;41mItem 9\n\033[1;42mItem 10\n\033[1;43mItem 11\n\033[1;44mItem 12\n\033[1;45mItem 13\n\033[1;46mItem 14\n\033[1;47mItem 15\n\033[1;48mItem 16\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Preview" --no-allow-typing
if [ -f "/tmp/theme.conf" ]; then
mv "/tmp/theme.conf" "$HOME/.config/spmenu/theme.conf"
else
rm -f "$HOME/.config/spmenu/theme.conf"
fi
get_l_repo && exit 0
;;
"Install")
[ "$(printf "Yes\nNo\n" | spmenu --require-match --lines 40 --columns 1 --prompt "Install theme ${tx}?" --no-allow-typing --hide-mode --hide-input --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --hide-caps --normal)" != "Yes" ] && get_l_repo && exit 0
[ -f "$tx.desc" ] && cp "$tx.desc" "$THEME_DIR"
[ -f "$tx.png" ] && cp "$tx.png" "$THEME_DIR"
[ -f "$tx" ] && cp "$tx" "$THEME_DIR"
get_l_repo && exit 0
;;
*)
get_repo && exit 0
;;
esac
main && exit 0
}
get_repo_theme_list() {
clone_repo
find "$REPO_DIR" -type f | grep -vE "[.]author|[.]title|[.]desc|[.]png" | grep "[.]conf" > /tmp/list
if [ -z "$(cat /tmp/list)" ]; then
printf "%b%s\n" "\033[0;31m" "No themes found in repository."
else
while read -r l; do
file="$(basename "$l")"
if [ -f "${l}.png" ] && [ "$ENABLE_IMAGE_PREVIEW" = "true" ]; then
printf "img://%s\t%b%s\n" "${l}.png" "\033[0;35m" "${file}"
else
printf "%b%s\n" "\033[0;35m" "${file}"
fi
done < "/tmp/list"; rm -f /tmp/list
fi
}
clone_repo() {
mkdir -p "${XDG_CACHE_DIR:-$HOME/.cache}/spmenuify" || exit 1
[ ! -x "$(command -v git)" ] && printf "Git not installed.\n" > /dev/stderr && exit 1
if [ ! -d "${REPO_DIR}" ]; then
git clone --quiet --depth 1 "${REPOSITORY}" "${REPO_DIR}" || die "Failed to clone repository $REPOSITORY"
else
cd "${REPO_DIR}" || exit 1
git pull --quiet
fi
[ ! -d "${REPO_DIR}" ] && die "Repository not found"
[ ! -f "${REPO_DIR}/.spmenuify" ] && die "Not a spmenuify >= 1.0 compatible theme repository"
}
main() {
check_spmenu_ver
mkdir -p "$HOME/.config/spmenu/"
output="$(print_list | spmenu --require-match --lines 40 --columns 1 --prompt "Select:" --hide-mode --hide-caps --hide-match-count --hide-left-arrow --hide-right-arrow --hide-highlighting --no-mark-items | sed -e 's/\x1b\[[0-9;]*m//g')"
printf "%s\n" "$output" > /tmp/spmenuify_out
case "$output" in
"Install") list_files ;;
"About") about ;;
"Download") get_repo ;;
"Clear") rm -f "${HOME}/.config/spmenu/theme.conf"; main "$@" ;;
"") exit 0 ;;
*) opt_theme ;;
esac
rm -f /tmp/spmenuify_out /tmp/spmenuify_about_out
}
main