Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently use nu as code block lang #1807

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The Standard Library also includes a helper command. The default `path add` beha
a directory so that it has higher precedence than the rest of the path. For example, the following can be
added to your startup config:

```nushell
```nu
use std/util "path add"
path add "~/.local/bin"
path add ($env.CARGO_HOME | path join "bin")
Expand Down
22 changes: 11 additions & 11 deletions book/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Suppose you have multiple lists you want to concatenate together, but you also w
some individual values. This can be done with `append` and `prepend`, but the spread
operator can let you do it more easily.

```nushell
```nu
let dogs = [Spot, Teddy, Tommy]
let cats = ["Mr. Humphrey Montgomery", Kitten]
[
Expand All @@ -166,7 +166,7 @@ let cats = ["Mr. Humphrey Montgomery", Kitten]
```

The below code is an equivalent version using `append`:
```nushell
```nu
$dogs |
append Polly |
append ($cats | each { |elt| $"($elt) \(cat\)" }) |
Expand All @@ -186,7 +186,7 @@ only be used before variables (`...$foo`), subexpressions (`...(foo)`), and list
The `...` also won't be recognized as the spread operator if there's any whitespace between it and
the next expression:

```nushell
```nu
[ ... [] ]
# => ╭───┬────────────────╮
# => │ 0 │ ... │
Expand All @@ -201,14 +201,14 @@ This is mainly so that `...` won't be confused for the spread operator in comman
Let's say you have a record with some configuration information and you want to add more fields to
this record:

```nushell
```nu
let config = { path: /tmp, limit: 5 }
```

You can make a new record with all the fields of `$config` and some new additions using the spread
operator. You can use the spread multiple records inside a single record literal.

```nushell
```nu
{
...$config,
users: [alice bob],
Expand Down Expand Up @@ -244,7 +244,7 @@ external command.

Here is an example custom command that has a rest parameter:

```nushell
```nu
def foo [ --flag req opt? ...args ] { [$flag, $req, $opt, $args] | to nuon }
```

Expand All @@ -255,7 +255,7 @@ If you have a list of arguments to pass to `args`, you can spread it the same wa
[inside a list literal](#in-list-literals). The same rules apply: the spread operator is only
recognized before variables, subexpressions, and list literals, and no whitespace is allowed in between.

```nushell
```nu
foo "bar" "baz" ...[1 2 3] # With ..., the numbers are treated as separate arguments
# => [false, bar, baz, [1, 2, 3]]
foo "bar" "baz" [1 2 3] # Without ..., [1 2 3] is treated as a single argument
Expand All @@ -265,30 +265,30 @@ foo "bar" "baz" [1 2 3] # Without ..., [1 2 3] is treated as a single argument
A more useful way to use the spread operator is if you have another command with a rest parameter
and you want it to forward its arguments to `foo`:

```nushell
```nu
def bar [ ...args ] { foo --flag "bar" "baz" ...$args }
bar 1 2 3
# => [true, bar, baz, [1, 2, 3]]
```

You can spread multiple lists in a single call, and also intersperse individual arguments:

```nushell
```nu
foo "bar" "baz" 1 ...[2 3] 4 5 ...(6..9 | take 2) last
# => [false, bar, baz, [1, 2, 3, 4, 5, 6, 7, last]]
```

Flags/named arguments can go after a spread argument, just like they can go after regular rest arguments:

```nushell
```nu
foo "bar" "baz" 1 ...[2 3] --flag 4
# => [true, bar, baz, [1, 2, 3, 4]]
```

If a spread argument comes before an optional positional parameter, that optional parameter is treated
as being omitted:

```nushell
```nu
foo "bar" ...[1 2] "not opt" # The null means no argument was given for opt
# => [false, bar, null, [1, 2, "not opt"]]
```
14 changes: 7 additions & 7 deletions book/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ Compare the following two command-lines that create a directory with tomorrow's

Using subexpressions:

```nushell
```nu
mkdir $'((date now) + 1day | format date '%F') Report'
```

or using pipelines:

```nushell
```nu
date now # 1: today
| $in + 1day # 2: tomorrow
| format date '%F' # 3: Format as YYYY-MM-DD
Expand All @@ -95,21 +95,21 @@ Let's examine the contents of `$in` on each line of the above example:

Certain [filter commands](/commands/categories/filters.html) may modify the pipeline input to their closure in order to provide more convenient access to the expected context. For example:

```nushell
```nu
1..10 | each {$in * 2}
```

Rather than referring to the entire range of 10 digits, the `each` filter modifies `$in` to refer to the value of the _current iteration_.

In most filters, the pipeline input and its resulting `$in` will be the same as the closure parameter. For the `each` filter, the following example is equivalent to the one above:

```nushell
```nu
1..10 | each {|value| $value * 2}
```

However, some filters will assign an even more convenient value to their closures' input. The `update` filter is one example. The pipeline input to the `update` command's closure (as well as `$in`) refers to the _column_ being updated, while the closure parameter refers to the entire record. As a result, the following two examples are also equivalent:

```nushell
```nu
ls | update name {|file| $file.name | str upcase}
ls | update name {str upcase}
```
Expand Down Expand Up @@ -257,7 +257,7 @@ While `$in` can be reused as demonstrated above, assigning its value to another

Example:

```nushell
```nu
def "date info" [] {
let day = $in
print ($day | format date '%v')
Expand Down Expand Up @@ -400,7 +400,7 @@ Are one and the same.
::: tip Note
The phrase _"are one and the same"_ above only applies to the graphical output in the shell, it does not mean the two data structures are the same:

```nushell
```nu
(ls) == (ls | table)
# => false
```
Expand Down
8 changes: 4 additions & 4 deletions book/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Nushell provides a set of "assertion" commands in the standard library.
One could use built-in equality / order tests such as `==` or `<=` or more complex commands and throw errors manually when an expected condition fails, but using what the standard library has to offer is arguably easier!

In the following, it will be assumed that the `std assert` module has been imported inside the current scope
```nushell
```nu
use std assert
```

Expand Down Expand Up @@ -132,7 +132,7 @@ The convention is that any command fully exported from the `tests` module will b
If your Nushell script or module is not part of a [Nupm] package, the simplest way is to write tests in standalone scripts and then call them, either from a `Makefile` or in a CI:

Let's say we have a simple `math.nu` module which contains a simple Fibonacci command:
```nushell
```nu
# `fib n` is the n-th Fibonacci number
export def fib [n: int] [ nothing -> int ] {
if $n == 0 {
Expand All @@ -145,7 +145,7 @@ export def fib [n: int] [ nothing -> int ] {
}
```
then a test script called `tests.nu` could look like
```nushell
```nu
use math.nu fib
use std assert
Expand All @@ -171,7 +171,7 @@ It is also possible to define tests in Nushell as functions with descriptive nam
them dynamically without requiring a [Nupm] package. The following uses `scope commands` and a
second instance of Nushell to run the generated list of tests.

```nushell
```nu
use std assert
source fib.nu
Expand Down
Loading