10up / dotfiles

3 stars 2 forks source link

VCS CLI #3

Open tddewey opened 11 years ago

tddewey commented 11 years ago

It would be good to show VCS information in the CLI prompt. I used a tool here: http://andrewray.me/bash-prompt-builder/

I think @helenhousandi has an even more exciting version, at least for SVN.

Example of what it looks like:

image

# This code was auto generated by with these options:
# http://andrewray.me/bash-prompt-builder/index.html#git=1&color-git=3&git-prefix=1&color-git-prefix=3&git-ahead=1&color-git-ahead=6b&git-modified=1&color-git-modified=3&git-conflicted=1&color-git-conflicted=1&git-revno=1&color-git-revno=3&git-bisect=1&color-git-bisect=5&option-submodule=1&color-option-submodule=5&git-ontag=1&color-git-ontag=3&color-hg=5&color-hg-prefix=5&color-hg-modified=5&color-hg-conflicted=1&color-hg-revno=5&color-hg-bisect=3&color-hg-patches=3&svn=1&color-svn=6&svn-modified=1&color-svn-modified=6&svn-revno=1&color-svn-revno=6&option-modified=%E2%9F%81&option-conflict=%E2%98%A2&color-option-conflict=3&max-conflicted-files=2&option-nobranch=no%20branch!&color-option-nobranch=1&bisecting-text=bisecting&submodule-text=%5Bsubmodule%5D%20

MAX_CONFLICTED_FILES=2
DELTA_CHAR="⟁"
CONFLICT_CHAR="☢"
BISECTING_TEXT="bisecting"
NOBRANCH_TEXT="no branch!"
REBASE_TEXT="✂ ʀebase"
SUBMODULE_TEXT="[submodule] "

# Colors for prompt
COLOR_RED=$(tput sgr0 && tput setaf 1)
COLOR_GREEN=$(tput sgr0 && tput setaf 2)
COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
COLOR_BLUE=$(tput sgr0 && tput setaf 4)
COLOR_MAGENTA=$(tput sgr0 && tput setaf 5)
COLOR_CYAN=$(tput sgr0 && tput setaf 6)
COLOR_GRAY=$(tput sgr0 && tput setaf 7)
COLOR_WHITE=$(tput sgr0 && tput setaf 7 && tput bold)
COLOR_LIGHTRED=$(tput sgr0 && tput setaf 1 && tput bold)
COLOR_LIGHTGREEN=$(tput sgr0 && tput setaf 2 && tput bold)
COLOR_LIGHTYELLOW=$(tput sgr0 && tput setaf 3 && tput bold)
COLOR_LIGHTBLUE=$(tput sgr0 && tput setaf 4 && tput bold)
COLOR_LIGHTMAGENTA=$(tput sgr0 && tput setaf 5 && tput bold)
COLOR_LIGHTCYAN=$(tput sgr0 && tput setaf 6 && tput bold)

COLOR_RESET=$(tput sgr0)

_svn_dir=""
function _svn_check {
    parent=""
    grandparent="."

    while [ -d "$grandparent/.svn" ]; do
        parent=$grandparent
        grandparent="$parent/.."
    done

    if [ ! -z "$parent" ]; then
        _svn_dir=`cd "$parent"; pwd`
        return 0
    else
        return 1
    fi
}

_git_dir=""
_git_svn_dir=""
function _git_check {
    _git_dir=`git rev-parse --show-toplevel 2> /dev/null`
    if [[ "$_git_dir" == "" ]]; then
        return 1
    else
        _gsvn_check=`cd "$_git_dir"; ls .git/svn/.metadata 2> /dev/null`

        if [[ ! -z "$_gsvn_check" ]]; then
            _git_svn_dir=$_git_dir
        fi
        return 0
    fi
}

function is_submodule() {
    local parent_git=`cd "$_git_dir/.." && git rev-parse --show-toplevel 2> /dev/null`
    if [[ -n $parent_git ]]; then
        local submodules=`cd "$parent_git" && git submodule --quiet foreach 'echo $path'`
        for line in $submodules; do
            cd "$parent_git/$line"
            if [[ `pwd` = $_git_dir ]]; then return 0; fi
        done
    fi
    return 1
}

# animated gifs from any video
# from alex sexton   gist.github.com/SlexAxton/4989674
gifify() {
  if [[ -n "$1" ]]; then
    if [[ $2 == '--good' ]]; then
      ffmpeg -i $1 -r 10 -vcodec png out-static-%05d.png
      time convert -verbose +dither -layers Optimize -resize 600x600\> out-static*.png  GIF:- | gifsicle --colors 128 --delay=5 --loop --optimize=3 --multifile - > $1.gif
      rm out-static*.png
    else
      ffmpeg -i $1 -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > $1.gif
    fi
  else
    echo "proper usage: gifify <input_movie.mov>. You DO need to include extension."
  fi
}

