Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port before_v0.60/config_management before_v0.60/language before_v0.60/prompt before_v0.60/tests #851

Merged
Prev Previous commit
Next Next commit
port prompt/git_status_prompt.nu
  • Loading branch information
39555 committed May 26, 2024
commit f3556e2e1777acb0fea19032b8352938ea5a80b3
119 changes: 61 additions & 58 deletions sourced/prompt/git_status_prompt.nu
Original file line number Diff line number Diff line change
@@ -1,54 +1,62 @@
# Displays a prompt
def git-status-prompt [] {
let not_windows = ($nu.path | first | into string | str contains '/')
$"(ansi reset)(ansi green)(if $not_windows {$nu.env.USER} {$nu.env.USERNAME})(ansi reset)@(hostname | str trim):(ansi green_dimmed)(prompt-pwd)(ansi reset)(git-branch-icon)(ansi reset)(char newline)(char prompt) "
let not_windows = ($nu.os-info.name !~ "windows")
$"(ansi reset)(ansi green)(if $not_windows {
$env.USER
} else {
$env.USERNAME
}
)(ansi reset)@(hostname | str trim):(ansi green_dimmed)(prompt-pwd)(ansi reset)(git-branch-icon)(ansi reset)(char newline)(char prompt) "
}

# Returns a shortened pwd for use in prompt
def prompt-pwd [] {
let not_windows = ($nu.path | first | into string | str contains '/')
let path = (pwd | if $not_windows { split row "/" } { split row "\" })
let home = (if $not_windows { ($nu.env.HOME | split row "/") } { (echo [$nu.env.HOMEDRIVE $nu.env.HOMEPATH] | path join | split row "\") })
let not_windows = ($nu.os-info.name !~ "windows")
let path = (pwd | if $not_windows { split row "/" } else { split row '\' })
let home = (if $not_windows {
($env.HOME | split row "/")
} else {
(echo [$env.HOMEDRIVE $env.HOMEPATH] | path join | split row '\')
}
)

if ($path | length) > 1 {
if ($home | reduce { $it in $path }) {
let path-without-home = ($path | skip ($home | length))

if ($path-without-home | wrap | compact | length) > 0 {
let parent = ($path | skip ($home | length) | drop)

if ($parent | wrap | compact | length) > 0 {
let short-part = ($parent | each { |part|
if ($part | str starts-with ".") {
$"($part | str substring [0 2])/"
} {
$"($part | str substring [0 1])/"
if ($home | all {|it| $it in $path }) {
let path_without_home = ($path | skip ($home | length))
if ($path_without_home | wrap path | compact | length) > 0 {
let parent = ($path | skip ($home | length) | drop)
if ($parent | wrap parent | compact | length) > 0 {
let short_part = ($parent | each { |part|
if ($part | str starts-with ".") {
$"($part | str substring [0 2])/"
} else {
$"($part | str substring [0 1])/"
}
})
$"~/( | str join)($path | last)"
} else {
$"~/($path | last)"
}
})
$"~/($short-part | str collect)($path | last)"
} {
$"~/($path | last)"
}
} {
"~"
} else {
"~"
}
} else {
let parent = (echo $path | drop | str substring [0 1] | each {|it| echo $it "/" })
$"/($parent)($path | last)"
}
} {
let parent = (echo $path | drop | str substring [0 1] | each { echo $it "/" })
$"/($parent)($path | last)"
}
} {
pwd
} else {
pwd
}
}
}

# Map of git status codes to ANSI colour codes
def git-prompt-map [] {
echo a m r c d "??" u |
rotate counter-clockwise |
reject Column0 | append (
rotate --ccw |
reject column0 | append (
echo (ansi green) (ansi yellow_bold) (ansi cyan) (ansi blue) (ansi red) (ansi red_dimmed) (ansi red) |
rotate counter-clockwise |
reject Column0
rotate --ccw |
reject column0
) | headers
}

Expand All @@ -57,14 +65,11 @@ def git-prompt-icons [k] {
let icns = ["✚ " "* " "➜ " "⇒ " "✖ " "? " "! "];

git-prompt-map |
pivot status colour | each --numbered { |icon|
let idx = $icon.index;

if $icon.item.status == $k {
$"($icon.item.colour)($icns | nth $idx)"
} {
= $nothing
}
transpose status colour | enumerate | each { |icon|
let idx = $icon.index;
if $icon.item.status == $k {
$"($icon.item.colour)($icns | get $idx)"
}
} | compact
}

Expand All @@ -74,21 +79,19 @@ def git-branch-icon [] {
let branch = (do -i { git rev-parse --abbrev-ref HEAD } | str trim)

if ($branch | str length) > 0 {
let modified = (do -i { git status --porcelain } | split row "\n" | str trim | split column " " status file);

if ($modified | get | first | empty?) {
$"|(ansi green)($branch)(ansi reset):(ansi green)✓(ansi reset)"
} {
let modified2 = (do -i { git status --porcelain } | split row "\n" | str substring [0 1])
let branch-colour = (if (echo $modified2 | each { $it in [A M R C D] } | reduce { $it or $acc }) {
"yellow"
} {
"red"
})
$"|(ansi $branch-colour)($branch)(ansi reset):($modified | get status | uniq | str downcase | each { git-prompt-icons $it })" | str collect
}
} {
""
let modified = (do -i { git status --porcelain } | split row "\n" | str trim | split column " " status file);
if ($modified | get 0 | first | is-empty) {
$"|(ansi green)($branch)(ansi reset):(ansi green)✓(ansi reset)"
} else {
let modified2 = (do -i { git status --porcelain } | split row "\n" | str substring [0 1])
let branch_colour = (if (echo $modified2 | each {|it| $it in [A M R C D] } | reduce {|it acc| $it or $acc }) {
"yellow"
} else {
"red"
}
)
$"|(ansi $branch_colour)($branch)(ansi reset):($modified | get status | uniq | str downcase | each {|it| git-prompt-icons $it })" | str join
}
}
}
}