Skip to content

Commit dd091df

Browse files
authored
Standard Library updates for 0.99 (#1585)
* Standard Library 0.99 chapter * Fix typos * Reports docs and help text * Add missing language-specifier * Updates from review comments
1 parent 815d11b commit dd091df

File tree

1 file changed

+184
-10
lines changed

1 file changed

+184
-10
lines changed

book/standard_library.md

Lines changed: 184 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Nushell ships with a standard library of useful commands written in native Nu. By default, the standard library is loaded into memory (but not automatically imported) when Nushell starts.
44

5+
[[toc]]
6+
7+
## Overview
8+
59
The standard library currently includes:
610

711
- Assertions
@@ -14,32 +18,202 @@ The standard library currently includes:
1418
To see a complete list of the commands available in the standard library, run the following:
1519

1620
```nu
17-
use std
18-
scope commands
19-
| where name =~ '^std '
20-
| select name usage extra_usage
21-
| wrap "std-lib"
21+
nu -c "
22+
use std
23+
scope commands
24+
| where name =~ '^std '
25+
| select name description extra_description
26+
| wrap 'Standard Library Commands'
27+
| table -e
28+
"
29+
```
30+
31+
::: note
32+
The `use std` command above loads the entire standard library so that you can see all of the commands at once. This is typically not how it will be used (more info below). It is also run in a separate Nu subshell simply so that it is not loaded into scope in the shell you are using.
33+
:::
34+
35+
## Importing the Standard Library
36+
37+
While working at the commandline, it can be convenient to load the entire standard library using:
38+
39+
```nu
40+
use std *
41+
```
42+
43+
However, this form should be avoided in custom commands and scripts since it has the longest load time.
44+
45+
::: important Optimal Startup when Using the Standard Library
46+
See the [notes below](#optimal-startup) on how to ensure that your configuration isn't loading the entire Standard Library.
47+
:::
48+
49+
### Importing Submodules
50+
51+
Each submodule of the standard library can be loaded separately. Again, _for best performance, load only the submodule(s) that you need in your code._
52+
53+
There are several forms that can be used:
54+
55+
#### 1. Import the submodule
56+
57+
Examples:
58+
59+
```nu
60+
use std/iter
61+
[2 5 "4" 7] | iter filter-map {|it| $it ** 2}
62+
63+
use std/help
64+
help ls
65+
help commands
66+
```
67+
68+
This form requires that you prefix the command using the submodule name. This can be useful in two scenarios.
69+
70+
1. In the `use std/iter` example, it's important to prefix the commands with the `iter` namespace. If, on the other hand, you were to `use std/iter *`, you would shadow the built-in `find` command with the `iter` module's `find` command.
71+
72+
2. In the `use std/help` example, `help` is both the submodule name and the name of the main command it exports. Using `use std/help` allows you to access:
73+
74+
```nu
75+
help
76+
help aliases
77+
help modules
78+
help commands
79+
help operators
80+
help externs
81+
```
82+
83+
In this case, the `std/help` commands are meant to shadow (replace) the built-in versions.
84+
85+
Submodules that are normally imported with `use std/<submodule>`:
86+
87+
- `use std/assert`: `assert` and its subcommands
88+
- `use std/bench`: The benchmarking command `bench`
89+
- `use std/dirs`: The directory stack command `dirs` and its subcommands
90+
- `use std/input`: The `input display` command
91+
- `use std/help`: An alternative version of the `help` command and its subcommands which supports completion and other features
92+
- `use std/iters`: Additional `iters`-prefixed iteration commands. Note: See-also alternative option #3 below.
93+
- `use std/log`: The `log <subcommands>` such as `log warning <msg>`
94+
- `use std/math`: Mathematical constants such as `$math.E`. These can also be imported without a prefix using Form #2 below.
95+
96+
#### 2. Import the _contents_ of the module directly
97+
98+
For certain submodules, you will want the commands from a submodule to be available in the current scope, so that you _can_ simply access the command by name. For instance:
99+
100+
```nu
101+
use std/formats *
102+
ls | to jsonl
103+
```
104+
105+
Submodules that are normally imported with `use std/<submodule> *`:
106+
107+
- `use std/dt *`: Additional commands for working with `date` values
108+
- `use std/formats *`: Additional `to` and `from` format conversions
109+
- `use std/math *`: The math constants without a prefix, such as `$E`. Note that the prefixed form #1 above is likely more understandable when reading and maintaining code.
110+
- `use std/xml *`: Additional commands for working with XML data
111+
112+
#### 3. Importing specific subcommands
113+
114+
As with most modules, you can choose to import only a subset of the commands. For instance, the following would import the `zip-with` command without requiring that it be called with`iter zip-with`.
115+
116+
```nu
117+
use std/iter [ zip-with ]
118+
```
119+
120+
#### 4. `use std <submodule>`
121+
122+
While it is _possible_ to import Standard Library submodules using a space-separated form:
123+
124+
```nu
125+
use std log
126+
use std formats *
22127
```
23128

24-
In addition, `stdlib-candidate`, found in the [nu_scripts Repository](https://github.com/nushell/nu_scripts/tree/main/stdlib-candidate/std-rfc), serves as a staging ground for new commands before they are added to the standard library.
129+
However, similar to `use std *`, this form first loads the _entire_ Standard Library into scope and _then_ imports the submodules. In contrast, using the slash-separated version _only_ imports the submodule and will be much faster as a result.
130+
131+
## The Standard Library Candidate Module
132+
133+
(Also known as `std-rfc`)
134+
135+
`stdlib-candidate`, found in the [nu_scripts Repository](https://github.com/nushell/nu_scripts/tree/main/stdlib-candidate/std-rfc), serves as a staging ground for possible Standard Library additions.
136+
137+
If you are interested in adding to the Standard Library, please submit your code via PR to the Candidate module in that repository. We also encourage you to install this module and provide feedback on upcoming candidate commands.
138+
139+
::: details More details
140+
141+
Candidate commands for the Standard Library should, in general:
142+
143+
- Have broad appeal - Be useful to a large number of users or use cases
144+
- Be well-written and clearly commented for future maintainers
145+
- Implement help comments with example usage
146+
- Have a description that explains why you feel the command should be a part of the standard library. Think of this as an "advertisement" of sorts to convince people to try the command and provide feedback so that it can be promoted in the future.
147+
148+
In order for a command to be graduated from RFC to the Standard Library, it must have:
149+
150+
- Positive feedback
151+
- Few (or no) outstanding issues and, of course, no significant issues
152+
- A PR author for the `std` submission. This does not necessarily have to be the original author of the command.
153+
- Test cases as part of the `std` submission PR
154+
155+
Ultimately a member of the core team will decide when and if to merge the command into `std` based on these criteria.
156+
157+
Of course, if a candidate command in `std-rfc` no longer works or has too many issues, it may be removed from or disabled in `std-rfc`.
25158

26-
::: tip Note
27-
Currently, parsing of the standard library impacts Nushell startup time. New commands are not being added to the library until this is resolved.
28159
:::
29160

161+
## Disabling the Standard Library
162+
30163
To disable the standard library, you can start using:
31164

32165
```nu
33166
nu --no-std-lib
34167
```
35168

36-
You will not be able to import the library using `use std *`, nor use any of its commands, if it is disabled in this way.
169+
This can be especially useful to minimize overhead when running a command in a subshell using `nu -c`. For example:
170+
171+
```nu
172+
nu --no-std-lib -n -c "$nu.startup-time"
173+
# => 1ms 125µs 10ns
174+
175+
nu -n -c "$nu.startup-time"
176+
# => 4ms 889µs 576ns
177+
```
178+
179+
You will not be able to import the library, any of its submodules, nor use any of its commands, when it is disabled in this way.
180+
181+
## Optimal Startup
182+
183+
If Nushell's startup time is important to your workflow, review your [startup configuration]([./configuration.md]) in `config.nu`, `env.nu`, and potentially others for inefficient use of the standard library. The following command should identify any problem areas:
184+
185+
```nu
186+
view files
187+
| enumerate | flatten
188+
| where filename !~ '^std'
189+
| where filename !~ '^entry'
190+
| where {|file|
191+
(view span $file.start $file.end) =~ 'use\W+std[^\/]'
192+
}
193+
```
194+
195+
Edit those files to use the recommended syntax below.
196+
:::
197+
198+
::: note
199+
If a Nushell library (e.g., from [the `nu_scripts` repository](https://github.com/nushell/nu_scripts)), example, or doc is using this syntax, please report it via an issue or PR. These will be updated over time after Nushell 0.99.0 is released.
200+
201+
If a third-party module is using this syntax, please report it to the author/maintainers to update.
202+
:::
203+
204+
## Viewing Standard Library Source
37205

38206
::: tip Did You Know?
39207
Because the standard library is simply composed of [custom commands](./custom_commands.html) in [modules](./modules.html) and [submodules](./modules.html#submodules-and-subcommands), you can see the source for each command with the [`view source`](/commands/docs/view_source.md) command. For example, to view the source for the `ellie` command (with syntax highlighting):
40208

41209
```nu
42-
use std *
210+
use std/util *
43211
view source ellie | nu-highlight
44212
:::
213+
214+
## Windows Path Syntax
215+
216+
::: important
217+
Nushell on Windows supports both forward-slashes and back-slashes as the path separator. However, to ensure cross-platform support for scripts and modules using `std`, using only the forward-slash `/` when importing Standard Library submodules is highly recommended.
218+
:::
45219
```

0 commit comments

Comments
 (0)