summaryrefslogtreecommitdiff
path: root/functions/fish_right_prompt.fish
blob: ea5681de4adf5c2da01ff7a907a5d67974ac90fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#
# Segments functions
#
set -g current_bg NONE
set -g segment_separator_right \uE0B2



function prompt_segment_right -d "Function to draw a segment"

  set -l bg
  set -l fg
  if [ -n "$argv[1]" ]
    set bg $argv[1]
  else
    set bg normal
  end

  if [ -n "$argv[2]" ]
    set fg $argv[2]
  else
    set fg normal
  end

  if [ -z "$argv[3]" ]
    return
  end

  set_color $bg
  echo -n " $segment_separator_right"

  set_color -b $bg
  set_color $fg


  echo -n " $argv[3]"
  set -g current_bg $bg
end

function git_status_summary_right -d "Deal with git status --porcelain"
  awk '
  BEGIN {
    untracked=0;
    unstaged=0;
    staged=0;
    ahead=0;
    behind=0;
  }
  {
    if (!after_first && $0 ~ /^##.+/) {
      if ( match($0, /ahead ([1-9]+)/) ) {
        split(substr($0, RSTART, RLENGTH), A);
        ahead=A[2];
      }
      if ( match($0, /behind ([1-9]+)/) ) {
        split(substr($0, RSTART, RLENGTH), A);
        behind=A[2];
      }
    } else if ($0 ~ /^\?\? .+/) {
      untracked += 1
    } else {
      if ($0 ~ /^.[^ ] .+/) {
        unstaged += 1
      }
      if ($0 ~ /^[^ ]. .+/) {
        staged += 1
      }
    }
    after_first = 1
  }
  END {
    if (!seen_header) {

    }
    print ahead ";" behind ";" untracked ";" unstaged ";" staged
  }'
end


function prompt_git_right -d "Display the current git state"
  set -l ref
  if [ -e .git ]
    set dirty ''
    set gstatus ( git status --porcelain -b | git_status_summary_right )

    # Check for commit ahead
    set ahead  (echo $gstatus | cut -d ';' -f 1)
    # Check for commit behind
    set behind (echo $gstatus | cut -d ';' -f 2)
    # Check for modified files
    set unstdg (echo $gstatus | cut -d ';' -f 3)
    # Check for untracked files
    set untrkd (echo $gstatus | cut -d ';' -f 4)
    # Check for staged files
    set staged (echo $gstatus | cut -d ';' -f 5)

    if [ $ahead -gt 0 ] 
      set dirty $dirty' ↑:'$ahead
    end
    if [ $behind -gt 0 ] 
      set dirty $dirty' ↓:'$behind
    end
    if [ $untrkd -gt 0 ] 
      set dirty $dirty' U:'$untrkd
    end
    if [ $unstdg -gt 0 ] 
      set dirty $dirty' ?:'$unstdg
    end
    if [ $staged -gt 0 ] 
      set dirty $dirty' S:'$staged
    end


    if [ $staged -eq 0 -a $untrkd -eq 0 ]
      set BG green
      set PROMPT "$dirty"
    else
      set BG yellow

      # Check if there's any commit in the repo
      set -l empty 0
      git rev-parse --quiet --verify HEAD > /dev/null 2>&1; or set empty 1

      set -l target
      if [ $empty = 1 ]
        # The repo is empty
        set dirty ''
        set target '4b825dc642cb6eb9a060e54bf8d69288fbee4904'

      else
        # The repo is not emtpy
        set target 'HEAD'
      end

      # Check for dirty
      if [ "$dirty" != "" ]
        set PROMPT "$dirty"
      end
    end
    prompt_segment_right $BG black $PROMPT
  end
end



function prompt_shlv_right -d "Display level shell"
  set pline ''
  if [ "$VIMRUNTIME" != "" ]
    set pline 'vim'
    if [ "$SHLVL" -gt 3 ]
      set pline 'vim:['$SHLVL']'
    end
  else
    if [ "$SHLVL" -gt 2 ]
      set pline '['$SHLVL']'
    end
  end
  if [ $pline != '' ]
    prompt_segment_right 8C8C8C black "$pline"
  end
end

function prompt_todo_right -d "Display todo"
  if which todo 2> /dev/null > /dev/null
      set all (todo | wc -l)
      set chkd (todo checked | wc -l)
      set td (math "$all-$chkd")

      if [ $all -gt 0 ]
        prompt_segment_right 5C5C5C blue "$td/$all"
      end
    end
end

function prompt_status_right -d "the symbols for a non zero exit status, root and background jobs"

    if [ $RETVAL -ne 0 ]
      prompt_segment_right black red "✘"
    end

    # if superuser (uid == 0)
    set -l uid (id -u $USER)
    if [ $uid -eq 0 ]
      prompt_segment_right black yellow "⚡"
    end

    # Jobs display
    if [ (jobs -l | wc -l) -gt 0 ]
      prompt_segment_right black cyan "⚙"
    end
end
function prompt_finish_right -d "Close open segments"
  if [ -n $current_bg ]
    set_color -b normal
  end
  set -g current_bg NONE
end


function available -a name -d "Check if a function or program is available."
  type "$name" 2>/dev/null >&2
end


#
# Right prompt
#
function fish_right_prompt
  set -g RETVAL $status
#  prompt_status_right
#  available git; and prompt_git_right
#  prompt_todo_right
#  prompt_shlv_right
#  prompt_finish_right
end