Last active
August 29, 2015 14:17
-
-
Save GeertJohan/ec25e8a13d42d42b7537 to your computer and use it in GitHub Desktop.
.zprezto/modules/prompt/functions/prompt_geertjohan_setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A three-line prompt displaying exit status, location, git info, and more. | |
# | |
# Authors: | |
# Geert-Johan Riemer <[email protected]> | |
# | |
# Load dependencies. | |
pmodload 'helper' | |
# turns seconds into human readable time | |
# 165392 => 1d 21h 56m 32s | |
function prompt_geertjohan_cmdhumantime { | |
local result_name=$1 | |
local timer_result=$2 | |
if [[ $timer_result -ge 3600 ]]; then | |
let "timer_hours = $timer_result / 3600" | |
let "remainder = $timer_result % 3600" | |
let "timer_minutes = $remainder / 60" | |
let "timer_seconds = $remainder % 60" | |
declare -g ${result_name}=" ${timer_hours}h${timer_minutes}m${timer_seconds}s " | |
elif [[ $timer_result -ge 60 ]]; then | |
let "timer_minutes = $timer_result / 60" | |
let "timer_seconds = $timer_result % 60" | |
declare -g ${result_name}=" ${timer_minutes}m${timer_seconds}s " | |
elif [[ $timer_result -gt 1 ]]; then | |
declare -g ${result_name}=" ${timer_result}s " | |
fi | |
} | |
function prompt_geertjohan_pwd { | |
local pwd="${PWD/#$HOME/~}" | |
if [[ "$pwd" == (#m)[/~] ]]; then | |
_prompt_geertjohan_pwd="$MATCH" | |
unset MATCH | |
else | |
_prompt_geertjohan_pwd="${${${${(@j:/:M)${(@s:/:)pwd}##.#?}:h}%/}//\%/%%}/${${pwd:t}//\%/%%}" | |
fi | |
} | |
function prompt_geertjohan_exitstatus { | |
_prompt_geertjohan_exitstatus='%(?.%U%F{077} ✔ .%F{160}%U ✘ %u%F{white}%K{160} %? )%f%k' | |
} | |
function prompt_pure_preexec { | |
_prompt_geertjohan_cmdtimestamp=$SECONDS | |
} | |
function prompt_geertjohan_precmd { | |
setopt LOCAL_OPTIONS | |
unsetopt XTRACE KSH_ARRAYS | |
# Format PWD. | |
prompt_geertjohan_pwd | |
# Prepare exitstatus line | |
prompt_geertjohan_exitstatus | |
# Get Git repository information. | |
if (( $+functions[git-info] )); then | |
git-info | |
fi | |
if [[ $_prompt_geertjohan_cmdtimestamp -gt 0 ]]; then | |
timer_result=$(($SECONDS-$_prompt_geertjohan_cmdtimestamp)) | |
if [[ $timer_result -gt 1 ]]; then | |
prompt_geertjohan_cmdhumantime "_prompt_geertjohan_cmdhumantime_value" $timer_result | |
else | |
_prompt_geertjohan_cmdhumantime_value='' | |
fi | |
fi | |
# reset value since `preexec` isn't always triggered | |
unset _prompt_geertjohan_cmdtimestamp | |
} | |
function prompt_geertjohan_setup { | |
setopt LOCAL_OPTIONS | |
unsetopt XTRACE KSH_ARRAYS | |
prompt_opts=(cr percent subst) | |
export PROMPT_EOL_MARK="%B%K{red} ⏎ %k%b" | |
# Load required functions. | |
autoload -Uz add-zsh-hook | |
# Add hook for calling git-info before each command. | |
add-zsh-hook precmd prompt_geertjohan_precmd | |
add-zsh-hook preexec prompt_pure_preexec | |
# Set editor-info parameters. | |
zstyle ':prezto:module:editor:info:completing' format '%B%F{red}...%f%b' | |
zstyle ':prezto:module:editor:info:keymap:primary' format ' %B%F{red}❯%F{yellow}❯%F{green}❯%f%b' | |
zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format ' %F{red}♺%f' | |
zstyle ':prezto:module:editor:info:keymap:alternate' format ' %B%F{green}❮%F{yellow}❮%F{red}❮%f%b' | |
# Set git-info parameters. | |
zstyle ':prezto:module:git:info' verbose 'yes' | |
zstyle ':prezto:module:git:info:branch' format '%F{green}%b%f' | |
zstyle ':prezto:module:git:info:action' format ':%%B%F{yellow}%s%f%%b' | |
zstyle ':prezto:module:git:info:added' format ' %%B%F{green}✚%f%%b' | |
zstyle ':prezto:module:git:info:ahead' format ' %%B%F{yellow}⬆%f%%b' | |
zstyle ':prezto:module:git:info:behind' format ' %%B%F{yellow}⬇%f%%b' | |
zstyle ':prezto:module:git:info:branch' format ':%F{green}%b%f' | |
zstyle ':prezto:module:git:info:commit' format ':%F{green}%.7c%f' | |
zstyle ':prezto:module:git:info:deleted' format ' %%B%F{red}✖%f%%b' | |
zstyle ':prezto:module:git:info:modified' format ' %%B%F{blue}✱%f%%b' | |
zstyle ':prezto:module:git:info:position' format ':%F{red}%p%f' | |
zstyle ':prezto:module:git:info:renamed' format ' %%B%F{magenta}➜%f%%b' | |
zstyle ':prezto:module:git:info:stashed' format ' %%B%F{cyan}✭%f%%b' | |
zstyle ':prezto:module:git:info:unmerged' format ' %%B%F{yellow}═%f%%b' | |
zstyle ':prezto:module:git:info:untracked' format ' %%B%F{white}◼%f%%b' | |
zstyle ':prezto:module:git:info:keys' format \ | |
'prompt' ' $(coalesce "%b" "%p" "%c")%s' \ | |
'status' '%A%B%S%a%d%m%r%U%u' | |
# Define prompts. | |
PROMPT='${_prompt_geertjohan_exitstatus}%U%F{244}${_prompt_geertjohan_cmdhumantime_value}%f%u | |
%B%F{yellow}${_prompt_geertjohan_pwd}%f%b${git_info:+${(e)git_info[prompt]}}$git_info[status]%(!. %B%F{red}#%f%b.) %f | |
%B%F{red}$ %f%b' | |
RPROMPT='${editor_info[overwrite]}%(?:: %F{red}⏎%f)' | |
SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? ' | |
} | |
prompt_geertjohan_setup "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment