Skip to content

Commit

Permalink
Kaizen on prompt
Browse files Browse the repository at this point in the history
* Add virtualenv to prompt
* Make prompt smarter with spaces
  • Loading branch information
roman committed Jul 28, 2014
1 parent 4fc25d0 commit a89322c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 65 deletions.
2 changes: 2 additions & 0 deletions bashrc
Expand Up @@ -51,6 +51,8 @@ source $modules/localbin.sh
source $modules/ruby.sh
# == RVM config
source $modules/rvm.sh
# == Virtualenv config
source $modules/virtualenv.sh
# == Hitch (Pair Programming)
source $modules/hitch.sh
# == Load the EC2 config if available
Expand Down
3 changes: 1 addition & 2 deletions modules/colors.sh
Expand Up @@ -7,7 +7,7 @@ if [ $platform = 'Linux' ]
then
alias ls='ls --color'
elif [ $platform = 'Darwin' ]
then
then
# Allow the ls command to have colors when displaying directories
export CLICOLOR=1
export LSCOLOR=ExFxCxDxBxegedabagacad
Expand All @@ -20,4 +20,3 @@ export LIGHT_BLACK_BG="\033[1;40m"
export LIGHT_YELLOW_FG="\033[1;33m"
export LIGHT_MAGENTA_FG="\033[1;35m"
export RESET="\033[0m"

145 changes: 82 additions & 63 deletions modules/prompt.sh
Expand Up @@ -4,67 +4,67 @@

# Displays the info of the git repo on the prompt
function perform_git_check {
local result=$?
local result=$?

local git_branch=`git branch 2> /dev/null`
if [[ -n git_branch ]]; then
local git_status=`git status 2> /dev/null`
local git_branch=`git branch 2> /dev/null`
if [[ -n git_branch ]]; then
local git_status=`git status 2> /dev/null`

local branch=`prompt_git_current_branch "$git_branch"`
local index_files=`prompt_git_is_there_files_on_index "$git_status"`
local new_files=`prompt_git_is_there_new_files "$git_status"`
local modified_files=`prompt_git_is_there_modified_files "$git_status"`
echo "$branch$index_files$modified_files$new_files"
fi
local branch=`prompt_git_current_branch "$git_branch"`
local index_files=`prompt_git_is_there_files_on_index "$git_status"`
local new_files=`prompt_git_is_there_new_files "$git_status"`
local modified_files=`prompt_git_is_there_modified_files "$git_status"`
echo "$branch$index_files$modified_files$new_files"
fi

exit $result
exit $result
}

# Prints an exclamation (!) whenever
# there is a modified file (not on the index)
function prompt_git_is_there_modified_files {

git_result=`echo "$1" | sed -n '/Changes not staged for commit:/p'`
git_result=`echo "$1" | sed -n '/Changes not staged for commit:/p'`

if [ -n "$git_result" ]; then
printf "$LIGHT_RED_FG[!]$RESET"
fi
if [ -n "$git_result" ]; then
printf "$LIGHT_RED_FG[!]$RESET"
fi

git_result=`echo "$1" | sed -n '/Changed but not updated:/p'`
git_result=`echo "$1" | sed -n '/Changed but not updated:/p'`

if [ -n "$git_result" ]; then
printf "$LIGHT_RED_FG[!]$RESET"
fi
if [ -n "$git_result" ]; then
printf "$LIGHT_RED_FG[!]$RESET"
fi

}

# Prints an star (*) whenever
# there is a modified file (on the index)
function prompt_git_is_there_files_on_index {
local git_result=`echo "$1" | sed -n '/Changes to be committed:/p'`
local git_result=`echo "$1" | sed -n '/Changes to be committed:/p'`

if [[ -n "$git_result" ]]; then
printf "$LIGHT_GREEN_FG[*]$RESET"
fi
if [[ -n "$git_result" ]]; then
printf "$LIGHT_GREEN_FG[*]$RESET"
fi
}

# Prints a question mark (?) whenever
# there is a not versioned file on the repo
function prompt_git_is_there_new_files {
local git_result=`echo $1 | sed -n '/Untracked files:/p'`
local git_result=`echo $1 | sed -n '/Untracked files:/p'`

if [[ -n "$git_result" ]]; then
printf "$LIGHT_YELLOW_FG[?]$RESET"
fi
if [[ -n "$git_result" ]]; then
printf "$LIGHT_YELLOW_FG[?]$RESET"
fi
}


# Prints the current git branch on the repo, in
# case there is none, it simply returns an empty string
#
function prompt_git_current_branch {
local result=`echo "$1" | sed -e '/^[^*]/d' -e 's/* \(.*\)/- git: \1 /'`
printf "$LIGHT_GREEN_FG$result$RESET"
local result=`echo "$1" | sed -e '/^[^*]/d' -e 's/* \(.*\)/- git: \1 /'`
printf "$LIGHT_GREEN_FG$result$RESET"
}

