Refactor theme_vim to follow template layout
This commit is contained in:
parent
8106c87633
commit
17bca81232
1 changed files with 64 additions and 67 deletions
131
theme_vim
131
theme_vim
|
@ -1,73 +1,43 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
readonly dependency=("chriskempson/base16-vim")
|
readonly dependency=("chriskempson/base16-vim")
|
||||||
|
readonly app="vim"
|
||||||
|
|
||||||
path="$1"
|
readonly path="$1"
|
||||||
package="$2"
|
readonly package="$2"
|
||||||
theme="$3"
|
readonly theme="$3"
|
||||||
permanent="$4"
|
readonly permanent="$4"
|
||||||
|
|
||||||
file_exists() {
|
## Main Entrypoint
|
||||||
if [[ -f "$1" ]]; then
|
#
|
||||||
true
|
# Finds the right theme template file and starts theme
|
||||||
else
|
# switching and, if necessary, permanent setting processes.
|
||||||
false
|
main() {
|
||||||
fi
|
dbg_msg $app "Starting Processor"
|
||||||
}
|
|
||||||
# check for existence of pattern $2 in file $1
|
|
||||||
line_exists() {
|
|
||||||
local file="$1"
|
|
||||||
local line="$2"
|
|
||||||
|
|
||||||
if ! file_exists "$file" || ! grep -qe "$line" "$file"; then
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
# prepare newline at eof to make adding newlines easier
|
|
||||||
eol_exists_or_append() {
|
|
||||||
local file="$1"
|
|
||||||
local eof
|
|
||||||
eof=$(tail -c 1 "$file")
|
|
||||||
|
|
||||||
if [ -n "$eof" ]; then
|
|
||||||
printf "\\n" >>"$file"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
# append line $2 to file $1
|
|
||||||
line_exists_or_append() {
|
|
||||||
local file="$1"
|
|
||||||
local line="$2"
|
|
||||||
local new="${3:-$2}"
|
|
||||||
|
|
||||||
if ! line_exists "$file" "$line"; then
|
|
||||||
eol_exists_or_append "$file"
|
|
||||||
echo "$new" >>"$file"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
theme_vim() {
|
|
||||||
dbg_msg="VIM: $package - "
|
|
||||||
tfile="$path/$package/colors/base16-$theme.vim"
|
tfile="$path/$package/colors/base16-$theme.vim"
|
||||||
|
|
||||||
check_tfile
|
|
||||||
|
|
||||||
if [[ "$permanent" == "true" ]]; then set_vim_theme; fi
|
|
||||||
recolor_vim_w_tmux
|
|
||||||
|
|
||||||
[[ "$DEBUG" == true ]] && echo "$dbg_msg"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_tfile() {
|
|
||||||
if ! file_exists "$tfile"; then
|
if ! file_exists "$tfile"; then
|
||||||
dbg_msg="$dbg_msg theme $theme for vim not found.\n"
|
dbg_msg $app "error" "Theme template $theme not found in package $package"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ "$permanent" == "true" ]]; then save; fi
|
||||||
|
theme
|
||||||
|
|
||||||
|
dbg_msg $app "Processor Done"
|
||||||
}
|
}
|
||||||
|
|
||||||
recolor_vim_w_tmux() {
|
## Theme switcher
|
||||||
|
#
|
||||||
|
# Makes sure that if any application instance is
|
||||||
|
# currently running, it switches to new theme.
|
||||||
|
theme() {
|
||||||
|
dbg_msg $app "Switching theme"
|
||||||
# this whole thing only works when tmux is running
|
# this whole thing only works when tmux is running
|
||||||
pgrep tmux >/dev/null || return
|
|
||||||
|
pgrep tmux >/dev/null || {
|
||||||
|
dbg_msg $app "warn" "No instance of tmux running, can not dynamically re-theme vim"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
while read -r l; do
|
while read -r l; do
|
||||||
# we need to close panels that are in a mode and get them out to send keys to vim
|
# we need to close panels that are in a mode and get them out to send keys to vim
|
||||||
|
@ -79,37 +49,64 @@ recolor_vim_w_tmux() {
|
||||||
|
|
||||||
# find all panes which currently have nvim/vim running
|
# 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:')
|
done < <(tmux list-panes -a -F '#D:#T:#{pane_current_command}:#{pane_in_mode}' | grep ':VIM:\|:nvim:')
|
||||||
|
|
||||||
|
dbg_msg $app "Successfully switched theme"
|
||||||
}
|
}
|
||||||
|
|
||||||
set_vim_theme() {
|
## Theme setter
|
||||||
|
#
|
||||||
|
# Takes care of permanently writing the desired
|
||||||
|
# base16 theme into application settings.
|
||||||
|
save() {
|
||||||
|
dbg_msg $app "Saving theme"
|
||||||
local vim_dirs="${XDG_CONFIG_HOME:-/$HOME/.config}/nvim $HOME/.vim"
|
local vim_dirs="${XDG_CONFIG_HOME:-/$HOME/.config}/nvim $HOME/.vim"
|
||||||
|
|
||||||
for vim_dir in $vim_dirs; do
|
for vim_dir in $vim_dirs; do
|
||||||
if [[ -d "$vim_dir" ]]; then
|
if [[ -d "$vim_dir" ]]; then
|
||||||
cat "$tfile" >"$vim_dir/colorscheme.vim"
|
cat "$tfile" >"$vim_dir/colorscheme.vim"
|
||||||
echo "colorscheme base16-$theme" >>"$vim_dir/colorscheme.vim"
|
echo "colorscheme base16-$theme" >>"$vim_dir/colorscheme.vim"
|
||||||
|
dbg_msg $app "Saved theme to $vim_dir/colorscheme.vim"
|
||||||
|
|
||||||
call_from_vim_init "$vim_dir"
|
include "$vim_dir"
|
||||||
|
else
|
||||||
dbg_msg="$dbg_msg Set theme $theme\n"
|
dbg_msg $app "warn" "No configuration directory found"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# make sure we're calling our new colorscheme whenever starting vim
|
## Theme includer
|
||||||
call_from_vim_init() {
|
#
|
||||||
|
# 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() {
|
||||||
local vim_dir="$1"
|
local vim_dir="$1"
|
||||||
|
dbg_msg $app "Including theme in configuration"
|
||||||
|
|
||||||
if file_exists "$vim_dir/init.vim"; then
|
if file_exists "$vim_dir/init.vim"; then
|
||||||
line_exists_or_append "$vim_dir/init.vim" "runtime colorscheme.vim"
|
line_exists_or_append "$vim_dir/init.vim" "runtime colorscheme.vim"
|
||||||
|
dbg_msg $app "Successfully included theme in configuration"
|
||||||
elif file_exists "$HOME/.vimrc"; then
|
elif file_exists "$HOME/.vimrc"; then
|
||||||
line_exists_or_append "$HOME/.vimrc" "runtime colorscheme.vim"
|
line_exists_or_append "$HOME/.vimrc" "runtime colorscheme.vim"
|
||||||
|
dbg_msg $app "Successfully included theme in configuration"
|
||||||
|
else
|
||||||
|
dbg_msg $app "warn" "No default configuration file found"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check deps
|
## Dependency Checker
|
||||||
|
#
|
||||||
|
# Makes sure the processor is called for the correct
|
||||||
|
# base16 package, or refuses to run if it is not.
|
||||||
if printf '%s\n' "${dependency[@]}" | grep -q -P "^$package$"; then
|
if printf '%s\n' "${dependency[@]}" | grep -q -P "^$package$"; then
|
||||||
theme_vim
|
# Safe sourcing: https://stackoverflow.com/a/12694189
|
||||||
|
DIR="${BASH_SOURCE%/*}"
|
||||||
|
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
|
||||||
|
# shellcheck source=utilities.sh
|
||||||
|
. "$DIR/utilities.sh"
|
||||||
|
|
||||||
|
main
|
||||||
else
|
else
|
||||||
printf "Processor does not work for %s, please use another." "$package"
|
dbg_msg $app "error" "Processor does not work for package $package"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue