dotfiles

my shiny new dotfiles
git clone git://git.jakekoroman.com/dotfiles
Log | Files | Refs | README

fish_right_prompt.fish (5362B)


      1 function fish_right_prompt
      2     set -l cmd_status $status
      3     if test $cmd_status -ne 0
      4         echo -n (set_color red)"✘ $cmd_status"
      5     end
      6 
      7     if not command -sq git
      8         set_color normal
      9         return
     10     end
     11 
     12     # Get the git directory for later use.
     13     # Return if not inside a Git repository work tree.
     14     if not set -l git_dir (command git rev-parse --git-dir 2>/dev/null)
     15         set_color normal
     16         return
     17     end
     18 
     19     # Get the current action ("merge", "rebase", etc.)
     20     # and if there's one get the current commit hash too.
     21     set -l commit ''
     22     if set -l action (fish_print_git_action "$git_dir")
     23         set commit (command git rev-parse HEAD 2> /dev/null | string sub -l 7)
     24     end
     25 
     26     # Get either the branch name or a branch descriptor.
     27     set -l branch_detached 0
     28     if not set -l branch (command git symbolic-ref --short HEAD 2>/dev/null)
     29         set branch_detached 1
     30         set branch (command git describe --contains --all HEAD 2>/dev/null)
     31     end
     32 
     33     # Get the commit difference counts between local and remote.
     34     command git rev-list --count --left-right 'HEAD...@{upstream}' 2>/dev/null \
     35         | read -d \t -l status_ahead status_behind
     36     if test $status -ne 0
     37         set status_ahead 0
     38         set status_behind 0
     39     end
     40 
     41     # Get the stash status.
     42     # (git stash list) is very slow. => Avoid using it.
     43     set -l status_stashed 0
     44     if test -f "$git_dir/refs/stash"
     45         set status_stashed 1
     46     else if test -r "$git_dir/commondir"
     47         read -l commondir <"$git_dir/commondir"
     48         if test -f "$commondir/refs/stash"
     49             set status_stashed 1
     50         end
     51     end
     52 
     53     # git-status' porcelain v1 format starts with 2 letters on each line:
     54     #   The first letter (X) denotes the index state.
     55     #   The second letter (Y) denotes the working directory state.
     56     #
     57     # The following table presents the possible combinations:
     58     # * The underscore character denotes whitespace.
     59     # * The cell values stand for the following file states:
     60     #   a: added
     61     #   d: deleted
     62     #   m: modified
     63     #   r: renamed
     64     #   u: unmerged
     65     #   t: untracked
     66     # * Cells with more than one letter signify that both states
     67     #   are simultaneously the case. This is possible since the git index
     68     #   and working directory operate independently of each other.
     69     # * Cells which are empty are unhandled by this code.
     70     # * T (= type change) is undocumented.
     71     #   See Git v1.7.8.2 release notes for more information.
     72     #
     73     #   \ Y→
     74     #  X \
     75     #  ↓  | A  | C  | D  | M  | R  | T  | U  | X  | B  | ?  | _
     76     # ----+----+----+----+----+----+----+----+----+----+----+----
     77     #  A  | u  |    | ad | am | r  | am | u  |    |    |    | a
     78     #  C  |    |    | ad | am | r  | am | u  |    |    |    | a
     79     #  D  |    |    | u  | am | r  | am | u  |    |    |    | a
     80     #  M  |    |    | ad | am | r  | am | u  |    |    |    | a
     81     #  R  | r  | r  | rd | rm | r  | rm | ur | r  | r  | r  | r
     82     #  T  |    |    | ad | am | r  | am | u  |    |    |    | a
     83     #  U  | u  | u  | u  | um | ur | um | u  | u  | u  | u  | u
     84     #  X  |    |    |    | m  | r  | m  | u  |    |    |    |
     85     #  B  |    |    |    | m  | r  | m  | u  |    |    |    |
     86     #  ?  |    |    |    | m  | r  | m  | u  |    |    | t  |
     87     #  _  |    |    | d  | m  | r  | m  | u  |    |    |    |
     88     set -l porcelain_status (command git status --porcelain 2>/dev/null | string sub -l2)
     89 
     90     set -l status_added 0
     91     if string match -qr '[ACDMT][ MT]|[ACMT]D' $porcelain_status
     92         set status_added 1
     93     end
     94     set -l status_deleted 0
     95     if string match -qr '[ ACMRT]D' $porcelain_status
     96         set status_deleted 1
     97     end
     98     set -l status_modified 0
     99     if string match -qr '[MT]$' $porcelain_status
    100         set status_modified 1
    101     end
    102     set -l status_renamed 0
    103     if string match -qe R $porcelain_status
    104         set status_renamed 1
    105     end
    106     set -l status_unmerged 0
    107     if string match -qr 'AA|DD|U' $porcelain_status
    108         set status_unmerged 1
    109     end
    110     set -l status_untracked 0
    111     if string match -qe '\?\?' $porcelain_status
    112         set status_untracked 1
    113     end
    114 
    115     set_color -o
    116 
    117     if test -n "$branch"
    118         if test $branch_detached -ne 0
    119             set_color brmagenta
    120         else
    121             set_color green
    122         end
    123         echo -n " $branch"
    124     end
    125     if test -n "$commit"
    126         echo -n ' '(set_color yellow)"$commit"
    127     end
    128     if test -n "$action"
    129         set_color normal
    130         echo -n (set_color white)':'(set_color -o brred)"$action"
    131     end
    132     if test $status_ahead -ne 0
    133         echo -n ' '(set_color brmagenta)'⬆'
    134     end
    135     if test $status_behind -ne 0
    136         echo -n ' '(set_color brmagenta)'⬇'
    137     end
    138     if test $status_stashed -ne 0
    139         echo -n ' '(set_color cyan)'✭'
    140     end
    141     if test $status_added -ne 0
    142         echo -n ' '(set_color green)'✚'
    143     end
    144     if test $status_deleted -ne 0
    145         echo -n ' '(set_color red)'✖'
    146     end
    147     if test $status_modified -ne 0
    148         echo -n ' '(set_color blue)'✱'
    149     end
    150     if test $status_renamed -ne 0
    151         echo -n ' '(set_color magenta)'➜'
    152     end
    153     if test $status_unmerged -ne 0
    154         echo -n ' '(set_color yellow)'═'
    155     end
    156     if test $status_untracked -ne 0
    157         echo -n ' '(set_color white)'◼'
    158     end
    159 
    160     set_color normal
    161 end