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