1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00
nu_scripts/modules/prompt/git_status_prompt.nu
Igor 9d399d8902
Port before_v0.60/config_management before_v0.60/language before_v0.60/prompt before_v0.60/tests (#851)
This PR is part of porting all old scripts #221 and includes a set of
modules:

- `config_management`
- `language`
- `tests`
- `prompt`

## 12 files changed

### `config_management`
This module was removed, because it was related to the old `config.toml`
format
1. `README.md`: removed
2. `config.toml`: removed
3. `startup.nu`: removed

### `language`
4. `playground.nu` : unchanged because this is just `source ...`
5. `std.nu` : unchanged because this is just `source ...`
6. `playground/lib.nu` -> `playground/mod.nu` and all commands have been
exported
7. `std/date.nu` + `export` keyword

### `tests`
This module contained only the usage of `language` module, so I merged
it with `language`
8. `tests/language/std/date.nu` -> `modules/language/tests/date.nu`
9. `main.nu`: removed because it was just `source date.nu`

### `prompt`
10. `git_status_prompt.nu` -> `modules/git_status_prompt.nu` + `export`
keyword
11. `left_and_right_prompt.nu` -> `modules/left_and_right_prompt.nu` +
`export` keyword
12. `README.md` : has been removed as a duplicate of
`modules/prompt/README.md`
2024-05-26 12:37:01 -05:00

97 lines
3.3 KiB
Text

# Displays a prompt
export def git-status-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.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 | 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)"
}
} else {
"~"
}
} else {
let parent = (echo $path | drop | str substring [0 1] | each {|it| echo $it "/" })
$"/($parent)($path | last)"
}
} else {
pwd
}
}
# Map of git status codes to ANSI colour codes
def git-prompt-map [] {
echo a m r c d "??" u |
rotate --ccw |
reject column0 | append (
echo (ansi green) (ansi yellow_bold) (ansi cyan) (ansi blue) (ansi red) (ansi red_dimmed) (ansi red) |
rotate --ccw |
reject column0
) | headers
}
# Gets an icon and a colour for a given git status code
def git-prompt-icons [k] {
let icns = ["✚ " "* " "➜ " "⇒ " "✖ " "? " "! "];
git-prompt-map |
transpose status colour | enumerate | each { |icon|
let idx = $icon.index;
if $icon.item.status == $k {
$"($icon.item.colour)($icns | get $idx)"
}
} | compact
}
# Checks git status of current working directory and displays an icon
def git-branch-icon [] {
do -i {
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 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
}
}
}
}