forked from nushell/nushell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stdlib: refactor into a multi-module library (nushell#8815)
- Loading branch information
Showing
17 changed files
with
775 additions
and
747 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std 'log debug' | ||
use std 'log info' | ||
use std 'log warning' | ||
use std 'log error' | ||
use std 'log critical' | ||
|
||
export def log [] { | ||
log debug "Debug message" | ||
log info "Info message" | ||
log warning "Warning message" | ||
log error "Error message" | ||
log critical "Critical message" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
# Universal assert command | ||
# | ||
# If the condition is not true, it generates an error. | ||
# | ||
# # Example | ||
# | ||
# ```nushell | ||
# >_ assert (3 == 3) | ||
# >_ assert (42 == 3) | ||
# Error: | ||
# × Assertion failed: | ||
# ╭─[myscript.nu:11:1] | ||
# 11 │ assert (3 == 3) | ||
# 12 │ assert (42 == 3) | ||
# · ───┬──── | ||
# · ╰── It is not true. | ||
# 13 │ | ||
# ╰──── | ||
# ``` | ||
# | ||
# The --error-label flag can be used if you want to create a custom assert command: | ||
# ``` | ||
# def "assert even" [number: int] { | ||
# assert ($number mod 2 == 0) --error-label { | ||
# start: (metadata $number).span.start, | ||
# end: (metadata $number).span.end, | ||
# text: $"($number) is not an even number", | ||
# } | ||
# } | ||
# ``` | ||
export def main [ | ||
condition: bool, # Condition, which should be true | ||
message?: string, # Optional error message | ||
--error-label: record # Label for `error make` if you want to create a custom assert | ||
] { | ||
if $condition { return } | ||
let span = (metadata $condition).span | ||
error make { | ||
msg: ($message | default "Assertion failed."), | ||
label: ($error_label | default { | ||
text: "It is not true.", | ||
start: (metadata $condition).span.start, | ||
end: (metadata $condition).span.end | ||
}) | ||
} | ||
} | ||
|
||
# Assert that executing the code generates an error | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert error {|| missing_command} # passes | ||
# > assert error {|| 12} # fails | ||
export def "assert error" [ | ||
code: closure, | ||
message?: string | ||
] { | ||
let error_raised = (try { do $code; false } catch { true }) | ||
main ($error_raised) $message --error-label { | ||
start: (metadata $code).span.start | ||
end: (metadata $code).span.end | ||
text: $"There were no error during code execution: (view source $code)" | ||
} | ||
} | ||
|
||
# Skip the current test case | ||
# | ||
# # Examples | ||
# | ||
# if $condition { assert skip } | ||
export def "assert skip" [] { | ||
error make {msg: "ASSERT:SKIP"} | ||
} | ||
|
||
|
||
# Assert $left == $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert equal 1 1 # passes | ||
# > assert equal (0.1 + 0.2) 0.3 | ||
# > assert equal 1 2 # fails | ||
export def "assert equal" [left: any, right: any, message?: string] { | ||
main ($left == $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"They are not equal. Left = ($left). Right = ($right)." | ||
} | ||
} | ||
|
||
# Assert $left != $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert not equal 1 2 # passes | ||
# > assert not equal 1 "apple" # passes | ||
# > assert not equal 7 7 # fails | ||
export def "assert not equal" [left: any, right: any, message?: string] { | ||
main ($left != $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"They both are ($left)." | ||
} | ||
} | ||
|
||
# Assert $left <= $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert less or equal 1 2 # passes | ||
# > assert less or equal 1 1 # passes | ||
# > assert less or equal 1 0 # fails | ||
export def "assert less or equal" [left: any, right: any, message?: string] { | ||
main ($left <= $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"Left: ($left), Right: ($right)" | ||
} | ||
} | ||
|
||
# Assert $left < $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert less 1 2 # passes | ||
# > assert less 1 1 # fails | ||
export def "assert less" [left: any, right: any, message?: string] { | ||
main ($left < $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"Left: ($left), Right: ($right)" | ||
} | ||
} | ||
|
||
# Assert $left > $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert greater 2 1 # passes | ||
# > assert greater 2 2 # fails | ||
export def "assert greater" [left: any, right: any, message?: string] { | ||
main ($left > $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"Left: ($left), Right: ($right)" | ||
} | ||
} | ||
|
||
# Assert $left >= $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert greater or equal 2 1 # passes | ||
# > assert greater or equal 2 2 # passes | ||
# > assert greater or equal 1 2 # fails | ||
export def "assert greater or equal" [left: any, right: any, message?: string] { | ||
main ($left >= $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"Left: ($left), Right: ($right)" | ||
} | ||
} | ||
|
||
# Assert length of $left is $right | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert length [0, 0] 2 # passes | ||
# > assert length [0] 3 # fails | ||
export def "assert length" [left: list, right: int, message?: string] { | ||
main (($left | length) == $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"Length of ($left) is ($left | length), not ($right)" | ||
} | ||
} | ||
|
||
# Assert that ($left | str contains $right) | ||
# | ||
# For more documentation see the assert command | ||
# | ||
# # Examples | ||
# | ||
# > assert str contains "arst" "rs" # passes | ||
# > assert str contains "arst" "k" # fails | ||
export def "assert str contains" [left: string, right: string, message?: string] { | ||
main ($left | str contains $right) $message --error-label { | ||
start: (metadata $left).span.start | ||
end: (metadata $right).span.end | ||
text: $"'($left)' does not contain '($right)'." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Maintain a list of working directories and navigates them | ||
|
||
# the directory stack | ||
export-env { | ||
let-env DIRS_POSITION = 0 | ||
let-env DIRS_LIST = [($env.PWD | path expand)] | ||
} | ||
|
||
# Add one or more directories to the list. | ||
# PWD becomes first of the newly added directories. | ||
export def-env "dirs add" [ | ||
...paths: string # directory or directories to add to working list | ||
] { | ||
mut abspaths = [] | ||
for p in $paths { | ||
let exp = ($p | path expand) | ||
if ($exp | path type) != 'dir' { | ||
let span = (metadata $p).span | ||
error make {msg: "not a directory", label: {text: "not a directory", start: $span.start, end: $span.end } } | ||
} | ||
$abspaths = ($abspaths | append $exp) | ||
|
||
} | ||
let-env DIRS_LIST = ($env.DIRS_LIST | insert ($env.DIRS_POSITION + 1) $abspaths | flatten) | ||
let-env DIRS_POSITION = $env.DIRS_POSITION + 1 | ||
|
||
_fetch 0 | ||
} | ||
|
||
# Advance to the next directory in the list or wrap to beginning. | ||
export def-env "dirs next" [ | ||
N:int = 1 # number of positions to move. | ||
] { | ||
_fetch $N | ||
} | ||
|
||
# Back up to the previous directory or wrap to the end. | ||
export def-env "dirs prev" [ | ||
N:int = 1 # number of positions to move. | ||
] { | ||
_fetch (-1 * $N) | ||
} | ||
|
||
# Drop the current directory from the list, if it's not the only one. | ||
# PWD becomes the next working directory | ||
export def-env "dirs drop" [] { | ||
if ($env.DIRS_LIST | length) > 1 { | ||
let-env DIRS_LIST = ( | ||
($env.DIRS_LIST | take $env.DIRS_POSITION) | ||
| append ($env.DIRS_LIST | skip ($env.DIRS_POSITION + 1)) | ||
) | ||
} | ||
|
||
_fetch 0 | ||
} | ||
|
||
# Display current working directories. | ||
export def-env "dirs show" [] { | ||
mut out = [] | ||
for $p in ($env.DIRS_LIST | enumerate) { | ||
$out = ($out | append [ | ||
[active, path]; | ||
[($p.index == $env.DIRS_POSITION), $p.item] | ||
]) | ||
} | ||
|
||
$out | ||
} | ||
|
||
# fetch item helper | ||
def-env _fetch [ | ||
offset: int, # signed change to position | ||
] { | ||
# nushell 'mod' operator is really 'remainder', can return negative values. | ||
# see: https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder | ||
let pos = ($env.DIRS_POSITION | ||
+ $offset | ||
+ ($env.DIRS_LIST | length) | ||
) mod ($env.DIRS_LIST | length) | ||
let-env DIRS_POSITION = $pos | ||
|
||
cd ($env.DIRS_LIST | get $pos ) | ||
} |
Oops, something went wrong.