nushell/crates/nu-std/std/util/mod.nu
Douglas 2a3805c164
Virtual std module subdirectories (#14040)
# Description

Uses "normal" module `std/<submodule>/mod.nu` instead of renaming the
files (as requested in #13842).

# User-Facing Changes

No user-facing changes other than in `view files` results. Imports
remain the same after this PR.

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

Also manually confirmed that it does not interfere with nupm, since we
did have a conflict at one point (and it's not possible to test here).

# Performance Tests

## Linux

### Nushell Startup - No config

```nu
bench --pretty -n 200  { <path_to>/nu -c "exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 22ms 730µs 768ns +/- 1ms 515µs 942ns
| This commit | 9ms 312µs 68ns +/- 709µs 378ns
| Yesterday's nightly | 9ms 230µs 953ns +/- 9ms 67µs 689ns

### Nushell Startup - Load full standard library

Measures relative impact of a full `use std *`, which isn't recommended,
but worth tracking.

```nu
bench --pretty -n 200  { <path_to>/nu -c "use std *; exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 23ms 10µs 636ns +/- 1ms 277µs 854ns
| This commit | 26ms 922µs 769ns +/- 562µs 538ns
| Yesterday's nightly | 28ms 133µs 95ns +/- 761µs 943ns
| `deprecated_dirs` removal PR * | 23ms 610µs 333ns +/- 369µs 436ns

\* Current increase is partially due to double-loading `dirs` with
removal warning in older version.

# After Submitting

Still TODO - Update standard library doc
2024-10-10 06:56:37 -05:00

125 lines
3.1 KiB
Plaintext

# Add the given paths to the PATH.
#
# # Example
# - adding some dummy paths to an empty PATH
# ```nushell
# >_ with-env { PATH: [] } {
# std path add "foo"
# std path add "bar" "baz"
# std path add "fooo" --append
#
# assert equal $env.PATH ["bar" "baz" "foo" "fooo"]
#
# print (std path add "returned" --ret)
# }
# ╭───┬──────────╮
# │ 0 │ returned │
# │ 1 │ bar │
# │ 2 │ baz │
# │ 3 │ foo │
# │ 4 │ fooo │
# ╰───┴──────────╯
# ```
# - adding paths based on the operating system
# ```nushell
# >_ std path add {linux: "foo", windows: "bar", darwin: "baz"}
# ```
export def --env "path add" [
--ret (-r) # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a) # append to $env.PATH instead of prepending to.
...paths # the paths to add to $env.PATH.
] {
let span = (metadata $paths).span
let paths = $paths | flatten
if ($paths | is-empty) or ($paths | length) == 0 {
error make {msg: "Empty input", label: {
text: "Provide at least one string or a record",
span: $span
}}
}
let path_name = if "PATH" in $env { "PATH" } else { "Path" }
let paths = $paths | each {|p|
let p = match ($p | describe | str replace --regex '<.*' '') {
"string" => $p,
"record" => { $p | get --ignore-errors $nu.os-info.name },
}
$p | path expand --no-symlink
}
if null in $paths or ($paths | is-empty) {
error make {msg: "Empty input", label: {
text: $"Received a record, that does not contain a ($nu.os-info.name) key",
span: $span
}}
}
load-env {$path_name: (
$env
| get $path_name
| split row (char esep)
| if $append { append $paths } else { prepend $paths }
)}
if $ret {
$env | get $path_name
}
}
# the cute and friendly mascot of Nushell :)
export def ellie [] {
let ellie = [
" __ ,",
" .--()°'.'",
"'|, . ,'",
" !_-(_\\",
]
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
}
# repeat anything a bunch of times, yielding a list of *n* times the input
#
# # Examples
# repeat a string
# > "foo" | std repeat 3 | str join
# "foofoofoo"
export def repeat [
n: int # the number of repetitions, must be positive
]: any -> list<any> {
let item = $in
if $n < 0 {
let span = metadata $n | get span
error make {
msg: $"(ansi red_bold)invalid_argument(ansi reset)"
label: {
text: $"n should be a positive integer, found ($n)"
span: $span
}
}
}
if $n == 0 {
return []
}
1..$n | each { $item }
}
# return a null device file.
#
# # Examples
# run a command and ignore it's stderr output
# > cat xxx.txt e> (null-device)
export def null-device []: nothing -> path {
if ($nu.os-info.name | str downcase) == "windows" {
'\\.\NUL'
} else {
"/dev/null"
}
}