From 112ee2b097c69e53db258e94c05b1c0499e86cd5 Mon Sep 17 00:00:00 2001 From: Marcel Schoengens Date: Tue, 14 Aug 2018 15:01:30 +0200 Subject: [PATCH] Configured more sophisticated prompt. git push --- zsh/zshrc | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/zsh/zshrc b/zsh/zshrc index 1a71051..0a5f742 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -15,7 +15,7 @@ fi ### Configure oh-my-zsh -ZSH_THEME="pygmalion" +# ZSH_THEME="pygmalion" plugins=( git @@ -30,6 +30,83 @@ source $ZSH/oh-my-zsh.sh ### Configure zsh +# Configure prompt + +prompt_segment() { + [[ -n $1 ]] && color=$1 || return + [[ -n $2 ]] && text='%{$fg['$color']%}'$2'%{$reset_color%}' || return + [[ -n $3 ]] && sep='%{$fg[red]%}'$3'%{$reset_color%}' || sep='' + echo -n $sep$text +} + +prompt_git() { + (( $+commands[git] )) || return + if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then + ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[green]%}" + ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}" + ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[yellow]%}⚡%{$reset_color%}" + ZSH_THEME_GIT_PROMPT_CLEAN="" + echo -n "git:$(git_prompt_info)" + fi +} + +prompt_virtualenv() { + local virtualenv_path="$VIRTUAL_ENV" + if [[ -n $virtualenv_path ]]; then + text="penv:`basename $virtualenv_path`" + echo -n $text + fi +} + +prompt_condaenv() { + local env_path="$CONDA_PREFIX" + if [[ -n $env_path ]]; then + text="cenv:`basename $env_path`" + echo -n $text + fi +} + +prompt_extensions=(prompt_virtualenv prompt_condaenv prompt_git) + +build_prompt() { + # build base prompt + PROMPT=$(prompt_segment magenta '%n') + PROMPT+=$(prompt_segment red '@') + PROMPT+=$(prompt_segment yellow '%m') + PROMPT+=$(prompt_segment red ':') + PROMPT+=$(prompt_segment cyan '%0~') + + # build prompt for extensions + local prompt_ext_text="" + local sep="" + for ext in $prompt_extensions; do + local ext_text=$(eval $ext) + if [[ -n $prompt_ext_text ]] && [[ -n $ext_text ]]; then + sep="," + else + sep="" + fi + prompt_ext_text+=$(prompt_segment green $ext_text $sep) + done + + # build prompt base - extension separator + if [[ -n $prompt_ext_text ]]; then + PROMPT+=$(prompt_segment red '|') + fi + PROMPT+=$prompt_ext_text + + # build prompt suffix + local prompt_nocolor=$(echo $PROMPT | perl -pe "s/%\{[^}]+\}//g") + local prompt_length=${#prompt_nocolor} + if [[ $prompt_length -gt 40 ]]; then + PROMPT+=$(prompt_segment cyan '\n%{\r%}⇒ ') + else + PROMPT+=$(prompt_segment cyan ' ⇒ ') + fi +} + +precmd_functions+=(build_prompt) + HISTSIZE=20000 HISTFILE=~/.zsh_history SAVEHIST=$HISTSIZE