Skip to content

Commit

Permalink
Unifies all cookbook codeblocks to use the same style. (#1151)
Browse files Browse the repository at this point in the history
The unified style favours the removal of any prompt indicator such as `>`.
  • Loading branch information
arnau committed Nov 19, 2023
1 parent f912847 commit 7183cab
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 105 deletions.
2 changes: 1 addition & 1 deletion cookbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where you will find interesting scripts written for Nushell.
And if you are looking for cool oneliners like this one

```nu
> (http get https://api.chucknorris.io/jokes/random).value
(http get https://api.chucknorris.io/jokes/random).value
```

head to join Nushell's discord channel and check the
Expand Down
12 changes: 6 additions & 6 deletions cookbook/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Use `help inc` to get more information.
Read the file's initial contents

```nu
> open Cargo.toml | get package.version
open Cargo.toml | get package.version
```

Output
Expand All @@ -24,7 +24,7 @@ Make the edit to the version number and save it.
_Note: running this command should work but it will reorder the toml file alphabetically by section._

```nu
> open Cargo.toml | upsert package.version { |p| $p | get package.version | inc --patch } | save Cargo.toml
open Cargo.toml | upsert package.version { |p| $p | get package.version | inc --patch } | save Cargo.toml
```

Output
Expand All @@ -33,7 +33,7 @@ _none_
View the changes we made to the file.

```nu
> open Cargo.toml | get package.version
open Cargo.toml | get package.version
```

Output
Expand All @@ -58,7 +58,7 @@ Fugazi:In On The Kill Taker:1993
You can parse it into a table.

```nu
> open bands.txt | lines | split column ":" Band Album Year | skip 1 | sort-by Year
open bands.txt | lines | split column ":" Band Album Year | skip 1 | sort-by Year
```

Output
Expand All @@ -84,7 +84,7 @@ open bands.txt | lines | parse "{Band}:{Album}:{Year}" | skip 1 | sort-by Year
Or, you can utilize the `headers` command to use the first row as a header row. The only difference would be the headers would match the case of the text file. So, in this case, the headers would be lowercase.

```nu
> open bands.txt | lines | split column ":" | headers | sort-by year
open bands.txt | lines | split column ":" | headers | sort-by year
```

---
Expand All @@ -94,7 +94,7 @@ Or, you can utilize the `headers` command to use the first row as a header row.
Suppose you would like to check the number of lines the string "Value" appears per file in the nushell project, then sort those files by largest line count.

```nu
> rg -c Value | lines | split column ":" file line_count | into int line_count | sort-by line_count | reverse
rg -c Value | lines | split column ":" file line_count | into int line_count | sort-by line_count | reverse
```

Output
Expand Down
6 changes: 3 additions & 3 deletions cookbook/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Nu can help with common `Git` tasks like removing all local branches which have
**Warning**: This command will hard delete the merged branches from your machine. You may want to check the branches selected for deletion by omitting the last git command.

```nu
> git branch --merged | lines | where ($it != "* master" and $it != "* main") | each {|br| git branch -D ($br | str trim) } | str trim
git branch --merged | lines | where ($it != "* master" and $it != "* main") | each {|br| git branch -D ($br | str trim) } | str trim
```

Output
Expand All @@ -25,7 +25,7 @@ Output
Parse formatted commit messages (more details in the parsing git log section)

```nu
> git log --pretty=%h»¦«%aN»¦«%s»¦«%aD | lines | split column "»¦«" sha1 committer desc merged_at | first 10
git log --pretty=%h»¦«%aN»¦«%s»¦«%aD | lines | split column "»¦«" sha1 committer desc merged_at | first 10
```

Output
Expand Down Expand Up @@ -55,7 +55,7 @@ Output
_Note: the `histogram` command is not yet ported to the latest version_

```nu
> git log --pretty=%h»¦«%aN»¦«%s»¦«%aD | lines | split column "»¦«" sha1 committer desc merged_at | histogram committer merger | sort-by merger | reverse
git log --pretty=%h»¦«%aN»¦«%s»¦«%aD | lines | split column "»¦«" sha1 committer desc merged_at | histogram committer merger | sort-by merger | reverse
```

```
Expand Down
4 changes: 2 additions & 2 deletions cookbook/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The `help` command is a good way to become familiar with all that Nu has to offe
### How to see all supported commands:

```nu
> help commands
help commands
```

---
Expand All @@ -19,7 +19,7 @@ The `help` command is a good way to become familiar with all that Nu has to offe
To find more specific information on a command, use `help <COMMAND>`. This works for regular commands (i.e. `http`) and subcommands (i.e. `http get`):

```nu
> help http get
help http get
```

Output:
Expand Down
14 changes: 8 additions & 6 deletions cookbook/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: HTTP
### Fetching JSON from a url

```nu
> http get https://jsonplaceholder.typicode.com/posts | first 5
http get https://jsonplaceholder.typicode.com/posts | first 5
```

Output
Expand Down Expand Up @@ -63,7 +63,7 @@ An example JSON file, `urls.json`, with the following contents:
```

```nu
> open urls.json | get urls | each { |u| http get $u }
open urls.json | get urls | each { |u| http get $u }
```

Output
Expand Down Expand Up @@ -94,7 +94,7 @@ Output
If you specify the `--raw` flag, you'll see 3 separate json objects, one in each row.

```nu
> open urls.json | get urls | each { |u| http get $u -r }
open urls.json | get urls | each { |u| http get $u -r }
```

Output
Expand Down Expand Up @@ -132,7 +132,7 @@ Output
To combine these responses together into a valid JSON array, you can turn the table into json.

```nu
> open urls.json | get urls | each { |u| http get $u } | to json
open urls.json | get urls | each { |u| http get $u } | to json
```

Output
Expand Down Expand Up @@ -175,7 +175,7 @@ Making a `post` request to an endpoint with a JSON payload. To make long request
```

```nu
> open payload.json | get my_payload | to json | post https://jsonplaceholder.typicode.com/posts $in
open payload.json | get my_payload | to json | post https://jsonplaceholder.typicode.com/posts $in
```

Output
Expand All @@ -193,9 +193,11 @@ Output
We can put this all together into a pipeline where we read data, manipulate it, and then send it back to the API. Lets `fetch` a post, `increment` the id, and `post` it back to the endpoint. In this particular example, the test endpoint gives back an arbitrary response which we can't actually mutate.

```nu
> open urls.json | get urls | first | http get $in | upsert id {|item| $item.id | inc} | to json | post https://jsonplaceholder.typicode.com/posts $in
open urls.json | get urls | first | http get $in | upsert id {|item| $item.id | inc} | to json | post https://jsonplaceholder.typicode.com/posts $in
```

Output

```
━━━━━
id
Expand Down
4 changes: 2 additions & 2 deletions cookbook/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Nu offers the ability to do some basic parsing.
How to parse an arbitrary pattern from a string of text into a multi-column table.

```nu
> cargo search shells --limit 10 | lines | parse "{crate_name} = {version} #{description}" | str trim
cargo search shells --limit 10 | lines | parse "{crate_name} = {version} #{description}" | str trim
```

Output
Output:

```
───┬──────────────┬─────────────────┬────────────────────────────────────────────────────────────────────────────────
Expand Down
38 changes: 19 additions & 19 deletions cookbook/parsing_git_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ title: Parsing Git Log
This `git log` command is interesting but you can't do a lot with it like this.

```nu
> git log
git log
```

Let's make it more parsable

```nu
> git log --pretty="%h|%s|%aN|%aE|%aD" -n 25
git log --pretty="%h|%s|%aN|%aE|%aD" -n 25
```

This will work but I've been burnt by this in the past when a pipe `|` gets injected in the commits.

So, let's try again with something that most likely won't show up in commits, `»¦«`. Also, since we're not using a pipe now we don't have to use quotes around the pretty format string. Notice that the output is just a bunch of strings.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5
```

```
Expand All @@ -39,7 +39,7 @@ Ahh, much better. Now that we have the raw data, let's try to parse it with nu.
First we need to get it in lines or rows. Notice that the output is now in a table format.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines
```

```
Expand All @@ -62,7 +62,7 @@ That's more like nushell, but it would be nice to have some columns.
We used the delimiter `»¦«` specifically so we can create columns so let's use it like this.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«"
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«"
```

```
Expand Down Expand Up @@ -90,7 +90,7 @@ Yay, for columns! But wait, it would really be nice if those columns had somethi
Let's try adding the columns names to `split column` like this.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date
```

Ahhh, that looks much better.
Expand Down Expand Up @@ -118,7 +118,7 @@ Ahhh, that looks much better.
Hmmm, that date string is a string. If it were a date vs a string it could be used for sorting by date. The way we do that is we have to convert the datetime to a real datetime and update the column. Try this.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime}
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime}
```

Now this looks more nu-ish
Expand Down Expand Up @@ -146,7 +146,7 @@ Now this looks more nu-ish
If we want to revert back to a date string we can do something like this with the `nth` command and the `get` command.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | select 3 | get date | date format | get 0
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 5 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | select 3 | get date | date format | get 0
```

