From 7653ea6f937ba69e43af0f6eb4a5e1638d31bdd0 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sun, 15 Apr 2018 16:22:08 +0100 Subject: [PATCH] Add -a option to enable almostontop behaviour * Add option parsing to `prompt_fresh_setup` to detect when options are set during setup. * Add `-a` option to enable almostontop behaviour, every time a command is invoked the screen is cleared and the is prompt redrawn before the command begins using the `preexec` hook. `-a` is disabled by default. * Add `prompt_fresh_help` function to display a description of available options using `prompt -h fresh`. * Canonicalize all utility functions for the fresh prompt theme to follow the `fresh_` convention. Fixes #1. --- prompt_fresh_setup | 47 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/prompt_fresh_setup b/prompt_fresh_setup index 60904ba..1cc5a41 100644 --- a/prompt_fresh_setup +++ b/prompt_fresh_setup @@ -1,8 +1,35 @@ +prompt_fresh_help() { + echo "\ +options: + -a enable almostontop like behaviour" +} + prompt_fresh_setup() { - compile_git_prompt + fresh_compile_git_prompt + + # Parse options + local almostontop=0 + while getopts 'a' opt; do + case $opt in + a) local almostontop=1 ;; + *) prompt -h fresh; return 1 ;; + esac + done autoload -U add-zsh-hook - add-zsh-hook precmd fresh_precmd + + # Hook to print the first line of the "two line" prompt, this line is not + # actually part of the prompt so does not get redrawn which can sometimes + # cause lines before the prompt to disappear. + add-zsh-hook precmd fresh_line_one + + if [ $almostontop -eq 1 ]; then + # Hook to clear the screen then prints the prompt with previous command at + # the top of the screen. + add-zsh-hook preexec fresh_almostontop + else + add-zsh-hook -d preexec fresh_almostontop + fi if [ "$USERNAME" = "root" ]; then local user="%{%F{9}%}%n%{%f%}" @@ -14,7 +41,7 @@ prompt_fresh_setup() { local user="%{%F{35}%}$user%{%f%}@%{%F{244}%}%M%{%f%}" fi - local length=`visible_length "$user"` + local length=`fresh_visible_length "$user"` PS1="«$user» " PS2="${(l:$length + 1:: :)}» " @@ -28,7 +55,7 @@ prompt_cleanup() { if [ -f ~/.cache/zsh/git-prompt ]; then rm ~/.cache/zsh/git-prompt; fi } -fresh_precmd() { +fresh_line_one() { # First get the last commands exit code before doing anything local exit_code=$? @@ -99,7 +126,7 @@ fresh_precmd() { else # The last command failed, display its error code at the right local result="%{%B%F{1}%}$exit_code%{%f%b%}" - local length=`visible_length "$line$result"` + local length=`fresh_visible_length "$line$result"` print -P "$line${(l:COLUMNS - $length - 1:: :)}$result" fi } @@ -115,11 +142,17 @@ fresh_rprompt() { echo $rprompt } -visible_length() { +fresh_almostontop() { + clear + fresh_line_one + print -P "$PROMPT"'$1' +} + +fresh_visible_length() { echo $(( ${#${(S%%)1//(\%(KF1]|)\{*\}|\%[Bbkf])}} )) } -compile_git_prompt() { +fresh_compile_git_prompt() { # Compile a simple C executable which parses the output of `git status # --procelain` to greatly decrease the time taken to render the prompt local cache=~/.cache/zsh