dvcs_function="
    # Figure out what repo we are in
    _git_check \
        || _svn_check

    # Build the prompt!
    prompt=\"\"

    # If we are in git ...
    if [ -n \"\$_git_dir\" ]; then
        # find current branch
        gitBranch=\$(git symbolic-ref HEAD 2> /dev/null)
        gitStatus=\`git status\`

        # Figure out if we are rebasing
        if [[ -d \"\$_git_dir/.git/rebase-apply\" || -d \"\$_git_dir/.git/rebase-merge\" ]]; then
            is_rebase=1
        fi

        # Figure out current branch, or if we are bisecting, or lost in space
        bisecting=\"\"
        if [ -z \"\$gitBranch\" ]; then
        if [ -n \"\$is_rebase\" ]; then
            rebase_prompt=\" \\[\$COLOR_LIGHT_CYAN\\]\"
            rebase_prompt=\$rebase_prompt\"\\[`tput sc`\\]  \\[`tput rc`\\]\\[\$REBASE_TEXT\\] \"
            rebase_prompt=\$rebase_prompt\"\\[\$COLOR_YELLOW\\]\"
        else
            bisect=\$(git rev-list --bisect 2> /dev/null | cut -c1-7)
            if [ -z \"\$bisect\" ]; then
            gitBranch=\"\\[\$COLOR_RED\\]\$NOBRANCH_TEXT\\[\$COLOR_YELLOW\\]\"
        else
            bisecting=\"\\[\$COLOR_MAGENTA\\]\$BISECTING_TEXT:\"\$bisect\"\\[\$COLOR_YELLOW\\]\"
            gitBranch=\"\"
        fi
        fi
        fi
        gitBranch=\${gitBranch#refs/heads/}
        #: git-svn
        if [ -z \"\$bisect\" ]; then
            if [ -n \"\$_git_svn_dir\" ]; then
                gitBranch=\"\\[\$COLOR_DARK_BLUE\\]git-svn\\[\$COLOR_YELLOW\\] \$gitBranch\"
            fi
        fi

        if [ -z \"\$is_rebase\" ]; then
            # changed *tracked* files in local directory?
            gitChange=\$(echo \$gitStatus | ack 'modified:|deleted:|new file:')
            if [ -n \"\$gitChange\" ]; then
                gitChange=\"\\[\$COLOR_YELLOW\\] \\[`tput sc`\\]  \\[`tput rc`\\]\\[\$DELTA_CHAR\\] \"
            fi
        fi

            # output the branch and changed character if present
            prompt=\$prompt\"\\[\$COLOR_YELLOW\\] (\"

            if is_submodule; then
                prompt=\$prompt\"\\[\$COLOR_MAGENTA\\]\$SUBMODULE_TEXT\\[\$COLOR_YELLOW\\]\"
            fi

            prefix=\"\\[\$COLOR_YELLOW\\]git:\\[\$COLOR_YELLOW\\]\"
            prompt=\$prompt\$prefix\$gitBranch\$bisecting
            revNo=\$(git rev-parse HEAD 2> /dev/null | cut -c1-7) || return

            # If bisecting don't output revno
            if [ -z \"\$bisecting\" ] && [ -z \"\$noBranch\" ];then
                prompt=\$prompt\":\$revNo\"
            fi
            tag=\`git describe --tags --exact 2> /dev/null\`
            if [ -n \"\$tag\" ]; then
                prompt=\"\$prompt\\[\$COLOR_YELLOW\\] \\\"\$tag\\\"\\[\$COLOR_YELLOW\\]\"
            fi
            prompt=\$prompt\"\$gitChange\\[\$COLOR_YELLOW\\])\\[\$COLOR_RESET\\]\"

            # How many local commits do you have ahead of origin?
            num=\$(echo \$gitStatus | grep \"Your branch is ahead of\" | awk '{split(\$0,a,\" \"); print a[13];}') || return
            if [ -n \"\$num\" ]; then
                prompt=\$prompt\"\\[\$COLOR_CYAN\\] +\$num\"
            fi

            # any conflicts? (sed madness is to remove line breaks)
            files=\$(git ls-files -u | cut -f 2 | sort -u | sed '$(($MAX_CONFLICTED_FILES+1)),1000d' | sed -e :a -e '\$!N;s/\\\n/, /;ta' -e 'P;D')
        fi

    # If we are in subversion ...
    if [ -n \"\$_svn_dir\" ]; then

        # changed files in local directory? NOTE: This command is the slowest of the bunch
        svnChange=\$(svn status | ack \"^M|^!\" | wc -l)
        if [[ \"\$svnChange\" != \"       0\" ]]; then
            svnChange=\"\\[\$COLOR_CYAN\\] \\[`tput sc`\\]  \\[`tput rc`\\]\\[\$DELTA_CHAR\\] \"
        else
            svnChange=\"\"
        fi

        # revision number (instead of branch name, silly svn)
        revNo=\`svnversion --no-newline\`
        prompt=\$prompt\"\\[\$COLOR_CYAN\\] (svn\"
        prompt=\$prompt\"\\[\$COLOR_CYAN\\] :\$revNo\\[\$COLOR_CYAN\\] \"
        prompt=\$prompt\"\$svnChange)\\[\$COLOR_RESET\\]\"
    fi

    # Show conflicted files if any
    if [ -n \"\$files\" ]; then
        prompt=\$prompt\" \\[\$COLOR_RED\\](\\[\$COLOR_YELLOW\\]\"
        prompt=\$prompt\"\\[`tput sc`\\]  \\[`tput rc`\\]\\[\$COLOR_YELLOW\\]\\[\$CONFLICT_CHAR\\] \"
        prompt=\$prompt\"\\[\$COLOR_RED\\] \${files})\"
    fi

    echo -e \$prompt"
# End code auto generated by http://andrewray.me/bash-prompt-builder/index.html

PS1="\$(${dvcs_function})\[$COLOR_RESET\] \$ "
##
johnpbloch commented 11 years ago

I'll have to try that out. Impromptu seems like a bit much for us to bundle here.