|
1 | | -# Max Brown |
2 | | -# a very basic git prompt |
3 | | -# sort of like panache-git, but fewer than 60 lines of code. |
| 1 | +# It's a very basic git prompt, |
| 2 | +# sort of like panache-git, but less than 50 lines of code. |
4 | 3 |
|
5 | | -# use as below without the comments and in your |
6 | | -# env.nu file |
| 4 | +# Authors: Max Brown, @rukins |
7 | 5 |
|
| 6 | +# Use as below without the comments and in your 'env.nu' file |
8 | 7 |
|
9 | 8 | # use path/to/basic-git.nu basic-git-left-prompt |
10 | | - |
11 | 9 | # $env.PROMPT_COMMAND = {|| |
12 | 10 | # let left = create_left_prompt |
13 | 11 | # basic-git-left-prompt $left |
14 | 12 | # } |
15 | 13 |
|
16 | 14 | def in_git_repo [] { |
17 | | - (do --ignore-errors { git rev-parse --abbrev-ref HEAD } | is-empty) == false |
| 15 | + (git branch --show-current | complete | get stderr | is-empty) == true |
18 | 16 | } |
19 | 17 |
|
20 | 18 | export def basic-git-left-prompt [in_left_prompt] { |
21 | 19 |
|
22 | | - # if we're in a repo, let's go! |
23 | 20 | let currently_in_git_repo = in_git_repo |
24 | 21 |
|
25 | 22 | if $currently_in_git_repo { |
26 | | - # get the branch info first |
27 | | - let branch_info = git branch -l |
28 | | - | lines |
29 | | - | filter {|e| $e | str contains "*" } |
30 | | - | each {|e| $e | str replace "* " "="} |
31 | | - | get 0 |
32 | | - let git_status = git status -s |
| 23 | + let current_branch = $"(git branch --show-current)" |
33 | 24 |
|
34 | | - # get the status in short |
35 | | - let git_status = $git_status |
| 25 | + let current_status = git status -s |
36 | 26 | | lines |
37 | | - | str replace -r '(.* ).*' '$1' |
38 | | - | sort |
| 27 | + | str replace -r '^(.{2}).*' '$1' |
39 | 28 | | uniq -c |
40 | | - | insert type { |
41 | | - |e| if ($e.value | str contains "M") { |
42 | | - "blue_bold" |
43 | | - } else if ($e.value | str contains "??") { |
44 | | - "yellow_bold" |
45 | | - } else if ($e.value | str contains "D") { |
46 | | - "red_bold" |
47 | | - } else if ($e.value | str contains "A") { |
48 | | - "cyan_bold" |
49 | | - } else "" |
| 29 | + | each { |
| 30 | + |el| insert X { value: ($el.value | str substring ..0 | str trim) } | insert Y { value: ($el.value | str substring 1.. | str trim) } |
50 | 31 | } |
51 | 32 | | each { |
52 | | - |e| $"(ansi $e.type)($e.count)($e.value | str trim)(ansi reset)" |
| 33 | + |el| if ([?, !] | any { |i| $el.X.value == $i and $el.Y.value == $i }) { |
| 34 | + insert X.color "yellow_bold" | insert Y.color "yellow_bold" |
| 35 | + } else if ($el.X.value == "M") { |
| 36 | + insert X.color "blue_bold" | insert Y.color "purple_bold" |
| 37 | + } else if ($el.X.value == "D") { |
| 38 | + insert X.color "red_bold" | insert Y.color "purple_bold" |
| 39 | + } else if ($el.X.value == "A") { |
| 40 | + insert X.color "cyan_bold" | insert Y.color "purple_bold" |
| 41 | + } else { |
| 42 | + insert X.color "green_bold" | insert Y.color "purple_bold" |
| 43 | + } |
| 44 | + |
| 45 | + ### or use colors similar to original `git status -s` output |
| 46 | + # |el| if ([?, !] | any { |i| $el.X.value == $i and $el.Y.value == $i }) { |
| 47 | + # insert X.color "red_bold" | insert Y.color "red_bold" |
| 48 | + # } else { |
| 49 | + # insert X.color "green_bold" | insert Y.color "red_bold" |
| 50 | + # } |
53 | 51 | } |
54 | | - | reduce --fold '' {|str all| $"($all),($str)" } |
55 | | - | str substring 1.. |
56 | | - |
57 | | - let final_git_status = if $git_status == "" { |
58 | | - "" |
59 | | - } else { |
60 | | - $" ($git_status)" |
61 | | - } |
62 | | - |
63 | | - # construct the prompt |
64 | | - $"($in_left_prompt)(ansi reset) [($branch_info)($final_git_status)]" |
| 52 | + | each { |
| 53 | + |el| $"(ansi cyan_bold)($el.count)(ansi $el.X.color)($el.X.value)(ansi $el.Y.color)($el.Y.value)(ansi reset)" |
| 54 | + } |
| 55 | + | str join "," |
65 | 56 |
|
| 57 | + $"($in_left_prompt)(ansi reset) [($current_branch)($current_status | if ($in | is-not-empty) { $' ($in)' })]" |
66 | 58 | } else { |
67 | | - # otherwise just return the normal prompt |
68 | 59 | $in_left_prompt |
69 | 60 | } |
70 | 61 | } |
0 commit comments