diff --git a/modules/prompt/functions/prompt_craveytrain_setup b/modules/prompt/functions/prompt_craveytrain_setup new file mode 100644 index 00000000..30cf395c --- /dev/null +++ b/modules/prompt/functions/prompt_craveytrain_setup @@ -0,0 +1,177 @@ +#!/usr/bin/env zsh + +# Load dependencies. +pmodload 'helper' + +# Memo-ized var +SEPARATOR="" + +# Begin a segment +# Takes two arguments, foreground and message. Foreground can be omitted, +# rendering default foreground. +prompt_segment() { + local fg + local message + + # If 2 params, 1st one is foreground + if [[ -n $2 ]]; then + # set fg to color passed in + fg="%F{$1}" + message="$2" + else + # otherwise set the fg color to reset value + fg="%f" + message="$1" + fi + + # color the prompt, spit out the message and reset the colors + print -Pn "$SEPARATOR%{$fg%}$message%{%f%}" + + # Let this run after the first run + SEPARATOR=" " +} + +function build_prompt { + # If exit status is non-zero + if [ $? -ne 0 ]; then + prompt_segment red '\u2718' + fi + + if [[ -n "$SSH_CONNECTION" ]]; then + # username + prompt_segment blue '%n' + prompt_segment 'at' + + # hostname + prompt_segment magenta '%m' + prompt_segment 'in' + fi + + # Working directory + prompt_segment cyan '%~' + + # If git is installed and $git_status is defined + if hash git 2>/dev/null && $git_status; then + # if $git_status; then + prompt_segment '${(e)git_info[prompt]}' + if [[ -n "${git_info[status]}" ]]; then + prompt_segment red '${git_info[status]}' + fi + fi +} + +start_time=$SECONDS +function prompt_craveytrain_preexec { + start_time=$SECONDS +} + +function calc_elapsed_time { + 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" + print -P "%B%F{red}>>> elapsed time ${timer_hours}h ${timer_minutes}m ${timer_seconds}s%b" + elif [[ $timer_result -ge 60 ]]; then + let "timer_minutes = $timer_result / 60" + let "timer_seconds = $timer_result % 60" + print -P "%B%F{yellow}>>> elapsed time ${timer_minutes}m ${timer_seconds}s%b" + elif [[ $timer_result -gt 10 ]]; then + print -P "%B%F{green}>>> elapsed time ${timer_result}s%b" + fi +} + +function prompt_craveytrain_precmd { + setopt LOCAL_OPTIONS + unsetopt XTRACE KSH_ARRAYS + RPROMPT='' + + # Get Git repository information. + if (( $+functions[git-info] )); then + git_status=git-info + fi + + # Get Ruby information + if (( $+functions[ruby-info] )); then + ruby-info + if [[ "$ruby_info[version]" != *system* ]]; then + RPROMPT='$ruby_info[version]' + fi + fi + + # Get Python information + if (( $+functions[python-info] )); then + python-info + if [ -n "$python_info[virtualenv]" ]; then + RPROMPT="$RPROMPT $python_info[virtualenv]" + fi + fi + + # Get Node information + if (( $+functions[node-info] )); then + node-info + if [[ "$node_info[version]" != *system* ]]; then + RPROMPT="$RPROMPT $node_info[version]" + fi + fi + + timer_result=$(($SECONDS-$start_time)) + if [[ $timer_result -gt 10 ]]; then + calc_elapsed_time + fi + start_time=$SECONDS +} + +function prompt_craveytrain_setup { + setopt LOCAL_OPTIONS + unsetopt XTRACE KSH_ARRAYS + prompt_opts=(cr percent subst) + + # Load required functions. + autoload -Uz add-zsh-hook + + # Add hook for calling git-info before each command. + add-zsh-hook preexec prompt_craveytrain_preexec + add-zsh-hook precmd prompt_craveytrain_precmd + + # Set ruby-info parameters. + # %v - ruby version. + zstyle ':prezto:module:ruby:info:version' format '%v' + + # Set python-info parameters. + # %v - virtualenv name. + zstyle ':prezto:module:python:info:virtualenv' format 'venv:%v' + + # Set node-info parameters. + # %v - Node.js version. + zstyle ':prezto:module:node:info:version' format 'nvm:%v' + + zstyle ':prezto:module:git:info' verbose 'yes' + zstyle ':prezto:module:editor:info:completing' format '%B%F{red}...%f%b' + zstyle ':prezto:module:editor:info:keymap:primary' format '%B%F{blue}❯%f%b' + zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format '%F{red}♺%f' + zstyle ':prezto:module:editor:info:keymap:alternate' format '%B%F{red}❮%f%b' + zstyle ':prezto:module:git:info:action' format '! %s' + zstyle ':prezto:module:git:info:added' format '✚' + zstyle ':prezto:module:git:info:ahead' format '⬆' + zstyle ':prezto:module:git:info:behind' format '⬇' + zstyle ':prezto:module:git:info:branch' format '\ue0a0 %b' + zstyle ':prezto:module:git:info:commit' format '%.7c' + zstyle ':prezto:module:git:info:deleted' format '✖' + zstyle ':prezto:module:git:info:modified' format '✱' + zstyle ':prezto:module:git:info:position' format '%p' + zstyle ':prezto:module:git:info:renamed' format '➙' + zstyle ':prezto:module:git:info:stashed' format 's' + zstyle ':prezto:module:git:info:unmerged' format '═' + zstyle ':prezto:module:git:info:untracked' format '?' + zstyle ':prezto:module:git:info:keys' format \ + 'prompt' '%F{yellow}$(coalesce "%b" "%p") [%F{226}%c%F{yellow}]%s' \ + 'status' '%A%B%S%a%d%m%r%U%u' + + # Define prompts. + PROMPT='%{%f%b%k%}$(build_prompt) +${editor_info[keymap]} ' + SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? ' +} + +prompt_craveytrain_setup "$@" diff --git a/modules/ruby/functions/ruby-info b/modules/ruby/functions/ruby-info index ebf0f7fe..48a35add 100644 --- a/modules/ruby/functions/ruby-info +++ b/modules/ruby/functions/ruby-info @@ -9,6 +9,7 @@ local version local version_format local version_formatted +local version_manager # Clean up previous $ruby_info. unset ruby_info @@ -16,15 +17,18 @@ typeset -gA ruby_info if (( $+commands[rvm-prompt] )); then version="$(rvm-prompt)" + version_manager="rvm" elif (( $+commands[rbenv] )); then version="$(rbenv version-name)" + version_manager="rbenv" elif (( $+commands[ruby] )); then version="${${$(ruby --version)[(w)1,(w)2]}/ /-}" + version_manager="ruby" fi # Format version. if [[ -n "$version" ]]; then zstyle -s ':prezto:module:ruby:info:version' format 'version_format' - zformat -f version_formatted "$version_format" "v:$version" + zformat -a version_formatted ":" "$version_manager:$version" ruby_info[version]="$version_formatted" fi