fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Nahorcolor: prefer `set_color --reset` over `set_color normal`867946468

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