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`
This commit is contained in:
Igor 2024-05-26 21:37:01 +04:00 committed by GitHub
parent e8df70a406
commit 9d399d8902
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 273 additions and 336 deletions

View File

@ -1,33 +0,0 @@
# Separate Startup Script
This config sources a separate startup script so that you can edit a `nu` script
instead of fiddling around with the `toml` config.
Additionally it provides some new commands:
- `config edit`: Opens the `config.toml` in a new editor (depending on the `EDITOR` env variable)
- `startup edit`: Opens the `startup.nu` in a new editor (depending on the `EDITOR` env variable)
- `startup path`: Like `config path`, provides the path to the `startup.nu` file
## The `config edit` and `startup edit` command
Both `config edit` and `startup edit` accept the flag `--editor` (or `-e`) that
lets you define a different editor. To open the config in
[VSCode](https://github.com/microsoft/vscode) VScode for example do:
```
config edit -e code
startup edit -e code
```
## The `startup path` and `config path` command
Like `config path` the command `startup path` provides the path to the
`startup.nu` file. This can be used e.g. to open both, the `config.toml` and the
`startup.nu` in an editor of your choice:
```
vim (config path) (startup path)
```

View File

@ -1 +0,0 @@
startup = ["source startup.nu"]

View File

@ -1,8 +0,0 @@
def "config edit" [
--editor (-e): string # Use a different editor (default is `$nu.env.EDITOR`)
] { let editor = (if $editor != "" { $editor } { $nu.env.EDITOR }); ^$editor (config path) }
def "startup" [] { help startup }
def "startup path" [] { config path | path dirname | append startup.nu | path join }
def "startup edit" [
--editor (-e): string # Use a different editor (default is `$nu.env.EDITOR`)
] { let editor = (if $editor != "" { $editor } { $nu.env.EDITOR }); ^$editor (startup path) }

View File

@ -1,55 +0,0 @@
def playground [topic, block] {
with-env [N 5 REJECT slow] {
echo $topic " tests" (char newline) | str collect
do $block
}
}
def scene [
topic: any
--tag: string
block: block
] {
$" ($topic)(char newline)"
do $block
}
def play [
topic: any
--tag: string
block: block
] {
let title = $topic;
let is_tag_empty = ($tag | empty?);
let should_run_all = ($nu.env | default RUN_ALL $false | get RUN_ALL);
if $is_tag_empty {
do $block
} {
if $tag == $nu.env.REJECT and $should_run_all {
$" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)"
} { do $block }
}
}
def expect [
actual: any
--to-be: any
] {
let are_equal = ($actual | zip { $to-be } | pivot header_names values | each {|case|
let values = $case.values;
$values.0 == $values.1
}
| all? $it) and (($actual | get | length) == ($to-be | get | length));
let line = (if $true == $are_equal {
$"(ansi green)ok(ansi reset)(char newline)"
} {
$"(ansi red)failed(ansi reset)(char newline)"
});
$" ($title) ... ($line)"
}

View File

@ -1,5 +0,0 @@
# Prompt Scripts
### Definition
These scripts should be used to draw a custom command prompt in nushell. They can include anything that we think is appropriate for prompts such as `git` commands, `starship`, `oh-my-posh`, etc.

View File

@ -1,94 +0,0 @@
# 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) "
}
# 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 "\") })
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])/"
}
})
$"~/($short-part | str collect)($path | last)"
} {
$"~/($path | last)"
}
} {
"~"
}
} {
let parent = (echo $path | drop | str substring [0 1] | each { echo $it "/" })
$"/($parent)($path | last)"
}
} {
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 (
echo (ansi green) (ansi yellow_bold) (ansi cyan) (ansi blue) (ansi red) (ansi red_dimmed) (ansi red) |
rotate counter-clockwise |
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 |
pivot status colour | each --numbered { |icon|
let idx = $icon.index;
if $icon.item.status == $k {
$"($icon.item.colour)($icns | nth $idx)"
} {
= $nothing
}
} | 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 | 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
}
} {
""
}
}
}

View File

@ -1,98 +0,0 @@
# This is a work in progress. Not working yet but you can see where I'm going.
def construct_prompt [] {
# let decorator = (char prompt)
let decorator = (create_second_line)
# not using machine name
# let machine_name = (sys | get host.hostname)
# the current working directory
# let current_dir = (pwd)
let current_dir = (home_abbrev)
# the current bit branch
# let git_status = (git -c core.quotepath=false -c color.status=false status -uall --short --branch)
let git_info = (do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str collect )
# what to put in the title
let title_bar = (set_title)
# get the terminal width
let term_width = (term size -w)
# get the current time
let current_time = (date now | date format '%I:%M:%S%.3f %p')
# let's construct the left and right prompt
# the left side of the prompt with ansi colors
let left_colored = $"(ansi gb)($current_dir)(ansi cb)(char lparen)($git_info)(char rparen)(ansi reset)"
# the left prompt length without the ansi escapes
let left_len = ($left_colored | ansi strip | str length)
# the right side of the prompt with ansi colors
let right_colored = $"(ansi blue)($nu.env.CMD_DURATION_MS)|(ansi dark_gray)($current_time)(ansi reset)"
# the right prompt length *with* ansi escapes (need this to determine how many escape chars there are)
let right_colored_len = ( $right_colored | str length)
# the right prompt length without the ansi escapes
let right_len = ($right_colored | ansi strip | str length)
# let's calculate the length of the right prompt so we know how much to pad the left prompt
let calculated_right_len = ($term_width - $left_len + ($right_colored_len - $right_len))
# finally, let's make the prompt
let the_prompt = $"($left_colored)($right_colored | str lpad -c ' ' -l $calculated_right_len)(char newline)($decorator) "
# let's update the title bar now
echo $title_bar
# and last, but not least, let's print the prompt
echo $the_prompt | str collect
## put this in your config.toml
# prompt = "construct_prompt"
## also you need to source the file in your startup like
# "source C:\\Users\\username\\source\\some\\folder\\nu_scripts\\prompt\\left_and_right_prompt.nu",
}
# Abbreviate home path
def home_abbrev [] {
let is_home_in_path = (pwd | into string | str starts-with $nu.home-dir)
if $is_home_in_path {
let lin-home = ($nu.home-dir | into string | str find-replace -a '\\' '/' | str downcase)
let lin-pwd = (pwd | into string | str find-replace -a '\\' '/' | str downcase)
$lin-pwd | str find-replace $lin-home '~'
} {
pwd
}
}
# Get Git Info custom commands
def git_br [] {
$"(ansi gb)(pwd)(ansi reset)(char lparen)(ansi cb)(do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str collect)(ansi reset)(char rparen)(char newline)(ansi yb)(date now | date format '%m/%d/%Y %I:%M:%S%.3f %p')(ansi reset)¯\\_(char lparen)ツ)_/¯(char prompt) "
}
# Set Title String custom commands
def set_title_str [str-arg] {
$"(ansi title) ($str-arg) (char bel)"
}
# def get_abbrev_pwd_win [] {
# echo [(pwd | split row '\' | first (pwd | split row '\' | length | each { $it - 1} ) | str substring '0,1' | format '{$it}/' | append (pwd | split row '\' | last ) | str collect)] | str collect
# }
def get_abbrev_pwd_lin [] {
# echo [(pwd | split row '/' | first (pwd | split row '/' | length | each { $it - 1} ) | each { str substring '0,1' | format '{$it}/' } | append (pwd | split row '/' | last ) | str collect)] | str collect
echo [(home_abbrev | split row '/' | first (home_abbrev | split row '/' | length | each { $it - 1} ) | each { str substring '0,1' | format '{$it}/' } | append (home_abbrev | split row '/' | last ) | str collect)] | str collect
}
def set_title [] {
set_title_str (build-string (get_abbrev_pwd_lin) ' ' (term size -w) 'x' (term size -t) | str collect)
}
def create_second_line [] {
build-string (ansi gb) (char -u "2514") (char -u "2500") ' $ ' (ansi cb) (char prompt) (ansi reset)
}

View File

@ -1,38 +0,0 @@
source lib/scripts.nu
def mock-now [] {
"2021-08-29 03:31:21.290597200 -05:00" | str to-datetime
}
def people [] {
[
[ 'name', 'tz'];
[ 'andres', 'America/Guayaquil']
[ 'fdncred', 'US/Central']
]
}
playground "std/date" {
scene 'command: "date local"' {
play "adds times in local timezone" {
let expected_times = [
[time];
["Sun Aug 29 03:31:21 2021"]
["Sun Aug 29 03:31:21 2021"]
];
let actual = (people |
date local (mock-now) |
select time
);
expect $actual --to-be $expected_times
}
}
}

View File

@ -1,2 +0,0 @@
source language/playground.nu
source tests/language/std/date.nu

View File

@ -0,0 +1,54 @@
export def main [topic, closure] {
with-env {N: 5 REJECT: slow } {
print (echo $topic " tests" (char newline) | str join)
do $closure
}
}
export def scene [
topic: any
--tag: string
closure: closure
] {
print $" ($topic)(char newline)"
do $closure
}
export def play [
topic: any
--tag: string
closure: closure
] {
let is_tag_empty = ($tag | is-empty);
let should_run_all = ($env | get -i RUN_ALL | default false);
if $is_tag_empty {
do $closure $topic
} else {
if $tag == $env.REJECT and $should_run_all {
$" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)"
} else {
do $closure $topic
}
}
}
export def expect [
topic: string
actual: list<any>
--to-be: list<any>
] {
let are_equal = (($actual | length) == ($to_be | length)) and ($actual | zip $to_be | all {|case|
$case.0 == $case.1
}
)
let line = (if true == $are_equal {
$"(ansi green)ok(ansi reset)(char newline)"
} else {
$"(ansi red)failed(ansi reset)(char newline)"
}
)
$" ($topic) ... ($line)"
}

View File

@ -1,7 +1,7 @@
def "date local" [now] {
export def "date local" [now] {
insert time {|value|
let converted = ($now | date to-timezone $value.tz);
$converted | date format '%c'
$converted | format date '%c'
}
}

View File

@ -0,0 +1,34 @@
use ../playground *
use ../std/date.nu *
def mock-now [] {
"2021-08-29 03:31:21" | into datetime
}
def people [] {
[
[ 'name', 'tz'];
[ 'andres', 'America/Guayaquil']
[ 'fdncred', 'US/Central']
]
}
playground "std/date" {
scene 'command: "date local"' {
play "adds times in local timezone" {|topic|
let expected_times = [
"Sun Aug 29 03:31:21 2021"
"Sun Aug 29 03:31:21 2021"
] | into datetime;
let actual = (people | date local (mock-now) | get time | into datetime)
expect $topic $actual --to-be $expected_times
}
}
}

View File

@ -0,0 +1,97 @@
# 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
}
}
}
}

View File

@ -0,0 +1,86 @@
# This is a work in progress. Not working yet but you can see where I'm going.
export def construct_prompt [] {
# let decorator = (char prompt)
let decorator = (create_second_line)
# not using machine name
# let machine_name = (sys | get host.hostname)
# the current working directory
# let current_dir = (pwd)
let current_dir = (home_abbrev)
# the current bit branch
# let git_status = (git -c core.quotepath=false -c color.status=false status -uall --short --branch)
let git_info = (do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str join )
# what to put in the title
let title_bar = (set_title)
# get the terminal width
let term_width = (term size).columns
# get the curren time
let current_time = (date now | format date '%I:%M:%S%.3f %p')
# let's construct the left and right prompt
# the left side of the prompt with ansi colors
let left_colored = $"(ansi gb)($current_dir)(ansi cb)(char lparen)($git_info)(char rparen)(ansi reset)"
# the left prompt length without the ansi escapes
let left_len = ($left_colored | ansi strip | str length)
# the right side of the prompt with ansi colors
let right_colored = $"(ansi blue)($env.CMD_DURATION_MS)|(ansi dark_gray)($current_time)(ansi reset)"
# let's calcuate the length of the right prompt so we know how much to pad the left prompt
let calculated_right_len = ($term_width - $left_len)
# finally, let's make the prompt
let the_prompt = $"($left_colored)($right_colored | fill -a r -c ' ' -w $calculated_right_len)(char newline)($decorator) "
# let's update the title bar now
print -n $title_bar
# and last, but not least, let's print the prompt
echo $the_prompt
## put this in your config.toml
# prompt = "construct_prompt"
## also you need to source the file in your startup like
# "source C:\\Users\\username\\source\\some\\folder\\nu_scripts\\prompt\\left_and_right_prompt.nu",
}
# Abbreviate home path
def home_abbrev [] {
let is_home_in_path = (pwd | into string | str starts-with $nu.home-path)
if $is_home_in_path {
let lin_home = ($nu.home-path | into string | str replace -a '\\' '/' | str downcase)
let lin_pwd = (pwd | into string | str replace -a '\\' '/' | str downcase)
$lin_pwd | str replace $lin_home '~'
} else {
pwd
}
}
# Get Git Info custom commands
def git_br [] {
$"(ansi gb)(pwd)(ansi reset)(char lparen)(ansi cb)(do -i { git rev-parse --abbrev-ref HEAD } | str trim -c (char nl) | str join)(ansi reset)(char rparen)(char newline)(ansi yb)(date now | format date '%m/%d/%Y %I:%M:%S%.3f %p')(ansi reset)¯\\_(char lparen)ツ)_/¯(char prompt) "
}
# Set Title String custom commands
def set_title_str [str_arg] {
$"(ansi title) ($str_arg) (char bel)"
}
def get_abbrev_pwd_lin [] {
home_abbrev | split row '/' | first (home_abbrev | split row '/' | length | each { $in - 1} ) | each { str substring 0..1 | $'($in)/' } | append (home_abbrev | split row '/' | last ) | str join
}
def set_title [] {
set_title_str ([(get_abbrev_pwd_lin) ' ' (term size).columns 'x' (term size).rows ] | str join)
}
def create_second_line [] {
[(ansi gb) (char -u "2514") (char -u "2500") ' $ ' (ansi cb) (char prompt) (ansi reset)] | str join
}