dotfiles

Older, unmaintained dotfiles by mrgrouse
Log | Files | Refs | README

fish_prompt.fish (12489B)


      1 # name: Agnoster
      2 # agnoster's Theme - https://gist.github.com/3712874
      3 # A Powerline-inspired theme for FISH
      4 #
      5 # # README
      6 #
      7 # In order for this theme to render correctly, you will need a
      8 # [Powerline-patched font](https://gist.github.com/1595572).
      9 
     10 ## Set this options in your config.fish (if you want to :])
     11 # set -g theme_hide_hostname yes
     12 set -g theme_display_user yes
     13 set -g theme_hide_hostname no
     14 # set -g default_user your_normal_user
     15 # set -g theme_svn_prompt_enabled yes
     16 # set -g theme_mercurial_prompt_enabled yes
     17 
     18 
     19 
     20 set -g current_bg NONE
     21 set -g segment_separator \uE0B0
     22 set -g right_segment_separator \uE0B0
     23 set -q scm_prompt_blacklist; or set -g scm_prompt_blacklist
     24 set -q max_package_count_visible_in_prompt; or set -g max_package_count_visible_in_prompt 10
     25 # We support trimming the version only in simple cases, such as "1.2.3".
     26 set -q try_to_trim_nix_package_version; or set -g try_to_trim_nix_package_version yes
     27 
     28 # ===========================
     29 # Color setting
     30 
     31 # You can set these variables in config.fish like:
     32 # set -g color_dir_bg red
     33 # If not set, default color from agnoster will be used.
     34 # ===========================
     35 
     36 set -q color_virtual_env_bg; or set -g color_virtual_env_bg white
     37 set -q color_virtual_env_str; or set -g color_virtual_env_str black
     38 
     39 set -q color_user_bg; or set -g color_user_bg 1d1d1d
     40 set -q color_user_str; or set -g color_user_str white
     41 
     42 set -q color_dir_bg; or set -g color_dir_bg C5C8C6
     43 set -q color_dir_str; or set -g color_dir_str black
     44 
     45 set -q color_hg_changed_bg; or set -g color_hg_changed_bg yellow
     46 set -q color_hg_changed_str; or set -g color_hg_changed_str black
     47 set -q color_hg_bg; or set -g color_hg_bg green
     48 set -q color_hg_str; or set -g color_hg_str black
     49 set -q color_git_dirty_bg; or set -g color_git_dirty_bg yellow
     50 set -q color_git_dirty_str; or set -g color_git_dirty_str black
     51 set -q color_git_bg; or set -g color_git_bg green
     52 set -q color_git_str; or set -g color_git_str black
     53 set -q color_svn_bg; or set -g color_svn_bg green
     54 set -q color_svn_str; or set -g color_svn_str black
     55 set -q color_status_nonzero_bg; or set -g color_status_nonzero_bg black
     56 set -q color_status_nonzero_str; or set -g color_status_nonzero_str red
     57 set -q glyph_status_nonzero; or set -g glyph_status_nonzero "✘"
     58 set -q color_status_superuser_bg; or set -g color_status_superuser_bg black
     59 set -q color_status_superuser_str; or set -g color_status_superuser_str yellow
     60 set -q glyph_status_superuser; or set -g glyph_status_superuser "🔒"
     61 set -q color_status_jobs_bg; or set -g color_status_jobs_bg black
     62 set -q color_status_jobs_str; or set -g color_status_jobs_str cyan
     63 set -q glyph_status_jobs; or set -g glyph_status_jobs "⚡"
     64 set -q color_status_private_bg; or set -g color_status_private_bg black
     65 set -q color_status_private_str; or set -g color_status_private_str purple
     66 set -q glyph_status_private; or set -g glyph_status_private "⚙"
     67 
     68 # ===========================
     69 # General VCS settings
     70 
     71 set -q fish_vcs_branch_name_length; or set -g fish_vcs_branch_name_length 15
     72 
     73 # ===========================
     74 # Git settings
     75 # set -g color_dir_bg red
     76 
     77 set -q fish_git_prompt_untracked_files; or set -g fish_git_prompt_untracked_files normal
     78 
     79 # ===========================
     80 # Subversion settings
     81 
     82 set -q theme_svn_prompt_enabled; or set -g theme_svn_prompt_enabled no
     83 
     84 # ===========================
     85 # Mercurial settings
     86 
     87 set -q theme_mercurial_prompt_enabled; or set -g theme_mercurial_prompt_enabled no
     88 
     89 # ===========================
     90 # Helper methods
     91 # ===========================
     92 
     93 set -g __fish_git_prompt_showdirtystate 'yes'
     94 set -g __fish_git_prompt_char_dirtystate '±'
     95 set -g __fish_git_prompt_char_cleanstate ''
     96 
     97 function shorten_branch_name -a branch_name
     98   set new_branch_name $branch_name
     99 
    100   if test (string length $branch_name) -gt $fish_vcs_branch_name_length
    101     # Round up length before dot (+0.5)
    102     # Remove half the length of dots (-1)
    103     # -> Total offset: -0.5
    104     set pre_dots_length (math -s0 $fish_vcs_branch_name_length / 2 - 0.5)
    105     # Round down length after dot (-0.5)
    106     # Remove half the length of dots (-1)
    107     # -> Total offset: -1.5
    108     set post_dots_length (math -s0 $fish_vcs_branch_name_length / 2 - 1.5)
    109     set new_branch_name (string replace -r "(.{$pre_dots_length}).*(.{$post_dots_length})" '$1..$2' $branch_name)
    110   end
    111 
    112   echo $new_branch_name
    113 end
    114 
    115 function parse_git_dirty
    116   if [ $__fish_git_prompt_showdirtystate = "yes" ]
    117     set -l submodule_syntax
    118     set submodule_syntax "--ignore-submodules=dirty"
    119     set untracked_syntax "--untracked-files=$fish_git_prompt_untracked_files"
    120     set git_dirty (command git status --porcelain $submodule_syntax $untracked_syntax 2> /dev/null)
    121     if [ -n "$git_dirty" ]
    122         echo -n "$__fish_git_prompt_char_dirtystate"
    123     else
    124         echo -n "$__fish_git_prompt_char_cleanstate"
    125     end
    126   end
    127 end
    128 
    129 function cwd_in_scm_blacklist
    130   for entry in $scm_prompt_blacklist
    131     pwd | grep "^$entry" -
    132   end
    133 end
    134 
    135 # ===========================
    136 # Segments functions
    137 # ===========================
    138 
    139 function prompt_segment -d "Function to draw a segment"
    140   set -l bg
    141   set -l fg
    142   if [ -n "$argv[1]" ]
    143     set bg $argv[1]
    144   else
    145     set bg normal
    146   end
    147   if [ -n "$argv[2]" ]
    148     set fg $argv[2]
    149   else
    150     set fg normal
    151   end
    152   if [ "$current_bg" != 'NONE' -a "$argv[1]" != "$current_bg" ]
    153     set_color -b $bg
    154     set_color $current_bg
    155     echo -n "$segment_separator "
    156     set_color -b $bg
    157     set_color $fg
    158   else
    159     set_color -b $bg
    160     set_color $fg
    161     echo -n " "
    162   end
    163   set current_bg $argv[1]
    164   if [ -n "$argv[3]" ]
    165     echo -n -s $argv[3] " "
    166   end
    167 end
    168 
    169 function prompt_finish -d "Close open segments"
    170   if [ -n $current_bg ]
    171     set_color normal
    172     set_color $current_bg
    173     echo -n "$segment_separator "
    174     set_color normal
    175   end
    176   set -g current_bg NONE
    177 end
    178 
    179 
    180 # ===========================
    181 # Theme components
    182 # ===========================
    183 
    184 function prompt_virtual_env -d "Display Python or Nix virtual environment"
    185   set envs
    186 
    187   if test "$CONDA_DEFAULT_ENV"
    188     set envs $envs "conda[$CONDA_DEFAULT_ENV]"
    189   end
    190 
    191   if test "$VIRTUAL_ENV"
    192     set py_env (basename $VIRTUAL_ENV)
    193     set envs $envs "py[$py_env]"
    194   end
    195 
    196   # Support for `nix shell` command in nix 2.4+. Only the packages passed on the command line are
    197   # available in PATH, so it is useful to print them all.
    198   set nix_packages
    199   for p in $PATH
    200     set package_name_version (string match --regex '/nix/store/\w+-([^/]+)/.*' $p)[2]
    201     if test "$package_name_version"
    202       set package_name (string match --regex '^(.*)-(\d+(\.\d)+|unstable-20\d{2}-\d{2}-\d{2})' $package_name_version)[2]
    203       if test "$try_to_trim_nix_package_version" = "yes" -a -n "$package_name"
    204         set package $package_name
    205       else
    206         set package $package_name_version
    207       end
    208       if not contains $package $nix_packages
    209         set nix_packages $nix_packages $package
    210       end
    211     end
    212   end
    213   if test (count $nix_packages) -gt $max_package_count_visible_in_prompt
    214     set nix_packages $nix_packages[1..$max_package_count_visible_in_prompt] "..."
    215   end
    216 
    217   if [ "$IN_NIX_SHELL" = "impure" ]
    218     # Support for
    219     #   1) `nix-shell` command 
    220     #   2) `nix develop` command in nix 2.4+.
    221     # These commands are typically dumping too many packages into PATH for it be useful to print
    222     # them. Thus we only print "nix[impure]".
    223     set envs $envs "nix[impure]"
    224   else if test "$nix_packages"
    225     # Support for `nix-shell -p`. Would print "nix[foo bar baz]".
    226     # We check for this case after checking for "impure" because impure brings too many packages 
    227     # into PATH.
    228     set envs $envs "nix[$nix_packages]"
    229   else if test "$IN_NIX_SHELL"
    230     # Support for `nix-shell --pure`. Would print "nix[pure]".
    231     # We check for this case after checking for individual packages because it otherwise might 
    232     # confuse the user into believing when they are in a pure shell, after they have invoked 
    233     # `nix shell` from within it.
    234     set envs $envs "nix[$IN_NIX_SHELL]"
    235   end
    236 
    237   if test "$envs"
    238     prompt_segment $color_virtual_env_bg $color_virtual_env_str (string join " " $envs)
    239   end
    240 end
    241 
    242 function prompt_user -d "Display current user if different from $default_user"
    243   if [ "$theme_display_user" = "yes" ]
    244     if [ "$USER" != "$default_user" -o -n "$SSH_CLIENT" ]
    245       set USER (whoami)
    246       get_hostname
    247       if [ $HOSTNAME_PROMPT ]
    248         set USER_PROMPT $USER@$HOSTNAME_PROMPT
    249       else
    250         set USER_PROMPT $USER
    251       end
    252       prompt_segment $color_user_bg $color_user_str $USER_PROMPT
    253     end
    254   else
    255     get_hostname
    256     if [ $HOSTNAME_PROMPT ]
    257       prompt_segment $color_user_bg $color_user_str $HOSTNAME_PROMPT
    258     end
    259   end
    260 end
    261 
    262 function get_hostname -d "Set current hostname to prompt variable $HOSTNAME_PROMPT if connected via SSH"
    263   set -g HOSTNAME_PROMPT ""
    264   if [ "$theme_hide_hostname" = "no" -o \( "$theme_hide_hostname" != "yes" -a -n "$SSH_CLIENT" \) ]
    265     set -g HOSTNAME_PROMPT (uname -n)
    266   end
    267 end
    268 
    269 function prompt_dir -d "Display the current directory"
    270   prompt_segment $color_dir_bg $color_dir_str (prompt_pwd -D 100)
    271 end
    272 
    273 
    274 function prompt_hg -d "Display mercurial state"
    275   not set -l root (fish_print_hg_root); and return
    276 
    277   set -l state
    278   set -l branch (cat $root/branch 2>/dev/null; or echo default)
    279   set -l bookmark (cat $root/bookmarks.current 2>/dev/null)
    280   set state (hg_get_state)
    281   set revision (command hg id -n)
    282   set branch_symbol \uE0A0
    283   set prompt_text "$branch_symbol $branch$bookmark:$revision"
    284   if [ "$state" = "0" ]
    285       prompt_segment $color_hg_changed_bg $color_hg_changed_str $prompt_text " ±"
    286   else
    287       prompt_segment $color_hg_bg $color_hg_str $prompt_text
    288   end
    289 end
    290 
    291 function hg_get_state -d "Get mercurial working directory state"
    292   if hg status | grep --quiet -e "^[A|M|R|!|?]"
    293     echo 0
    294   else
    295     echo 1
    296   end
    297 end
    298 
    299 
    300 function prompt_git -d "Display the current git state"
    301   set -l ref
    302   set -l dirty
    303   if command git rev-parse --is-inside-work-tree >/dev/null 2>&1
    304     set dirty (parse_git_dirty)
    305     set ref (command git symbolic-ref HEAD 2> /dev/null)
    306     if [ $status -gt 0 ]
    307       set -l branch (command git show-ref --head -s --abbrev |head -n1 2> /dev/null)
    308       set ref "➦ $branch "
    309     end
    310     set branch_symbol \uE0A0
    311     set -l long_branch (echo $ref | sed "s#refs/heads/##")
    312     set -l branch (shorten_branch_name $long_branch)
    313     if [ "$dirty" != "" ]
    314       prompt_segment $color_git_dirty_bg $color_git_dirty_str "$branch_symbol $branch $dirty"
    315     else
    316       prompt_segment $color_git_bg $color_git_str "$branch_symbol $branch $dirty"
    317     end
    318   end
    319 end
    320 
    321 
    322 function prompt_svn -d "Display the current svn state"
    323   set -l ref
    324   if command svn info >/dev/null 2>&1
    325     set long_branch (svn_get_branch)
    326     set -l branch (shorten_branch_name $long_branch)
    327     set branch_symbol \uE0A0
    328     set revision (svn_get_revision)
    329     prompt_segment $color_svn_bg $color_svn_str "$branch_symbol $branch:$revision"
    330   end
    331 end
    332 
    333 function svn_get_branch -d "get the current branch name"
    334   svn info 2> /dev/null | awk -F/ \
    335       '/^URL:/ { \
    336         for (i=0; i<=NF; i++) { \
    337           if ($i == "branches" || $i == "tags" ) { \
    338             print $(i+1); \
    339             break;\
    340           }; \
    341           if ($i == "trunk") { print $i; break; } \
    342         } \
    343       }'
    344 end
    345 
    346 function svn_get_revision -d "get the current revision number"
    347   svn info 2> /dev/null | sed -n 's/Revision:\ //p'
    348 end
    349 
    350 
    351 function prompt_status -d "the symbols for a non zero exit status, root and background jobs"
    352     if [ $RETVAL -ne 0 ]
    353       prompt_segment $color_status_nonzero_bg $color_status_nonzero_str $glyph_status_nonzero
    354     end
    355 
    356     if [ "$fish_private_mode" ]
    357       prompt_segment $color_status_private_bg $color_status_private_str $glyph_status_private
    358     end
    359 
    360     # if superuser (uid == 0)
    361     set -l uid (id -u $USER)
    362     if [ $uid -eq 0 ]
    363       prompt_segment $color_status_superuser_bg $color_status_superuser_str $glyph_status_superuser
    364     end
    365 
    366     # Jobs display
    367     if [ (jobs -l | wc -l) -gt 0 ]
    368       prompt_segment $color_status_jobs_bg $color_status_jobs_str $glyph_status_jobs
    369     end
    370 end
    371 
    372 # ===========================
    373 # Apply theme
    374 # ===========================
    375 
    376 function fish_prompt
    377   set -g RETVAL $status
    378   prompt_status
    379   prompt_user
    380   prompt_dir
    381   prompt_virtual_env
    382   if [ (cwd_in_scm_blacklist | wc -c) -eq 0 ]
    383     type -q git; and prompt_git
    384     if [ "$theme_mercurial_prompt_enabled" = "yes" ]
    385       prompt_hg
    386     end
    387     if [ "$theme_svn_prompt_enabled" = "yes" ]
    388       prompt_svn
    389     end
    390   end
    391   prompt_finish
    392 end