Skip to content

Commit c639113

Browse files
authored
Basic git improvements (#1082)
Thanks to @Euphrasiologist for the prompt! I found some bugs, and here are a few improvements. 1. Fix checking if currently in git repo (#1079) 2. Use `git branch --show-current` instead of `git rev-parse --abbrev-ref HEAD` to get current branch 3. Refactor processing current status (relying on https://git-scm.com/docs/git-status). Before, in some cases it leads to unexpected behavior. (for example, when renaming or deleting files)
1 parent d7adaf9 commit c639113

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

modules/prompt/basic-git.nu

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,61 @@
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.
43

5-
# use as below without the comments and in your
6-
# env.nu file
4+
# Authors: Max Brown, @rukins
75

6+
# Use as below without the comments and in your 'env.nu' file
87

98
# use path/to/basic-git.nu basic-git-left-prompt
10-
119
# $env.PROMPT_COMMAND = {||
1210
# let left = create_left_prompt
1311
# basic-git-left-prompt $left
1412
# }
1513

1614
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
1816
}
1917

2018
export def basic-git-left-prompt [in_left_prompt] {
2119

22-
# if we're in a repo, let's go!
2320
let currently_in_git_repo = in_git_repo
2421

2522
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)"
3324

34-
# get the status in short
35-
let git_status = $git_status
25+
let current_status = git status -s
3626
| lines
37-
| str replace -r '(.* ).*' '$1'
38-
| sort
27+
| str replace -r '^(.{2}).*' '$1'
3928
| 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) }
5031
}
5132
| 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+
# }
5351
}
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 ","
6556

57+
$"($in_left_prompt)(ansi reset) [($current_branch)($current_status | if ($in | is-not-empty) { $' ($in)' })]"
6658
} else {
67-
# otherwise just return the normal prompt
6859
$in_left_prompt
6960
}
7061
}

0 commit comments

Comments
 (0)