Merge pull request #262 from bobthecow/fish-all-the-fish

This commit is contained in:
Bruno Pinto 2014-11-28 19:42:07 +11:00
commit 06b35235ff
2 changed files with 116 additions and 74 deletions

View File

@ -28,6 +28,8 @@ This theme is based loosely on [agnoster][agnoster].
* Background jobs (%)
* You currently have superpowers ($)
* User@Host (unless you're the default user)
* Current RVM or rbenv (Ruby) version
* Current virtualenv (Python) version
* Abbreviated parent directory
* Current directory, or Git or Mercurial project name
* Current project's repo branch ( master) or detached head (➦ d0dfd9b)
@ -43,6 +45,18 @@ This theme is based loosely on [agnoster][agnoster].
* Abbreviated project-relative path
### Configuration
You can override some default options in your `config.fish`:
set -g theme_display_git no
set -g theme_display_hg yes
set -g theme_display_virtualenv no
set -g theme_display_ruby no
set -g theme_display_user yes
set -g default_user your_normal_user
[screenshot]: http://i.0x7f.us/bobthefish.png
[patching]: https://powerline.readthedocs.org/en/latest/fontpatching.html
[fonts]: https://github.com/Lokaltog/powerline-fonts

View File

@ -12,6 +12,10 @@
#
# You can override some default options in your config.fish:
#
# set -g theme_display_git no
# set -g theme_display_hg yes
# set -g theme_display_virtualenv no
# set -g theme_display_ruby no
# set -g theme_display_user yes
# set -g default_user your_normal_user
@ -46,8 +50,10 @@ set __bobthefish_dk_green 0c4801
set __bobthefish_lt_red C99
set __bobthefish_med_red ce000f
set __bobthefish_dk_red 600
set __bobthefish_ruby_red af0000
set __bobthefish_slate_blue 255e87
set __bobthefish_med_blue 005faf
set __bobthefish_lt_orange f6b117
set __bobthefish_dk_orange 3a2a03
@ -60,51 +66,57 @@ set __bobthefish_dk_brown 4d2600
set __bobthefish_med_brown 803F00
set __bobthefish_lt_brown BF5E00
set __bobthefish_dk_blue 1E2933
set __bobthefish_med_blue 275379
set __bobthefish_lt_blue 326D9E
# ===========================
# Helper methods
# ===========================
function __bobthefish_in_git -d 'Check whether pwd is inside a git repo'
command which git > /dev/null 2>&1; and command git rev-parse --is-inside-work-tree >/dev/null 2>&1
end
# function __bobthefish_in_git -d 'Check whether pwd is inside a git repo'
# command which git > /dev/null 2>&1; and command git rev-parse --is-inside-work-tree >/dev/null 2>&1
# end
function __bobthefish_in_hg -d 'Check whether pwd is inside a hg repo'
command which hg > /dev/null 2>&1; and command hg stat > /dev/null 2>&1
end
# function __bobthefish_in_hg -d 'Check whether pwd is inside a hg repo'
# command which hg > /dev/null 2>&1; and command hg stat > /dev/null 2>&1
# end
function __bobthefish_git_branch -d 'Get the current git branch (or commitish)'
set -l ref (command git symbolic-ref HEAD 2> /dev/null)
set -l ref (command git symbolic-ref HEAD ^/dev/null)
if [ $status -gt 0 ]
set -l branch (command git show-ref --head -s --abbrev |head -n1 2> /dev/null)
set -l branch (command git show-ref --head -s --abbrev | head -n1 ^/dev/null)
set ref "$__bobthefish_detached_glyph $branch"
end
echo $ref | sed "s-refs/heads/-$__bobthefish_branch_glyph -"
echo $ref | sed "s#refs/heads/#$__bobthefish_branch_glyph #"
end
function __bobthefish_hg_branch -d 'Get the current hg branch'
set -l branch (hg branch ^/dev/null)
set -l book " @ "(hg book | grep \* | cut -d\ -f3)
set -l branch (command hg branch ^/dev/null)
set -l book " @ "(command hg book | grep \* | cut -d\ -f3)
echo "$__bobthefish_branch_glyph $branch$book"
end
function __bobthefish_pretty_parent -d 'Print a parent directory, shortened to fit the prompt'
echo -n (dirname $argv[1]) | sed -e 's|/private||' -e "s|^$HOME|~|" -e 's-/\(\.\{0,1\}[^/]\)\([^/]*\)-/\1-g' -e 's|/$||'
echo -n (dirname $argv[1]) | sed -e 's#/private##' -e "s#^$HOME#~#" -e 's#/\(\.\{0,1\}[^/]\)\([^/]*\)#/\1#g' -e 's#/$##'
end
function __bobthefish_git_project_dir -d 'Print the current git project base directory'
command git rev-parse --show-toplevel 2>/dev/null
[ "$theme_display_git" = 'no' ]; and return
command git rev-parse --show-toplevel ^/dev/null
end
function __bobthefish_hg_project_dir -d 'Print the current hg project base directory'
command hg root 2>/dev/null
[ "$theme_display_hg" = 'yes' ]; or return
set d (pwd)
while not [ $d = / ]
if [ -e $d/.hg ]
command hg root --cwd "$d" ^/dev/null
return
end
set d (dirname $d)
end
end
function __bobthefish_project_pwd -d 'Print the working directory relative to project root'
echo "$PWD" | sed -e "s*$argv[1]**g" -e 's*^/**'
echo "$PWD" | sed -e "s#$argv[1]##g" -e 's#^/##'
end
@ -113,28 +125,34 @@ end
# ===========================
function __bobthefish_start_segment -d 'Start a prompt segment'
set_color -b $argv[1]
set_color $argv[2]
set -l bg $argv[1]
set -e argv[1]
set -l fg $argv[1]
set -e argv[1]
set_color normal # clear out anything bold or underline...
set_color -b $bg
set_color $fg $argv
if [ "$__bobthefish_current_bg" = 'NONE' ]
# If there's no background, just start one
echo -n ' '
else
# If there's already a background...
if [ "$argv[1]" = "$__bobthefish_current_bg" ]
if [ "$bg" = "$__bobthefish_current_bg" ]
# and it's the same color, draw a separator
echo -n "$__bobthefish_right_arrow_glyph "
else
# otherwise, draw the end of the previous segment and the start of the next
set_color $__bobthefish_current_bg
echo -n "$__bobthefish_right_black_arrow_glyph "
set_color $argv[2]
set_color $fg $argv
end
end
set __bobthefish_current_bg $argv[1]
set __bobthefish_current_bg $bg
end
function __bobthefish_path_segment -d 'Display a shortened form of a directory'
if test -w "$argv[1]"
if [ -w "$argv[1]" ]
__bobthefish_start_segment $__bobthefish_dk_grey $__bobthefish_med_grey
else
__bobthefish_start_segment $__bobthefish_dk_red $__bobthefish_lt_red
@ -154,7 +172,7 @@ function __bobthefish_path_segment -d 'Display a shortened form of a directory'
set directory (basename "$argv[1]")
end
test "$parent"; and echo -n -s "$parent"
[ "$parent" ]; and echo -n -s "$parent"
set_color fff --bold
echo -n "$directory "
set_color normal
@ -198,7 +216,7 @@ function __bobthefish_prompt_status -d 'Display symbols for a non zero exit stat
set -l status_flags "$nonzero$superuser$bg_jobs"
if test "$nonzero" -o "$superuser" -o "$bg_jobs"
if [ "$nonzero" -o "$superuser" -o "$bg_jobs" ]
__bobthefish_start_segment fff 000
if [ "$nonzero" ]
set_color $__bobthefish_med_red --bold
@ -232,28 +250,27 @@ function __bobthefish_prompt_hg -d 'Display the actual hg state'
set -l dirty (command hg stat; or echo -n '*')
set -l flags "$dirty"
test "$flags"; and set flags ""
[ "$flags" ]; and set flags ""
set -l flag_bg $__bobthefish_lt_green
set -l flag_fg $__bobthefish_dk_green
if test "$dirty"
if [ "$dirty" ]
set flag_bg $__bobthefish_med_red
set flag_fg fff
end
__bobthefish_path_segment (__bobthefish_hg_project_dir)
__bobthefish_path_segment $argv[1]
__bobthefish_start_segment $flag_bg $flag_fg
echo -n -s $__bobthefish_hg_glyph ' '
__bobthefish_start_segment $flag_bg $flag_fg
set_color $flag_fg --bold
__bobthefish_start_segment $flag_bg $flag_fg --bold
echo -n -s (__bobthefish_hg_branch) $flags ' '
set_color normal
set -l project_pwd (__bobthefish_project_pwd (__bobthefish_hg_project_dir))
if test "$project_pwd"
if test -w "$PWD"
set -l project_pwd (__bobthefish_project_pwd $argv[1])
if [ "$project_pwd" ]
if [ -w "$PWD" ]
__bobthefish_start_segment 333 999
else
__bobthefish_start_segment $__bobthefish_med_red $__bobthefish_lt_red
@ -263,41 +280,37 @@ function __bobthefish_prompt_hg -d 'Display the actual hg state'
end
end
# TODO: clean up the fugly $ahead business
function __bobthefish_prompt_git -d 'Display the actual git state'
set -l dirty (command git diff --no-ext-diff --quiet --exit-code; or echo -n '*')
set -l staged (command git diff --cached --no-ext-diff --quiet --exit-code; or echo -n '~')
set -l stashed (command git rev-parse --verify refs/stash > /dev/null 2>&1; and echo -n '$')
set -l ahead (command git branch -v 2> /dev/null | grep -Eo '^\* [^ ]* *[^ ]* *\[[^]]*\]' | grep -Eo '\[[^]]*\]$' | awk 'ORS="";/ahead/ {print "+"} /behind/ {print "-"}' | sed -e 's/+-/±/')
set -l stashed (command git rev-parse --verify --quiet refs/stash >/dev/null; and echo -n '$')
set -l ahead (command git rev-list --left-right '@{upstream}...HEAD' ^/dev/null | awk '/>/ {a += 1} /</ {b += 1} {if (a > 0) nextfile} END {if (a > 0 && b > 0) print "±"; else if (a > 0) print "+"; else if (b > 0) print "-"}')
set -l new (command git ls-files --other --exclude-standard);
test "$new"; and set new '…'
[ "$new" ]; and set new '…'
set -l flags "$dirty$staged$stashed$ahead$new"
test "$flags"; and set flags " $flags"
[ "$flags" ]; and set flags " $flags"
set -l flag_bg $__bobthefish_lt_green
set -l flag_fg $__bobthefish_dk_green
if test "$dirty" -o "$staged"
if [ "$dirty" -o "$staged" ]
set flag_bg $__bobthefish_med_red
set flag_fg fff
else
if test "$stashed"
else if [ "$stashed" ]
set flag_bg $__bobthefish_lt_orange
set flag_fg $__bobthefish_dk_orange
end
end
__bobthefish_path_segment (__bobthefish_git_project_dir)
__bobthefish_path_segment $argv[1]
__bobthefish_start_segment $flag_bg $flag_fg
set_color $flag_fg --bold
__bobthefish_start_segment $flag_bg $flag_fg --bold
echo -n -s (__bobthefish_git_branch) $flags ' '
set_color normal
set -l project_pwd (__bobthefish_project_pwd (__bobthefish_git_project_dir))
if test "$project_pwd"
if test -w "$PWD"
set -l project_pwd (__bobthefish_project_pwd $argv[1])
if [ "$project_pwd" ]
if [ -w "$PWD" ]
__bobthefish_start_segment 333 999
else
__bobthefish_start_segment $__bobthefish_med_red $__bobthefish_lt_red
@ -311,12 +324,9 @@ function __bobthefish_prompt_dir -d 'Display a shortened form of the current dir
__bobthefish_path_segment "$PWD"
end
function __bobthefish_in_virtualfish_virtualenv
set -q VIRTUAL_ENV
end
function __bobthefish_virtualenv_python_version -d 'Get current python version'
switch (readlink (which python))
set -l python_version (readlink (which python))
switch "$python_version"
case python2
echo $__bobthefish_superscript_glyph[2]
case python3
@ -326,16 +336,32 @@ function __bobthefish_virtualenv_python_version -d 'Get current python version'
end
end
function __bobthefish_virtualenv -d 'Get the current virtualenv'
echo $__bobthefish_virtualenv_glyph(__bobthefish_virtualenv_python_version) (basename "$VIRTUAL_ENV")
function __bobthefish_prompt_virtualfish -d "Display activated virtual environment (only for virtualfish, virtualenv's activate.fish changes prompt by itself)"
[ "$theme_display_virtualenv" = 'no' -o -z "$VIRTUAL_ENV" ]; and return
set -l version_glyph (__bobthefish_virtualenv_python_version)
if [ "$version_glyph" ]
__bobthefish_start_segment $__bobthefish_med_blue $__bobthefish_lt_grey
echo -n -s $__bobthefish_virtualenv_glyph $version_glyph
end
__bobthefish_start_segment $__bobthefish_med_blue $__bobthefish_lt_grey --bold
echo -n -s (basename "$VIRTUAL_ENV") ' '
set_color normal
end
function __bobthefish_prompt_virtualfish -d "Display activated virtual environment (only for virtualfish, virtualenv's activate.fish changes prompt by itself)"
set flag_bg $__bobthefish_lt_blue
set flag_fg $__bobthefish_dk_blue
__bobthefish_start_segment $flag_bg $flag_fg
set_color $flag_fg --bold
echo -n -s (__bobthefish_virtualenv) $flags ' '
function __bobthefish_prompt_rubies -d 'Display current Ruby (rvm/rbenv)'
[ "$theme_display_ruby" = 'no' ]; and return
set -l ruby_version
if type rvm-prompt >/dev/null
set ruby_version (rvm-prompt i v g)
else if type rbenv >/dev/null
set ruby_version (rbenv version-name)
# Don't show global ruby version...
[ "$ruby_version" = (rbenv global) ]; and return
end
[ -w "$ruby_version" ]; and return
__bobthefish_start_segment $__bobthefish_ruby_red $__bobthefish_lt_grey --bold
echo -n -s $ruby_version ' '
set_color normal
end
@ -347,13 +373,15 @@ end
function fish_prompt -d 'bobthefish, a fish theme optimized for awesome'
__bobthefish_prompt_status
__bobthefish_prompt_user
if __bobthefish_in_virtualfish_virtualenv
__bobthefish_prompt_rubies
__bobthefish_prompt_virtualfish
end
if __bobthefish_in_git # TODO: do this right.
__bobthefish_prompt_git # if something is in both git and hg, check the length of
else if __bobthefish_in_hg # __bobthefish_git_project_dir vs __bobthefish_hg_project_dir
__bobthefish_prompt_hg # and pick the longer of the two.
set -l git_root (__bobthefish_git_project_dir)
set -l hg_root (__bobthefish_hg_project_dir)
if [ (echo "$hg_root" | wc -c) -gt (echo "$git_root" | wc -c) ]
__bobthefish_prompt_hg $hg_root
else if [ "$git_root" ]
__bobthefish_prompt_git $git_root
else
__bobthefish_prompt_dir
end