diff options
Diffstat (limited to 'conf.d/60-prompt.zsh')
-rw-r--r-- | conf.d/60-prompt.zsh | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/conf.d/60-prompt.zsh b/conf.d/60-prompt.zsh new file mode 100644 index 0000000..ad86dd2 --- /dev/null +++ b/conf.d/60-prompt.zsh @@ -0,0 +1,101 @@ +bindkey -e # emacs-style keybindings + +setopt autocd + +setopt prompt_subst prompt_percent +autoload colors && colors + +# wrap in %{%} to prevent zsh from counting the content towards +# the text length, leading to over-compensation for zero-width characters +zcol() { + echo -n "%{$@%}" +} + +timestamp() { + echo $((`date "+%s + %N / 1e9"`)) +} + +elapsed() { + echo $(($(timestamp) - $command_start_time)) +} + +duration=0 +command_start_time=$(timestamp) +# called just before line is evaluated +preexec() { + command_start_time=$(timestamp) +} + +# called just before prompt is updated which +# is usually after the program ran, but also +# on ^C or Enter on an empty line +precmd() { + duration=$(elapsed) + command_start_time=$(timestamp) +} + +git_prompt() { + local git_branch=$(git branch 2>/dev/null | grep \* | sed 's/* //') + # is in git repository if branch != "" + if [[ -n "$git_branch" ]]; then + # if no uncommitted changes + if git diff-index --quiet HEAD --; then + zcol "$fg[green]" + else + zcol "$fg[red]" + fi + echo -n "$git_branch " + fi +} + +left_prompt() { + local last_status=$? + + case $USER in + "$PRIMARY_USER" | "root") ;; + *) echo -n $USER + esac + + if test -n "$SSH_TTY"; then + zcol "$fg[cyan]" + echo -n "@$(hostname | tr -d '\n') " + fi + + zcol "$fg[green]" + { pwd; echo -n " " } | tr -d '\n' | sed -e "s:^$HOME:~:" + + git_prompt + + case $USER in + root) zcol "$fg[red]"; echo -n "# " ;; + *) zcol "$fg[blue]"; echo -n "> " ;; + esac + + if [ "$last_status" -ne 0 ]; then + zcol "$fg_bold[red]" + echo -n "$last_status " + fi + + zcol "$reset_color" +} + +right_prompt() { + command_start_time=$(timestamp) + if (($duration > 0.99)); then + zcol "$fg_bold[cyan]" + printf "%.1fs" "$duration" + fi + + zcol "$reset_color" +} + +PROMPT='$(left_prompt)' +RPROMPT='$(right_prompt)' + +bindkey "${terminfo[khome]}" beginning-of-line +bindkey "${terminfo[kend]}" end-of-line + +bindkey '\eOA' history-substring-search-up # or ^[OA +bindkey '\eOB' history-substring-search-down # or ^[OB + +export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=fg=cyan |