```
Expand All @@ -157,7 +157,7 @@ Cool! Now that we have a real datetime we can do some interesting things with it
Let's try `sort-by` first

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | sort-by date
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | sort-by date
```

```
Expand Down Expand Up @@ -229,7 +229,7 @@ Let's try `sort-by` first
That's neat but what if I want it sorted in the opposite order? Try the `reverse` command and notice the newest commits are at the top.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | sort-by date | reverse
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | sort-by date | reverse
```

```
Expand Down Expand Up @@ -301,7 +301,7 @@ That's neat but what if I want it sorted in the opposite order? Try the `reverse
Now let's try `group-by` and see what happens. This is a tiny bit tricky because dates are tricky. When you use `group-by` on dates you have to remember to use the `group-by date` subcommand so it's `group-by date date_column_name`.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime | date format '%Y-%m-%d'} | group-by date
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime | date format '%Y-%m-%d'} | group-by date
```

```
Expand All @@ -317,7 +317,7 @@ Now let's try `group-by` and see what happens. This is a tiny bit tricky because
This would look better if we transpose the data and name the columns

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime | date format '%Y-%m-%d'} | group-by date | transpose date count
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime | date format '%Y-%m-%d'} | group-by date | transpose date count
```

```
Expand All @@ -335,7 +335,7 @@ This would look better if we transpose the data and name the columns
How about `where` now? Show only the records that are less than a year old.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day))
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day))
```

```
Expand Down Expand Up @@ -408,7 +408,7 @@ How about `where` now? Show only the records that are less than a year old.
Or even show me all the commits in the last 7 days.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 7day))
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD -n 25 | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 7day))
```

```
Expand Down Expand Up @@ -480,7 +480,7 @@ Or even show me all the commits in the last 7 days.
Now, with the 365 day slice of data, let's `group-by` name where the commits are less than a year old. This table has a lot of columns so it's unreadable. However, if we `group-by` name and `transpose` the table things will look much cleaner. `Pivot` takes rows and turns them into columns or turns columns into rows.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
```

```
Expand Down Expand Up @@ -517,14 +517,14 @@ error: Unknown column
Here's one tip for dealing with this error. We have a `do` command that has an `--ignore_errors` parameter. This is how you'd use it in the above example, if it were giving errors.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | do -i { split column "»¦«" commit subject name email date } | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | do -i { split column "»¦«" commit subject name email date } | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose
```

Now, back to parsing.
What if we throw in the `sort-by` and `reverse` commands for good measure? Also, while we're in there, let's get rid of the `[table 21 rows]` thing too. We do that by using the `length` command on each row of column1.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | reverse
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | where ($it.date > ((date now) - 365day)) | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | reverse
```

```
Expand All @@ -550,7 +550,7 @@ What if we throw in the `sort-by` and `reverse` commands for good measure? Also,
This is still a lot of data so let's just look at the top 10 and use the `rename` command to name the columns. We could've also provided the column names with the `transpose` command.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | rename name commits | reverse | first 10
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | rename name commits | reverse | first 10
```

```
Expand All @@ -575,7 +575,7 @@ And there you have it. The top 10 committers and we learned a little bit of pars
Here's one last little known command. Perhaps you don't want your table numbered starting with 0. Here's a way to change that with the `table` command.

```nu
> git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | rename name commits | reverse | first 10 | table -n 1
git log --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD | lines | split column "»¦«" commit subject name email date | upsert date {|d| $d.date | into datetime} | group-by name | transpose | upsert column1 {|c| $c.column1 | length} | sort-by column1 | rename name commits | reverse | first 10 | table -n 1
```

```
Expand Down
Loading

0 comments on commit 7183cab

Please sign in to comment.