mirror of
https://github.com/nushell/nu_scripts.git
synced 2024-09-11 13:45:56 +03:00
panache-git: do not print stderr from Git commands (#813)
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.
This commit is contained in:
parent
c30efc727b
commit
b1019dab18
@ -4,20 +4,20 @@
|
|||||||
# Quick Start:
|
# Quick Start:
|
||||||
# - Download this file (panache-git.nu)
|
# - Download this file (panache-git.nu)
|
||||||
# - In your Nushell config:
|
# - 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
|
# - Set panache-git as your prompt command
|
||||||
# - Disable the separate prompt indicator by setting it to an empty string
|
# - Disable the separate prompt indicator by setting it to an empty string
|
||||||
# - For example, with this file in your home directory:
|
# - For example, with this file in your home directory:
|
||||||
# use ~/panache-git.nu panache-git
|
# use ~/panache-git.nu main
|
||||||
# $env.PROMPT_COMMAND = { panache-git }
|
# $env.PROMPT_COMMAND = {|| panache-git }
|
||||||
# $env.PROMPT_INDICATOR = { "" }
|
# $env.PROMPT_INDICATOR = {|| "" }
|
||||||
# - Restart Nushell
|
# - Restart Nushell
|
||||||
#
|
#
|
||||||
# For more documentation or to file an issue, see https://github.com/ehdevries/panache-git
|
# 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
|
# 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)
|
let prompt = ($'(current-dir) (repo-styled)' | str trim)
|
||||||
$'($prompt)> '
|
$'($prompt)> '
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ export def current-dir [] {
|
|||||||
do --ignore-errors { $current_dir | path relative-to $nu.home-path } | str join
|
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 {
|
let current_dir_abbreviated = (if $in_sub_dir_of_home {
|
||||||
$'~(char separator)($current_dir_relative_to_home)' | str replace -ar '\\' '/'
|
$'~(char separator)($current_dir_relative_to_home)' | str replace -ar '\\' '/'
|
||||||
@ -43,7 +43,7 @@ export def current-dir [] {
|
|||||||
|
|
||||||
# Get repository status as structured data
|
# Get repository status as structured data
|
||||||
export def repo-structured [] {
|
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 {
|
let status = (if $in_git_repo {
|
||||||
git --no-optional-locks status --porcelain=2 --branch | lines
|
git --no-optional-locks status --porcelain=2 --branch | lines
|
||||||
@ -55,8 +55,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with '# branch.head')
|
| where ($it | str starts-with '# branch.head')
|
||||||
| first
|
| first
|
||||||
| str contains '(detached)'
|
| str contains --not '(detached)'
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -86,8 +85,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with '# branch.upstream')
|
| where ($it | str starts-with '# branch.upstream')
|
||||||
| str join
|
| str join
|
||||||
| is-empty
|
| is-not-empty
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -96,8 +94,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with '# branch.ab')
|
| where ($it | str starts-with '# branch.ab')
|
||||||
| str join
|
| str join
|
||||||
| is-empty
|
| is-not-empty
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -133,8 +130,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with '1') or ($it | str starts-with '2')
|
| where ($it | str starts-with '1') or ($it | str starts-with '2')
|
||||||
| str join
|
| str join
|
||||||
| is-empty
|
| is-not-empty
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -143,8 +139,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with '?')
|
| where ($it | str starts-with '?')
|
||||||
| str join
|
| str join
|
||||||
| is-empty
|
| is-not-empty
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -153,8 +148,7 @@ export def repo-structured [] {
|
|||||||
$status
|
$status
|
||||||
| where ($it | str starts-with 'u')
|
| where ($it | str starts-with 'u')
|
||||||
| str join
|
| str join
|
||||||
| is-empty
|
| is-not-empty
|
||||||
| nope
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@ -380,10 +374,6 @@ export def repo-styled [] {
|
|||||||
|
|
||||||
# Helper commands to encapsulate style and make everything else more readable
|
# Helper commands to encapsulate style and make everything else more readable
|
||||||
|
|
||||||
def nope [] {
|
|
||||||
each { |it| $it == false }
|
|
||||||
}
|
|
||||||
|
|
||||||
def bright-cyan [] {
|
def bright-cyan [] {
|
||||||
each { |it| $"(ansi -e '96m')($it)(ansi reset)" }
|
each { |it| $"(ansi -e '96m')($it)(ansi reset)" }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user