2020-01-30 21:46:30 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
readonly dependency=("chriskempson/base16-vim")
|
2020-01-31 11:31:02 +00:00
|
|
|
readonly app="vim"
|
|
|
|
|
|
|
|
readonly path="$1"
|
|
|
|
readonly package="$2"
|
|
|
|
readonly theme="$3"
|
|
|
|
readonly permanent="$4"
|
|
|
|
|
|
|
|
## Main Entrypoint
|
|
|
|
#
|
|
|
|
# Finds the right theme template file and starts theme
|
|
|
|
# switching and, if necessary, permanent setting processes.
|
|
|
|
main() {
|
|
|
|
dbg_msg $app "Starting Processor"
|
2020-01-30 21:46:30 +00:00
|
|
|
tfile="$path/$package/colors/base16-$theme.vim"
|
|
|
|
|
|
|
|
if ! file_exists "$tfile"; then
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "error" "Theme template $theme not found in package $package"
|
2020-01-30 22:13:46 +00:00
|
|
|
exit 1
|
2020-01-30 21:46:30 +00:00
|
|
|
fi
|
2020-01-31 11:31:02 +00:00
|
|
|
|
|
|
|
if [[ "$permanent" == "true" ]]; then save; fi
|
|
|
|
theme
|
|
|
|
|
|
|
|
dbg_msg $app "Processor Done"
|
2020-01-30 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 11:31:02 +00:00
|
|
|
## Theme switcher
|
|
|
|
#
|
|
|
|
# Makes sure that if any application instance is
|
|
|
|
# currently running, it switches to new theme.
|
|
|
|
theme() {
|
|
|
|
dbg_msg $app "Switching theme"
|
2020-01-30 21:46:30 +00:00
|
|
|
# this whole thing only works when tmux is running
|
2020-01-31 11:31:02 +00:00
|
|
|
|
|
|
|
pgrep tmux >/dev/null || {
|
|
|
|
dbg_msg $app "warn" "No instance of tmux running, can not dynamically re-theme vim"
|
|
|
|
return
|
|
|
|
}
|
2020-01-30 21:46:30 +00:00
|
|
|
|
|
|
|
while read -r l; do
|
|
|
|
# we need to close panels that are in a mode and get them out to send keys to vim
|
|
|
|
if [ "${l: -1}" = 1 ]; then
|
|
|
|
tmux send-keys -t "${l/:*/}" -X cancel && tmux send-keys -t "${l/:*/}" ":source $tfile" "Enter"
|
|
|
|
else
|
|
|
|
tmux send-keys -t "${l/:*/}" ":source $tfile" "Enter"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# find all panes which currently have nvim/vim running
|
|
|
|
done < <(tmux list-panes -a -F '#D:#T:#{pane_current_command}:#{pane_in_mode}' | grep ':VIM:\|:nvim:')
|
2020-01-31 11:31:02 +00:00
|
|
|
|
|
|
|
dbg_msg $app "Successfully switched theme"
|
2020-01-30 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 11:31:02 +00:00
|
|
|
## Theme setter
|
|
|
|
#
|
|
|
|
# Takes care of permanently writing the desired
|
|
|
|
# base16 theme into application settings.
|
|
|
|
save() {
|
|
|
|
dbg_msg $app "Saving theme"
|
2020-01-30 21:46:30 +00:00
|
|
|
local vim_dirs="${XDG_CONFIG_HOME:-/$HOME/.config}/nvim $HOME/.vim"
|
2020-01-31 11:46:59 +00:00
|
|
|
local foundone=false
|
2020-01-30 21:46:30 +00:00
|
|
|
|
|
|
|
for vim_dir in $vim_dirs; do
|
|
|
|
if [[ -d "$vim_dir" ]]; then
|
2020-01-31 11:46:59 +00:00
|
|
|
foundone=true
|
|
|
|
|
2020-01-30 21:46:30 +00:00
|
|
|
cat "$tfile" >"$vim_dir/colorscheme.vim"
|
|
|
|
echo "colorscheme base16-$theme" >>"$vim_dir/colorscheme.vim"
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "Saved theme to $vim_dir/colorscheme.vim"
|
2020-01-30 21:46:30 +00:00
|
|
|
|
2020-01-31 11:31:02 +00:00
|
|
|
include "$vim_dir"
|
2020-01-30 21:46:30 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-01-31 11:46:59 +00:00
|
|
|
|
|
|
|
[[ "$foundone" == false ]] && dbg_msg $app "warn" "No configuration directory found"
|
2020-01-30 21:46:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 11:31:02 +00:00
|
|
|
## Theme includer
|
|
|
|
#
|
|
|
|
# Makes sure that theme is actually included in
|
|
|
|
# default application startup routine.
|
|
|
|
# This is the most invasive step of theming since it
|
|
|
|
# rewrites within existing configuration files.
|
|
|
|
include() {
|
2020-01-30 22:13:46 +00:00
|
|
|
local vim_dir="$1"
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "Including theme in configuration"
|
2020-01-30 22:13:46 +00:00
|
|
|
|
2020-01-30 21:46:30 +00:00
|
|
|
if file_exists "$vim_dir/init.vim"; then
|
|
|
|
line_exists_or_append "$vim_dir/init.vim" "runtime colorscheme.vim"
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "Successfully included theme in configuration"
|
2020-01-30 21:46:30 +00:00
|
|
|
elif file_exists "$HOME/.vimrc"; then
|
|
|
|
line_exists_or_append "$HOME/.vimrc" "runtime colorscheme.vim"
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "Successfully included theme in configuration"
|
|
|
|
else
|
|
|
|
dbg_msg $app "warn" "No default configuration file found"
|
2020-01-30 21:46:30 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-02-15 14:25:20 +00:00
|
|
|
# Safe sourcing: https://stackoverflow.com/a/12694189
|
|
|
|
DIR="${BASH_SOURCE%/*}"
|
|
|
|
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
|
|
|
|
# shellcheck source=utilities.sh
|
|
|
|
. "$DIR/utilities.sh"
|
2020-01-31 11:31:02 +00:00
|
|
|
## Dependency Checker
|
|
|
|
#
|
|
|
|
# Makes sure the processor is called for the correct
|
|
|
|
# base16 package, or refuses to run if it is not.
|
2020-01-30 21:46:30 +00:00
|
|
|
if printf '%s\n' "${dependency[@]}" | grep -q -P "^$package$"; then
|
2020-01-31 11:31:02 +00:00
|
|
|
main
|
2020-01-30 21:46:30 +00:00
|
|
|
else
|
2020-01-31 11:31:02 +00:00
|
|
|
dbg_msg $app "error" "Processor does not work for package $package"
|
2020-01-30 21:46:30 +00:00
|
|
|
fi
|