1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 07:07:46 +00:00

Convert panache-git from script to module (#292)

In Nushell 0.68, the `source` command has been deprecated.
Going forward, the way to utilize custom commands is
to put them into a module and import them with `use`.

This means that `panache-git.nu` had to be converted
from a script to a module so that the main prompt command
could be imported by a user's Nushell environment config.
This commit is contained in:
Edward DeVries 2022-09-07 17:52:45 -04:00 committed by GitHub
parent bdfed700a4
commit 53108cd068
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,13 +2,13 @@
# An opinionated Git prompt for Nushell, styled after posh-git
#
# Quick Start:
# - Download this script (panache-git.nu)
# - Download this file (panache-git.nu)
# - In your Nushell config:
# - Source this script
# - Import the panache-git 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 script in your home directory:
# source ~/panache-git.nu
# - For example, with this file in your home directory:
# use ~/panache-git.nu panache-git
# let-env PROMPT_COMMAND = { panache-git }
# let-env PROMPT_INDICATOR = { "" }
# - Restart Nushell
@ -16,11 +16,14 @@
# For more documentation or to file an issue, see https://github.com/ehdevries/panache-git
# Internal commands for building up the panache-git shell prompt
module panache-plumbing {
# An opinionated Git prompt for Nushell, styled after posh-git
export def panache-git [] {
let prompt = ($'(current-dir) (repo-styled)' | str trim)
$'($prompt)> '
}
# Get the current directory with home abbreviated
export def "panache-git dir" [] {
# Get the current directory with home abbreviated
def current-dir [] {
let current_dir = ($env.PWD)
let current_dir_relative_to_home = (
@ -36,10 +39,10 @@ module panache-plumbing {
})
$'(ansi reset)($current_dir_abbreviated)'
}
}
# Get repository status as structured data
export def "panache-git structured" [] {
# Get repository status as structured data
def repo-structured [] {
let in_git_repo = (do --ignore-errors { git rev-parse --abbrev-ref HEAD } | is-empty | nope)
let status = (if $in_git_repo {
@ -239,11 +242,11 @@ module panache-plumbing {
worktree_deleted_count: $worktree_deleted_count,
merge_conflict_count: $merge_conflict_count
}
}
}
# Get repository status as a styled string
export def "panache-git styled" [] {
let status = (panache-git structured)
# Get repository status as a styled string
def repo-styled [] {
let status = (repo-structured)
let is_local_only = ($status.tracking_upstream_branch != true)
@ -373,104 +376,96 @@ module panache-plumbing {
} else {
''
})
}
}
# Helper commands to encapsulate style and make everything else more readable
# Helper commands to encapsulate style and make everything else more readable
def nope [] {
def nope [] {
each { |it| $it == false }
}
}
def bright-cyan [] {
def bright-cyan [] {
each { |it| $"(ansi -e '96m')($it)(ansi reset)" }
}
}
def bright-green [] {
def bright-green [] {
each { |it| $"(ansi -e '92m')($it)(ansi reset)" }
}
}
def bright-red [] {
def bright-red [] {
each { |it| $"(ansi -e '91m')($it)(ansi reset)" }
}
}
def bright-yellow [] {
def bright-yellow [] {
each { |it| $"(ansi -e '93m')($it)(ansi reset)" }
}
}
def green [] {
def green [] {
each { |it| $"(ansi green)($it)(ansi reset)" }
}
}
def red [] {
def red [] {
each { |it| $"(ansi red)($it)(ansi reset)" }
}
}
def branch-local-only [
def branch-local-only [
branch: string
] {
] {
$branch | bright-cyan
}
}
def branch-upstream-deleted [
def branch-upstream-deleted [
branch: string
] {
] {
$'($branch) (char failed)' | bright-cyan
}
}
def branch-up-to-date [
def branch-up-to-date [
branch: string
] {
] {
$'($branch) (char identical_to)' | bright-cyan
}
}
def branch-ahead [
def branch-ahead [
branch: string
ahead: int
] {
] {
$'($branch) (char branch_ahead)($ahead)' | bright-green
}
}
def branch-behind [
def branch-behind [
branch: string
behind: int
] {
] {
$'($branch) (char branch_behind)($behind)' | bright-red
}
}
def branch-ahead-and-behind [
def branch-ahead-and-behind [
branch: string
ahead: int
behind: int
] {
] {
$'($branch) (char branch_behind)($behind) (char branch_ahead)($ahead)' | bright-yellow
}
}
def staging-changes [
def staging-changes [
added: int
modified: int
deleted: int
] {
] {
$'+($added) ~($modified) -($deleted)' | green
}
}
def worktree-changes [
def worktree-changes [
added: int
modified: int
deleted: int
] {
] {
$'+($added) ~($modified) -($deleted)' | red
}
}
def unresolved-conflicts [
def unresolved-conflicts [
conflicts: int
] {
] {
$'!($conflicts)' | red
}
}
# An opinionated Git prompt for Nushell, styled after posh-git
def panache-git [] {
use panache-plumbing *
let prompt = ($'(panache_git dir) (panache-git styled)' | str trim)
$'($prompt)> '
}