### Last Command utilities
Expand All @@ -76,62 +76,79 @@ function prompt_git_current_branch {
# string
#
function last_command_result {
local result=$?
local result=$?

if [[ $result -ne 0 ]]; then
# print a green "$" sign if the last command was successful
echo "(╯°□°)╯︵ ┻━┻ [$result] "
echo ""
fi
if [[ $result -ne 0 ]]; then
# print a green "$" sign if the last command was successful
echo "(╯°□°)╯︵ ┻━┻ [$result] "
echo ""
fi

exit $result
exit $result
}

# Returns a color from the previous status code, if
# there was a failure, returns a red color, a green
# otherwise
function color_for_last_command_result {
local result=$?
local result=$?

if [[ $result -eq 0 ]]; then
echo -e "$LIGHT_GREEN_FG"
else
echo -e "$LIGHT_RED_FG"
fi
if [[ $result -eq 0 ]]; then
echo -e "$LIGHT_GREEN_FG"
else
echo -e "$LIGHT_RED_FG"
fi

exit $result
exit $result
}

function color_for_user {
local result=$?
local user=`whoami`
local result=$?
local user=`whoami`

if [[ $user = 'vagrant' ]]; then
echo -e "$LIGHT_MAGENTA_FG"
fi
if [[ $user = 'vagrant' ]]; then
echo -e "$LIGHT_MAGENTA_FG"
fi

exit $result
exit $result
}

function get_current_gemset {
local result=$?
if [[ $(command -v rvm 2>&1 /dev/null) ]]; then
local current_gemset=`rvm gemset list | grep '=>' | awk '{ print $2 }' 2> /dev/null`
if [[ -n current_gemset ]] && [[ $current_gemset != '(default)' ]]; then
printf "$LIGHT_MAGENTA_FG(gemset: $current_gemset)$RESET "
local result=$?
if [[ $(command -v rvm 2>&1 /dev/null) ]]; then
local current_gemset=$(rvm-prompt i g)
if [[ $(echo $current_gemset | grep '@') ]]; then
printf "$LIGHT_MAGENTA_FG[$current_gemset]$RESET"
fi
fi
fi
exit $result
}

exit $result
function get_current_virtualenv {
local result=$?
if [[ -e $VIRTUAL_ENV ]]; then
printf "$LIGHT_GREEN_FG[`basename $VIRTUAL_ENV`]$RESET"
fi
exit $result
}

function get_ghc_sandboxed {
local result=$?
ls | grep 'cabal.sandbox.config' > /dev/null
if [[ $? = 0 ]]; then
printf "$LIGHT_YELLOW_FG(cabal: sandboxed)$RESET "
fi
exit $result
local result=$?
ls | grep 'cabal.sandbox.config' > /dev/null
if [[ $? = 0 ]]; then
printf "$LIGHT_YELLOW_FG[sandboxed]$RESET"
fi
exit $result
}

function print_env {
local result=$?
local env_string="$(get_ghc_sandboxed)$(get_current_virtualenv)$(get_current_gemset)"
local env_string_size=${#env_string}
if [[ $env_string_size -gt 0 ]]; then
printf "$env_string "
fi
exit $?
}

# This prompt should print:
Expand All @@ -147,6 +164,8 @@ function get_ghc_sandboxed {
# We are not using this escaping on the git repo info, this is because
# the line they are in doesn't have any input, so we don't have to bother
# about that
export PS1="\n\$(get_ghc_sandboxed)\$(get_current_gemset)\$(color_for_user)\u$RESET@\h: \[$LIGHT_RED_FG\]\w\[$RESET\] \

#export PS1="\n\$(get_ghc_sandboxed)\$(get_current_virtualenv)\$(get_current_gemset) \$(color_for_user)\u$RESET@\h: \[$LIGHT_RED_FG\]\w\[$RESET\] \
export PS1="\n\$(print_env)\$(color_for_user)\u$RESET@\h: \[$LIGHT_RED_FG\]\w\[$RESET\] \
\$(perform_git_check)\n\
\[\$(color_for_last_command_result)\]\$(last_command_result)\$\[$RESET\] "
6 changes: 6 additions & 0 deletions modules/virtualenv.sh
@@ -0,0 +1,6 @@
#!/bin/bash

export VIRTUAL_ENV_DISABLE_PROMPT=1
if [[ $(command -v virtualenvwrapper.sh 2>&1 /dev/null) ]]; then
source `which virtualenvwrapper.sh`
fi

0 comments on commit a89322c

Please sign in to comment.