From 53cedcb25f486dea7ceb6c9715d774ba72da7256 Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 18 Jan 2023 15:09:08 -0500 Subject: [PATCH] syntax highlighting! automatically! --- zsh/.config/zsh/config.d/40_highlighting.zsh | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 zsh/.config/zsh/config.d/40_highlighting.zsh diff --git a/zsh/.config/zsh/config.d/40_highlighting.zsh b/zsh/.config/zsh/config.d/40_highlighting.zsh new file mode 100644 index 0000000..cc533b4 --- /dev/null +++ b/zsh/.config/zsh/config.d/40_highlighting.zsh @@ -0,0 +1,45 @@ +# where to look for the syntax highlighting repo +targetdir="$HOME/src/github.com/zsh-users/zsh-syntax-highlighting" +# if this file exists, do not attempt to load the highlight module +nohighlightfile="$HOME/.config/zsh/.nohighlight" + +# loads the highlighter +function load_highlight { + source "$targetdir/zsh-syntax-highlighting.zsh" +} + +function confirm { + while true; do + read "answer?$* [(yes)/no]: " + case $answer in + [Yy]|yes|"") + return 0 + ;; + [Nn]|no) + return 1 + ;; + esac + done +} + +# only proceed if we haven't opted out +if [ ! -e "$nohighlightfile" ]; then + # attempt to load the highlighter if the repo exists + if [ -d "$targetdir" ]; then + load_highlight + else + # if it isn't found, offer to clone it automatically + echo "zsh-syntax-highlighting not found." + if confirm "Clone from git into $targetdir?"; then + mkdir -p "$targetdir" + git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$targetdir" + load_highlight + clear + else + # we don't want highlighting, don't ask about it again + mkdir -p "$(dirname $nohighlightfile)" + touch "$nohighlightfile" + echo "If you change your mind, delete $nohighlightfile and I'll ask again on your next session." + fi + fi +fi