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

Add real Nushell syntax highlighting support #1078

Merged
merged 9 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: Update docs
  • Loading branch information
hustcer committed Oct 2, 2023
commit 4690f53b921a6a6e2f2879f74dfe4ce6dde76d73
26 changes: 13 additions & 13 deletions book/loading_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Nushell Object Notation (NUON) aims to be for Nushell what JavaScript Object Not
That is, NUON code is a valid Nushell code that describes some data structure.
For example, this is a valid NUON (example from the [default configuration file](https://github.com/nushell/nushell/blob/main/crates/nu-utils/src/sample_config/default_config.nu)):

```
```nu
{
menus: [
# Configuration for default nushell menus
Expand Down Expand Up @@ -97,7 +97,7 @@ An important part of working with data coming from outside Nu is that it's not a

Let's imagine that we're given this data file:

```
```nu
> open people.txt
Octavia | Butler | Writer
Bob | Ross | Painter
Expand All @@ -108,7 +108,7 @@ Each bit of data we want is separated by the pipe ('|') symbol, and each person

The first thing we want to do when bringing in the file is to work with it a line at a time:

```
```nu
> open people.txt | lines
───┬──────────────────────────────
0 │ Octavia | Butler | Writer
Expand All @@ -119,7 +119,7 @@ The first thing we want to do when bringing in the file is to work with it a lin

We can see that we're working with the lines because we're back into a list. Our next step is to see if we can split up the rows into something a little more useful. For that, we'll use the [`split`](/commands/docs/split.md) command. [`split`](/commands/docs/split.md), as the name implies, gives us a way to split a delimited string. We will use [`split`](/commands/docs/split.md)'s `column` subcommand to split the contents across multiple columns. We tell it what the delimiter is, and it does the rest:

```
```nu
> open people.txt | lines | split column "|"
───┬──────────┬───────────┬───────────
# │ column1 │ column2 │ column3
Expand All @@ -132,7 +132,7 @@ We can see that we're working with the lines because we're back into a list. Our

That _almost_ looks correct. It looks like there's an extra space there. Let's [`trim`](/commands/docs/str_trim.md) that extra space:

```
```nu
> open people.txt | lines | split column "|" | str trim
───┬─────────┬─────────┬──────────
# │ column1 │ column2 │ column3
Expand All @@ -145,7 +145,7 @@ That _almost_ looks correct. It looks like there's an extra space there. Let's [

Not bad. The [`split`](/commands/docs/split.md) command gives us data we can use. It also goes ahead and gives us default column names:

```
```nu
> open people.txt | lines | split column "|" | str trim | get column1
───┬─────────
0 │ Octavia
Expand All @@ -156,7 +156,7 @@ Not bad. The [`split`](/commands/docs/split.md) command gives us data we can use

We can also name our columns instead of using the default names:

```
```nu
> open people.txt | lines | split column "|" first_name last_name job | str trim
───┬────────────┬───────────┬──────────
# │ first_name │ last_name │ job
Expand All @@ -169,7 +169,7 @@ We can also name our columns instead of using the default names:

Now that our data is in a table, we can use all the commands we've used on tables before:

```
```nu
> open people.txt | lines | split column "|" first_name last_name job | str trim | sort-by first_name
───┬────────────┬───────────┬──────────
# │ first_name │ last_name │ job
Expand All @@ -188,7 +188,7 @@ There are other commands you can use to work with strings:

There is also a set of helper commands we can call if we know the data has a structure that Nu should be able to understand. For example, let's open a Rust lock file:

```
```nu
> open Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
Expand All @@ -207,7 +207,7 @@ The [`from`](/commands/docs/from.md) command can be used for each of the structu

While it's helpful to be able to open a file and immediately work with a table of its data, this is not always what you want to do. To get to the underlying text, the [`open`](/commands/docs/open.md) command can take an optional `--raw` flag:

```
```nu
> open Cargo.toml --raw
[package] name = "nu"
version = "0.1.3"
Expand All @@ -220,19 +220,19 @@ license = "MIT"

SQLite databases are automatically detected by [`open`](/commands/docs/open.md), no matter what their file extension is. You can open a whole database:

```
```nu
> open foo.db
```

Or [`get`](/commands/docs/get.md) a specific table:

```
```nu
> open foo.db | get some_table
```

Or run any SQL query you like:

```
```nu
> open foo.db | query db "select * from some_table"
```

Expand Down
14 changes: 7 additions & 7 deletions book/thinking_in_nu.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ While it does have these amenities, Nushell isn't bash. The bash way of working,

In Nushell, we use the `>` as the greater-than operator. This fits better with the language aspect of Nushell. Instead, you pipe to a command that has the job of saving content:

```
```nu
> "hello" | save output.txt
```

Expand All @@ -34,7 +34,7 @@ An important part of Nushell's design and specifically where it differs from man

For example, the following doesn't make sense in Nushell, and will fail to execute if run as a script:

```
```nu
"def abc [] { 1 + 2 }" | save output.nu
source "output.nu"
abc
Expand All @@ -44,7 +44,7 @@ The [`source`](/commands/docs/source.md) command will grow the source that is co

Another common issue is trying to dynamically create the filename to source from:

```
```nu
> source $"($my_path)/common.nu"
```

Expand All @@ -62,21 +62,21 @@ You might wonder why Nushell uses immutable variables. Early on in Nushell's dev

Just because Nushell variables are immutable doesn't mean things don't change. Nushell makes heavy use of the technique of "shadowing". Shadowing means creating a new variable with the same name as a previously declared variable. For example, say you had an `$x` in scope, and you wanted a new `$x` that was one greater:

```
```nu
let x = $x + 1
```

This new `x` is visible to any code that follows this line. Careful use of shadowing can make for an easier time working with variables, though it's not required.

Loop counters are another common pattern for mutable variables and are built into most iterating commands, for example you can get both each item and an index of each item using [`each`](/commands/docs/each.md):

```
```nu
> ls | enumerate | each { |it| $"Number ($it.index) is size ($it.item.size)" }
```

You can also use the [`reduce`](/commands/docs/reduce.md) command to work in the same way you might mutate a variable in a loop. For example, if you wanted to find the largest string in a list of strings, you might do:

```
```nu
> [one, two, three, four, five, six] | reduce {|curr, max|
if ($curr | str length) > ($max | str length) {
$curr
Expand All @@ -96,7 +96,7 @@ In Nushell, blocks control their own environment. Changes to the environment are

In practice, this lets you write some concise code for working with subdirectories, for example, if you wanted to build each sub-project in the current directory, you could run:

```
```nu
> ls | each { |it|
cd $it.name
make
Expand Down
Loading