Skip to content

Commit

Permalink
panache-git: do not print stderr from Git commands (nushell#813)
Browse files Browse the repository at this point in the history
Nushell 0.92 changed how external commands operate in pipelines:
https://www.nushell.sh/blog/2024-04-02-nushell_0_92_0.html

After updating to Nushell 0.92, panache-git would print errors when the
current working directory was not a Git repository: "fatal: not a git
repository (or any of the parent directories): .git"

This change properly handles stderr from Git commands according to
Nushell's updated external command behavior.

Also, Nushell 0.91 introduced "is-not-empty", so places in the code
where a string was checked for being not-empty or not-contains were
simplified a bit.

Finally, the main command in the module was renamed to "main" to make it
simpler to import and use in config.
  • Loading branch information
ehdevries committed Apr 11, 2024
1 parent c30efc7 commit b1019da
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions modules/prompt/panache-git.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# Quick Start:
# - Download this file (panache-git.nu)
# - In your Nushell config:
# - Import the panache-git command from the panache-git.nu module file
# - Import the main command from the panache-git.nu module file
# - Set panache-git as your prompt command
# - Disable the separate prompt indicator by setting it to an empty string
# - For example, with this file in your home directory:
# use ~/panache-git.nu panache-git
# $env.PROMPT_COMMAND = { panache-git }
# $env.PROMPT_INDICATOR = { "" }
# use ~/panache-git.nu main
# $env.PROMPT_COMMAND = {|| panache-git }
# $env.PROMPT_INDICATOR = {|| "" }
# - Restart Nushell
#
# For more documentation or to file an issue, see https://github.com/ehdevries/panache-git


# An opinionated Git prompt for Nushell, styled after posh-git
export def prompt-with-panache [] {
export def main [] {
let prompt = ($'(current-dir) (repo-styled)' | str trim)
$'($prompt)> '
}
Expand All @@ -30,7 +30,7 @@ export def current-dir [] {
do --ignore-errors { $current_dir | path relative-to $nu.home-path } | str join
)

let in_sub_dir_of_home = ($current_dir_relative_to_home | is-empty | nope)
let in_sub_dir_of_home = ($current_dir_relative_to_home | is-not-empty)

let current_dir_abbreviated = (if $in_sub_dir_of_home {
$'~(char separator)($current_dir_relative_to_home)' | str replace -ar '\\' '/'
Expand All @@ -43,7 +43,7 @@ export def current-dir [] {

# Get repository status as structured data
export def repo-structured [] {
let in_git_repo = (do --ignore-errors { git rev-parse --abbrev-ref HEAD } | is-empty | nope)
let in_git_repo = (do { git rev-parse --abbrev-ref HEAD } | complete | get stdout | is-not-empty)

let status = (if $in_git_repo {
git --no-optional-locks status --porcelain=2 --branch | lines
Expand All @@ -55,8 +55,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with '# branch.head')
| first
| str contains '(detached)'
| nope
| str contains --not '(detached)'
} else {
false
})
Expand Down Expand Up @@ -86,8 +85,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with '# branch.upstream')
| str join
| is-empty
| nope
| is-not-empty
} else {
false
})
Expand All @@ -96,8 +94,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with '# branch.ab')
| str join
| is-empty
| nope
| is-not-empty
} else {
false
})
Expand Down Expand Up @@ -133,8 +130,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with '1') or ($it | str starts-with '2')
| str join
| is-empty
| nope
| is-not-empty
} else {
false
})
Expand All @@ -143,8 +139,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with '?')
| str join
| is-empty
| nope
| is-not-empty
} else {
false
})
Expand All @@ -153,8 +148,7 @@ export def repo-structured [] {
$status
| where ($it | str starts-with 'u')
| str join
| is-empty
| nope
| is-not-empty
} else {
false
})
Expand Down Expand Up @@ -380,10 +374,6 @@ export def repo-styled [] {

# Helper commands to encapsulate style and make everything else more readable

def nope [] {
each { |it| $it == false }
}

def bright-cyan [] {
each { |it| $"(ansi -e '96m')($it)(ansi reset)" }
}
Expand Down

0 comments on commit b1019da

Please sign in to comment.