From 55bb501c71ae66bf311ee976c1eb450a1fd97f29 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Fri, 19 May 2023 22:27:45 +0200 Subject: [PATCH] FEATURE: fix the namespace of the standard library (not testing) (#9193) --- crates/nu-std/lib/dirs.nu | 26 ++++---- crates/nu-std/lib/help.nu | 10 +-- crates/nu-std/lib/iter.nu | 14 ++-- crates/nu-std/lib/log.nu | 110 ++++++++++++++++---------------- crates/nu-std/lib/mod.nu | 12 ++-- crates/nu-std/lib/testing.nu | 2 +- crates/nu-std/src/lib.rs | 12 ++-- crates/nu-std/tests/test_xml.nu | 6 +- 8 files changed, 96 insertions(+), 96 deletions(-) diff --git a/crates/nu-std/lib/dirs.nu b/crates/nu-std/lib/dirs.nu index b43e0e5086..6aebca2e14 100644 --- a/crates/nu-std/lib/dirs.nu +++ b/crates/nu-std/lib/dirs.nu @@ -8,7 +8,7 @@ export-env { # Add one or more directories to the list. # PWD becomes first of the newly added directories. -export def-env "dirs add" [ +export def-env add [ ...paths: string # directory or directories to add to working list ] { mut abspaths = [] @@ -27,29 +27,29 @@ export def-env "dirs add" [ _fetch 0 } -export alias enter = dirs add +export alias enter = add # Advance to the next directory in the list or wrap to beginning. -export def-env "dirs next" [ +export def-env next [ N:int = 1 # number of positions to move. ] { _fetch $N } -export alias n = dirs next +export alias n = next # Back up to the previous directory or wrap to the end. -export def-env "dirs prev" [ +export def-env prev [ N:int = 1 # number of positions to move. ] { _fetch (-1 * $N) } -export alias p = dirs prev +export alias p = prev # 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" [] { +export def-env drop [] { if ($env.DIRS_LIST | length) > 1 { let-env DIRS_LIST = ( ($env.DIRS_LIST | take $env.DIRS_POSITION) @@ -60,10 +60,10 @@ export def-env "dirs drop" [] { _fetch 0 } -export alias dexit = dirs drop +export alias dexit = drop # Display current working directories. -export def-env "dirs show" [] { +export def-env show [] { mut out = [] for $p in ($env.DIRS_LIST | enumerate) { $out = ($out | append [ @@ -75,11 +75,11 @@ export def-env "dirs show" [] { $out } -export alias shells = dirs show +export alias shells = show -export def-env "dirs goto" [shell?: int] { +export def-env goto [shell?: int] { if $shell == null { - return (dirs show) + return (show) } if $shell < 0 or $shell >= ($env.DIRS_LIST | length) { @@ -98,7 +98,7 @@ export def-env "dirs goto" [shell?: int] { cd ($env.DIRS_LIST | get $env.DIRS_POSITION) } -export alias g = dirs goto +export alias g = goto # fetch item helper def-env _fetch [ diff --git a/crates/nu-std/lib/help.nu b/crates/nu-std/lib/help.nu index 5cec601ec3..bff9546369 100644 --- a/crates/nu-std/lib/help.nu +++ b/crates/nu-std/lib/help.nu @@ -237,7 +237,7 @@ def show-module [module: record] { # · ────────┬─────── # · ╰── module not found # ╰──── -export def "help modules" [ +export def modules [ ...module: string@"nu-complete list-modules" # the name of module to get help on --find (-f): string # string to find in module names ] { @@ -340,7 +340,7 @@ def show-alias [alias: record] { # · ────────┬─────── # · ╰── alias not found # ╰──── -export def "help aliases" [ +export def aliases [ ...alias: string@"nu-complete list-aliases" # the name of alias to get help on --find (-f): string # string to find in alias names ] { @@ -373,7 +373,7 @@ def show-extern [extern: record] { } # Show help on nushell externs. -export def "help externs" [ +export def externs [ ...extern: string@"nu-complete list-externs" # the name of extern to get help on --find (-f): string # string to find in extern names ] { @@ -444,7 +444,7 @@ def show-operator [operator: record] { # · ────────┬─────── # · ╰── operator not found # ╰──── -export def "help operators" [ +export def operators [ ...operator: string@"nu-complete list-operators" # the name of operator to get help on --find (-f): string # string to find in operator names ] { @@ -647,7 +647,7 @@ def show-command [command: record] { } # Show help on commands. -export def "help commands" [ +export def commands [ ...command: string@"nu-complete list-commands" # the name of command to get help on --find (-f): string # string to find in command names and usage ] { diff --git a/crates/nu-std/lib/iter.nu b/crates/nu-std/lib/iter.nu index 8efe636506..21f520b0d3 100644 --- a/crates/nu-std/lib/iter.nu +++ b/crates/nu-std/lib/iter.nu @@ -27,7 +27,7 @@ # assert equal $found "abc" # assert equal $not_found null # ``` -export def "iter find" [ # -> any | null +export def find [ # -> any | null fn: closure # the closure used to perform the search ] { try { @@ -57,7 +57,7 @@ export def "iter find" [ # -> any | null # let res = ([3 5 13 91] | iter find-index $is_even) # assert equal $res -1 # ``` -export def "iter find-index" [ # -> int +export def find-index [ # -> int fn: closure # the closure used to perform the search ] { let matches = ( @@ -86,7 +86,7 @@ export def "iter find-index" [ # -> int # let res = ([1 2 3 4] | iter intersperse 0) # assert equal $res [1 0 2 0 3 0 4] # ``` -export def "iter intersperse" [ # -> list +export def intersperse [ # -> list separator: any # the separator to be used ] { reduce -f [] {|it, acc| @@ -117,7 +117,7 @@ export def "iter intersperse" [ # -> list # # assert equal $scanned [1, 3, 6] # ``` -export def "iter scan" [ # -> list +export def scan [ # -> list init: any # initial value to seed the initial state fn: closure # the closure to perform the scan --noinit(-n) # remove the initial value from the result @@ -144,7 +144,7 @@ export def "iter scan" [ # -> list # # assert equal $res [4 25 49] # ``` -export def "iter filter-map" [ # -> list +export def filter-map [ # -> list fn: closure # the closure to apply to the input ] { each {|$it| @@ -170,7 +170,7 @@ export def "iter filter-map" [ # -> list # ) # assert equal $res [6 9 18] # ``` -export def "iter flat-map" [ # -> list +export def flat-map [ # -> list fn: closure # the closure to map to the nested structures ] { each {|it| do $fn $it } | flatten @@ -188,7 +188,7 @@ export def "iter flat-map" [ # -> list # # assert equal $res [3 5 7] # ``` -export def "iter zip-with" [ # -> list +export def zip-with [ # -> list other: any # the structure to zip with fn: closure # the closure to apply to the zips ] { diff --git a/crates/nu-std/lib/log.nu b/crates/nu-std/lib/log.nu index db2363706f..b5b877f450 100644 --- a/crates/nu-std/lib/log.nu +++ b/crates/nu-std/lib/log.nu @@ -1,40 +1,40 @@ -export def "log CRITICAL_LEVEL" [] { +export def CRITICAL_LEVEL [] { 50 } -export def "log ERROR_LEVEL" [] { +export def ERROR_LEVEL [] { 40 } -export def "log WARNING_LEVEL" [] { +export def WARNING_LEVEL [] { 30 } -export def "log INFO_LEVEL" [] { +export def INFO_LEVEL [] { 20 } -export def "log DEBUG_LEVEL" [] { +export def DEBUG_LEVEL [] { 10 } def parse-string-level [ level: string ] { - if $level in [(log CRITICAL_LEVEL_PREFIX) (log CRITICAL_LEVEL_PREFIX --short) "CRIT" "CRITICAL"] { - log CRITICAL_LEVEL - } else if $level in [(log ERROR_LEVEL_PREFIX) (log ERROR_LEVEL_PREFIX --short) "ERROR" ] { - log ERROR_LEVEL - } else if $level in [(log WARNING_LEVEL_PREFIX) (log WARNING_LEVEL_PREFIX --short) "WARN" "WARNING"] { - log WARNING_LEVEL - } else if $level in [(log DEBUG_LEVEL_PREFIX) (log DEBUG_LEVEL_PREFIX --short) "DEBUG"] { - log DEBUG_LEVEL + if $level in [(CRITICAL_LEVEL_PREFIX) (CRITICAL_LEVEL_PREFIX --short) "CRIT" "CRITICAL"] { + CRITICAL_LEVEL + } else if $level in [(ERROR_LEVEL_PREFIX) (ERROR_LEVEL_PREFIX --short) "ERROR" ] { + ERROR_LEVEL + } else if $level in [(WARNING_LEVEL_PREFIX) (WARNING_LEVEL_PREFIX --short) "WARN" "WARNING"] { + WARNING_LEVEL + } else if $level in [(DEBUG_LEVEL_PREFIX) (DEBUG_LEVEL_PREFIX --short) "DEBUG"] { + DEBUG_LEVEL } else { - log INFO_LEVEL + INFO_LEVEL } } -export def "log CRITICAL_LEVEL_PREFIX" [ +export def CRITICAL_LEVEL_PREFIX [ --short (-s) ] { if $short { @@ -44,7 +44,7 @@ export def "log CRITICAL_LEVEL_PREFIX" [ } } -export def "log ERROR_LEVEL_PREFIX" [ +export def ERROR_LEVEL_PREFIX [ --short (-s) ] { if $short { @@ -54,7 +54,7 @@ export def "log ERROR_LEVEL_PREFIX" [ } } -export def "log WARNING_LEVEL_PREFIX" [ +export def WARNING_LEVEL_PREFIX [ --short (-s) ] { if $short { @@ -64,7 +64,7 @@ export def "log WARNING_LEVEL_PREFIX" [ } } -export def "log INFO_LEVEL_PREFIX" [ +export def INFO_LEVEL_PREFIX [ --short (-s) ] { if $short { @@ -74,7 +74,7 @@ export def "log INFO_LEVEL_PREFIX" [ } } -export def "log DEBUG_LEVEL_PREFIX" [ +export def DEBUG_LEVEL_PREFIX [ --short (-s) ] { if $short { @@ -88,41 +88,41 @@ def parse-int-level [ level: int, --short (-s) ] { - if $level >= (log CRITICAL_LEVEL) { + if $level >= (CRITICAL_LEVEL) { if $short { - log CRITICAL_LEVEL_PREFIX --short + CRITICAL_LEVEL_PREFIX --short } else { - log CRITICAL_LEVEL_PREFIX + CRITICAL_LEVEL_PREFIX } - } else if $level >= (log ERROR_LEVEL) { + } else if $level >= (ERROR_LEVEL) { if $short { - log ERROR_LEVEL_PREFIX --short + ERROR_LEVEL_PREFIX --short } else { - log ERROR_LEVEL_PREFIX + ERROR_LEVEL_PREFIX } - } else if $level >= (log WARNING_LEVEL) { + } else if $level >= (WARNING_LEVEL) { if $short { - log WARNING_LEVEL_PREFIX --short + WARNING_LEVEL_PREFIX --short } else { - log WARNING_LEVEL_PREFIX + WARNING_LEVEL_PREFIX } - } else if $level >= (log INFO_LEVEL) { + } else if $level >= (INFO_LEVEL) { if $short { - log INFO_LEVEL_PREFIX --short + INFO_LEVEL_PREFIX --short } else { - log INFO_LEVEL_PREFIX + INFO_LEVEL_PREFIX } } else { if $short { - log DEBUG_LEVEL_PREFIX --short + DEBUG_LEVEL_PREFIX --short } else { - log DEBUG_LEVEL_PREFIX + DEBUG_LEVEL_PREFIX } } } def current-log-level [] { - let env_level = ($env.NU_LOG_LEVEL? | default (log INFO_LEVEL)) + let env_level = ($env.NU_LOG_LEVEL? | default (INFO_LEVEL)) try { $env_level | into int @@ -144,86 +144,86 @@ def log-formatted [ } # Log a critical message -export def "log critical" [ +export def critical [ message: string, # A message --short (-s) # Whether to use a short prefix ] { - if (current-log-level) > (log CRITICAL_LEVEL) { + if (current-log-level) > (CRITICAL_LEVEL) { return } let prefix = if $short { - log CRITICAL_LEVEL_PREFIX --short + CRITICAL_LEVEL_PREFIX --short } else { - log CRITICAL_LEVEL_PREFIX + CRITICAL_LEVEL_PREFIX } log-formatted (ansi red_bold) $prefix $message } # Log an error message -export def "log error" [ +export def error [ message: string, # A message --short (-s) # Whether to use a short prefix ] { - if (current-log-level) > (log ERROR_LEVEL) { + if (current-log-level) > (ERROR_LEVEL) { return } let prefix = if $short { - log ERROR_LEVEL_PREFIX --short + ERROR_LEVEL_PREFIX --short } else { - log ERROR_LEVEL_PREFIX + ERROR_LEVEL_PREFIX } log-formatted (ansi red) $prefix $message } # Log a warning message -export def "log warning" [ +export def warning [ message: string, # A message --short (-s) # Whether to use a short prefix ] { - if (current-log-level) > (log WARNING_LEVEL) { + if (current-log-level) > (WARNING_LEVEL) { return } let prefix = if $short { - log WARNING_LEVEL_PREFIX --short + WARNING_LEVEL_PREFIX --short } else { - log WARNING_LEVEL_PREFIX + WARNING_LEVEL_PREFIX } log-formatted (ansi yellow) $prefix $message } # Log an info message -export def "log info" [ +export def info [ message: string, # A message --short (-s) # Whether to use a short prefix ] { - if (current-log-level) > (log INFO_LEVEL) { + if (current-log-level) > (INFO_LEVEL) { return } let prefix = if $short { - log INFO_LEVEL_PREFIX --short + INFO_LEVEL_PREFIX --short } else { - log INFO_LEVEL_PREFIX + INFO_LEVEL_PREFIX } log-formatted (ansi default) $prefix $message } # Log a debug message -export def "log debug" [ +export def debug [ message: string, # A message --short (-s) # Whether to use a short prefix ] { - if (current-log-level) > (log DEBUG_LEVEL) { + if (current-log-level) > (DEBUG_LEVEL) { return } let prefix = if $short { - log DEBUG_LEVEL_PREFIX --short + DEBUG_LEVEL_PREFIX --short } else { - log DEBUG_LEVEL_PREFIX + DEBUG_LEVEL_PREFIX } log-formatted (ansi default_dimmed) $prefix $message } @@ -237,7 +237,7 @@ export def "log debug" [ # # Examples: # - std log custom "my message" $"(ansi yellow)[%LEVEL%]MY MESSAGE: %MSG% [%DATE%](ansi reset)" (std log WARNING_LEVEL) -export def "log custom" [ +export def custom [ message: string, # A message format: string, # A format log_level: int # A log level diff --git a/crates/nu-std/lib/mod.nu b/crates/nu-std/lib/mod.nu index 7d21b142c6..8b5c940de8 100644 --- a/crates/nu-std/lib/mod.nu +++ b/crates/nu-std/lib/mod.nu @@ -1,15 +1,15 @@ # std.nu, `used` to load all standard library components -export use dirs * +export use dirs export-env { use dirs * } -export use help * -export use iter * -export use log * +export use help +export use iter +export use log export use testing * -export use xml * -export use dt [datetime-diff, pretty-print-duration] +export use xml +use dt [datetime-diff, pretty-print-duration] # Add the given paths to the PATH. # diff --git a/crates/nu-std/lib/testing.nu b/crates/nu-std/lib/testing.nu index eb3e9576ca..b2dc5f2fa1 100644 --- a/crates/nu-std/lib/testing.nu +++ b/crates/nu-std/lib/testing.nu @@ -5,7 +5,7 @@ # Assert commands and test runner. # ################################################################################## -use log * +use log # Universal assert command # diff --git a/crates/nu-std/src/lib.rs b/crates/nu-std/src/lib.rs index b25f4e7473..1aae505562 100644 --- a/crates/nu-std/src/lib.rs +++ b/crates/nu-std/src/lib.rs @@ -97,12 +97,12 @@ pub fn load_standard_library( ("std help externs", "help externs"), ("std help operators", "help operators"), - ("enter", "enter"), - ("shells", "shells"), - ("g", "g"), - ("n", "n"), - ("p", "p"), - ("dexit", "dexit"), + ("enter", "dirs enter"), + ("shells", "dirs shells"), + ("g", "dirs g"), + ("n", "dirs n"), + ("p", "dirs p"), + ("dexit", "dirs dexit"), ]; let mut working_set = StateWorkingSet::new(engine_state); diff --git a/crates/nu-std/tests/test_xml.nu b/crates/nu-std/tests/test_xml.nu index 27054d8406..54f019718c 100644 --- a/crates/nu-std/tests/test_xml.nu +++ b/crates/nu-std/tests/test_xml.nu @@ -1,6 +1,6 @@ -use std "xaccess" -use std "xupdate" -use std "xinsert" +use std xml xaccess +use std xml xupdate +use std xml xinsert use std "assert equal" export def setup [] {