diff --git a/README.md b/README.md index 6776cef..1ceee7e 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,16 @@ Execute blocks of nushell code within markdown documents, write results back to ```nushell no-run # this block won't run as it has the option `no-run` in its code fence -> git clone https://github.com/nushell-prophet/numd; cd numd -> nupm install --force --path . # optionally you can install this module via nupm -> use numd -> numd run README.md --no-save + +git clone https://github.com/nushell-prophet/numd; cd numd + +# optionally you can install this module via nupm +nupm install --force --path . + +use numd + +# run it on any file to check +numd run z_examples/1_simple_markdown/simple_markdown.md --no-save ``` ## How it works @@ -21,14 +27,17 @@ Experienced nushell users can understand the logic better by looking at [example ### Details on parsing code blocks and displaying the output 1. `numd` looks for code blocks marked with ` ```nushell ` or ` ```nu `. -2. In code blocks that do not contain any lines starting with the `>` symbol, `numd` executes the entire code block as is. If the code produces any output, the output is added next to the code block after an empty line, a line with the word `Output:`, and another empty line. The output is enclosed in code fences without a language identifier. -3. In code blocks that contain one or more lines starting with the `>` symbol, `numd` filters only lines that start with the `>` or `#` symbol. It executes or prints those lines one by one, and outputs the results immediately after the executed line. +2. Code blocks are split into command groups by blank lines (double newlines). Each command group is executed separately. +3. Output from each command group is displayed inline with `# =>` prefix immediately after the command. +4. Multiline commands (pipelines split across lines without blank lines) are treated as a single command group. +5. Plain `#` comments are preserved; `# =>` output lines are regenerated on each run. +6. Use the `separate-block` fence option to output results in a separate code block instead of inline. ### `numd run` flags and params ```nushell -> use numd -> numd run --help +use numd +numd run --help # => Run Nushell code blocks in a markdown file, output results back to the `.md`, and optionally to terminal # => # => Usage: @@ -66,13 +75,14 @@ Experienced nushell users can understand the logic better by looking at [example `numd` understands the following block options. Several comma-separated block options will be combined together. The block options should be in the [infostring](https://github.github.com/gfm/#info-string) of the opening code fence like the example: ` ```nushell try, new-instance ` ```nushell -> numd list-code-options --list -# => ╭─────long─────┬─short─┬───────────────────────────description───────────────────────────╮ -# => │ no-output │ O │ execute code without outputting results │ -# => │ no-run │ N │ do not execute code in block │ -# => │ try │ t │ execute block inside `try {}` for error handling │ -# => │ new-instance │ n │ execute block in new Nushell instance (useful with `try` block) │ -# => ╰─────long─────┴─short─┴───────────────────────────description───────────────────────────╯ +numd list-code-options --list +# => ╭──────long──────┬─short─┬───────────────────────────description────────────────────────────╮ +# => │ no-output │ O │ execute code without outputting results │ +# => │ no-run │ N │ do not execute code in block │ +# => │ try │ t │ execute block inside `try {}` for error handling │ +# => │ new-instance │ n │ execute block in new Nushell instance (useful with `try` block) │ +# => │ separate-block │ s │ output results in a separate code block instead of inline `# =>` │ +# => ╰──────long──────┴─short─┴───────────────────────────description────────────────────────────╯ ``` ### Stats of changes @@ -80,15 +90,15 @@ Experienced nushell users can understand the logic better by looking at [example By default, `numd` provides basic stats on changes made. ```nushell -> let path = [z_examples 1_simple_markdown simple_markdown_with_no_output.md] | path join -> numd run --no-save $path +let path = [z_examples 1_simple_markdown simple_markdown_with_no_output.md] | path join +numd run --no-save $path # => ╭──────────────────┬───────────────────────────────────╮ # => │ filename │ simple_markdown_with_no_output.md │ # => │ nushell_blocks │ 3 │ -# => │ levenshtein_dist │ 53 │ -# => │ diff_lines │ +9 (30.0%) │ -# => │ diff_words │ +6 (8.7%) │ -# => │ diff_chars │ +53 (12.1%) │ +# => │ levenshtein_dist │ 52 │ +# => │ diff_lines │ +8 (25.8%) │ +# => │ diff_words │ +6 (8.5%) │ +# => │ diff_chars │ +52 (11.6%) │ # => ╰──────────────────┴───────────────────────────────────╯ ``` @@ -109,18 +119,8 @@ numd run $path --echo --no-save --no-stats --prepend-code " $env.config.table.index_mode = 'never' $env.config.table.mode = 'basic_compact' " -``` - -Output: - -``` # => ```nushell # => [[a b c]; [1 2 3]] -# => ``` -# => -# => Output: -# => -# => ``` # => # => +---+---+---+ # => # => | a | b | c | # => # => | 1 | 2 | 3 | @@ -131,7 +131,7 @@ Output: ### `numd clear-outputs` ```nu -> numd clear-outputs --help +numd clear-outputs --help # => Remove numd execution outputs from the file # => # => Usage: @@ -159,7 +159,7 @@ Output: `numd` can use the `display_output` hook to write the current session prompts together with their output into a specified markdown file. There are corresponding commands `numd capture start` and `numd capture stop`. ```nushell -> numd capture start --help +numd capture start --help # => start capturing commands and their outputs into a file # => # => Usage: @@ -167,7 +167,7 @@ Output: # => # => Flags: # => -h, --help: Display the help message for this command -# => --separate: don't use `>` notation, create separate blocks for each pipeline +# => --separate-blocks: create separate code blocks for each pipeline instead of inline `# =>` output # => # => Parameters: # => file : (optional, default: 'numd_capture.md') @@ -180,7 +180,7 @@ Output: ``` ```nushell -> numd capture stop --help +numd capture stop --help # => stop capturing commands and their outputs # => # => Usage: @@ -199,11 +199,10 @@ Output: ### Some random familiar examples ```nushell -> ls z_examples | sort-by name | reject modified size +ls z_examples | sort-by name | reject modified size # => ╭──────────────────name───────────────────┬─type─╮ # => │ z_examples/1_simple_markdown │ dir │ # => │ z_examples/2_numd_commands_explanations │ dir │ -# => │ z_examples/3_book_types_of_data │ dir │ # => │ z_examples/4_book_working_with_lists │ dir │ # => │ z_examples/5_simple_nu_table │ dir │ # => │ z_examples/6_edge_cases │ dir │ @@ -213,13 +212,13 @@ Output: # => │ z_examples/9_other │ dir │ # => ╰──────────────────name───────────────────┴─type─╯ -> sys host | get boot_time -# => Fri Dec 5 01:08:37 2025 +sys host | get boot_time +# => Fri Dec 5 03:49:33 2025 -> 2 + 2 +2 + 2 # => 4 -> git tag | lines | sort -n | last +git tag | lines | sort -n | last # => 0.1.21 ``` @@ -230,12 +229,6 @@ Output: [z_examples 1_simple_markdown simple_markdown.md] | path join | numd run $in --echo --no-save - -# run examples in the `types_of_data.md` file, -# save intermed nushell script to `types_of_data.md_intermed_from_readme.nu` -[z_examples 3_book_types_of_data types_of_data.md] -| path join -| numd run $in --no-backup --save-intermed-script $'($in)_intermed_from_readme.nu' ``` ## Development and testing @@ -246,13 +239,13 @@ Testing of the `numd` module is done via `toolkit.nu`: ```nushell no-run # Run all tests (unit + integration) -> nu toolkit.nu testing +nu toolkit.nu testing # Run only unit tests (uses nutest framework) -> nu toolkit.nu testing-unit +nu toolkit.nu testing-unit # Run only integration tests (executes example markdown files) -> nu toolkit.nu testing-integration +nu toolkit.nu testing-integration ``` ### Unit tests @@ -264,7 +257,7 @@ Unit tests in `tests/` use the [nutest](https://github.com/vyadh/nutest) framewo Integration tests run all example files in `z_examples/` through numd and report changes via Levenshtein distance. Whatever changes are made in the module - it can be easily seen if they break anything (both by the Levenshtein distance metric or by `git diff` of the updated example files versus their initial versions). ```nushell no-run -> nu toolkit.nu testing-integration +nu toolkit.nu testing-integration # => ╭───────────────────────────────────────────────┬─────────────────┬───────────────────┬────────────┬──────────────┬─────╮ # => │ filename │ nushell_blocks │ levenshtein_dist │ diff_lines │ diff_words │ ... │ # => ├───────────────────────────────────────────────┼─────────────────┼───────────────────┼────────────┼──────────────┼─────┤ diff --git a/numd/commands.nu b/numd/commands.nu index 5feb1a3..2c5bcab 100644 --- a/numd/commands.nu +++ b/numd/commands.nu @@ -93,10 +93,7 @@ export def clear-outputs [ | group-by block_index | items {|block_index block_lines| $block_lines.line.0 - | where $it !~ '^# => ?' - | if ($in | where $it =~ '^>' | is-empty) { } else { - where $it =~ '^(>|#|```)' - } + | where $it !~ '^# => ?' # strip `# =>` output lines, preserve plain `#` comments | prepend (mark-code-block $block_index) } | flatten @@ -107,7 +104,6 @@ export def clear-outputs [ lines | update 0 { $'(char nl) # ($in)' } # keep infostring | drop - | str replace --all --regex '^>\s*' '' | str join (char nl) | str replace -r '\s*$' (char nl) } @@ -123,7 +119,7 @@ export def clear-outputs [ # start capturing commands and their outputs into a file export def --env 'capture start' [ file: path = 'numd_capture.md' - --separate # don't use `>` notation, create separate blocks for each pipeline + --separate-blocks # create separate code blocks for each pipeline instead of inline `# =>` output ]: nothing -> nothing { cprint $'numd commands capture has been started. Commands and their outputs of the current nushell instance @@ -134,9 +130,9 @@ export def --env 'capture start' [ $env.numd.status = 'running' $env.numd.path = ($file | path expand) - $env.numd.separate-blocks = $separate + $env.numd.separate-blocks = $separate_blocks - if not $separate { "```nushell\n" | save -a $env.numd.path } + if not $separate_blocks { "```nushell\n" | save -a $env.numd.path } $env.backup.hooks.display_output = ( $env.config.hooks?.display_output? @@ -159,7 +155,9 @@ export def --env 'capture start' [ $"```nushell\n($command)\n```\n```output-numd\n($in)\n```\n\n" | str replace --regex --all "[\n\r ]+```\n" "\n```\n" } else { - $"> ($command)\n($in)\n\n" + # inline output format: command followed by `# =>` prefixed output + let output_lines = $in | lines | each {$'# => ($in)'} | str join (char nl) + $"($command)\n($output_lines)\n\n" } | str replace --regex "\n{3,}$" "\n\n" | if ($in !~ 'numd capture') { @@ -340,13 +338,17 @@ export def create-execution-code [ } } else { } | if 'no-output' in $fence_options { } else { - if $whole_block { create-fence-output } else { } + # separate-block: output goes to a separate ```output-numd``` fence + # default: output is inline with `# =>` prefix + if 'separate-block' in $fence_options { create-fence-output } else { } | if (check-print-append $in) { create-indented-output | generate-print-statement } else { } } | $in + (char nl) + # Always print a blank line after each command group to preserve visual separation + | $in + "print ''" $highlighted_command + $code_execution } @@ -383,31 +385,35 @@ export def generate-intermediate-script []: table` output. export def execute-block-lines [ fence_options: list ]: list -> list { skip | drop # skip code fences - | if ($in | where $it =~ '^>' | is-empty) { - # find blocks with no `>` symbol to execute them entirely - str join (char nl) - | create-execution-code $fence_options --whole_block - | [$in] # quick fix so the `execute-block-lines` would always output lists. Should be refactored. - } else { - each { - # define what to do with each line of the current block one by one - if $in starts-with '>' { - # if a line starts with `>`, execute it - create-execution-code $fence_options - } else if $in starts-with '#' { - if $in !~ '# =>' { - # if a line starts with `#`, print it - create-highlight-command - } - } + | where not ($it =~ '^# =>') # strip existing `# =>` output lines (keep plain `#` comments) + | str join (char nl) + | split-by-blank-lines + | each {|group| + if ($group | str trim | is-empty) { + # preserve blank line separators + '' + } else if ($group | str trim | str starts-with '#') and ($group | str trim | lines | all {|line| $line =~ '^#'}) { + # pure comment group - just highlight it + $group | create-highlight-command + } else { + # executable command group + $group | create-execution-code $fence_options --whole_block } } } +# Split string by blank lines (double newlines) into command groups. +# Preserves multiline commands that don't have blank lines between them. +export def split-by-blank-lines []: string -> list { + split row "\n\n" + | each { str trim -c "\n" } +} + # Parse block indices from Nushell output lines and return a table with the original markdown line numbers. export def extract-block-index []: list -> table { let clean_lines = skip until {|x| $x =~ (mark-code-block) } @@ -527,6 +533,7 @@ export def list-code-options [ ["no-run" "N" "do not execute code in block"] ["try" "t" "execute block inside `try {}` for error handling"] ["new-instance" "n" "execute block in new Nushell instance (useful with `try` block)"] + ["separate-block" "s" "output results in a separate code block instead of inline `# =>`"] # ["picture-output" "p" "capture output as picture and place after block"] ] | if $list { } else { @@ -609,8 +616,7 @@ export def create-highlight-command []: string -> string { # Trim comments and extra whitespaces from code blocks for use in the generated script. export def remove-comments-plus []: string -> string { - str replace -r '^[>\s]+' '' # trim starting `>` - | str replace -r '[\s\n]+$' '' # trim newlines and spaces from the end of a line + str replace -r '[\s\n]+$' '' # trim newlines and spaces from the end of a line | str replace -r '\s+#.*$' '' # remove comments from the last line. Might spoil code blocks with the # symbol, used not for commenting } diff --git a/todo/20251210-171238-unquoted-notation.md b/todo/20251210-171238-unquoted-notation.md new file mode 100644 index 0000000..99020a1 --- /dev/null +++ b/todo/20251210-171238-unquoted-notation.md @@ -0,0 +1,83 @@ +--- +status: in-progress +created: 20251210-171238 +updated: 20251210-171238 +--- + +## Goal: Remove `>` Notation for Command Execution + +### Current Behavior (to be removed) +- Code blocks **with** `>` prefix: lines starting with `>` or `#` are filtered, executed one-by-one, results output immediately after each line +- Code blocks **without** `>` prefix: entire block executed as single unit, output appended after block + +### New Behavior +1. **Parse entire block** without `>` notation +2. **Group commands by double newlines** (blank lines delimit command groups) +3. **Output results** in `# =>` comment lines immediately after each command group +4. **`separate-block` option** in fence produces separate output blocks instead of inline comments +5. **On re-run**: `# =>` lines are stripped and regenerated; plain `#` comments are preserved + +### Example + +Input: +```nushell +ls | length + +ls +| length + +echo "hello" +``` + +Output after execution: +```nushell +ls | length +# => 5 + +ls +| length +# => 5 + +echo "hello" +# => hello +``` + +With `separate-block` fence option, output goes into a separate code fence instead of inline. + +## Functions to Modify in `numd/commands.nu` + +### Primary Changes + +| Function | Lines | Current Role | Change Required | +|----------|-------|--------------|-----------------| +| `execute-block-lines` | 386-409 | Detects `>` prefix, branches to line-by-line vs whole-block | Rewrite: split by blank lines, execute each group, insert `# =>` output | +| `remove-comments-plus` | 611-615 | Strips `>` prefix before execution | Remove `>` stripping logic | +| `clear-outputs` | 79-121 | Preserves `>` lines, removes `# =>` | Remove `>` preservation, keep `# =>` stripping | +| `capture start` | 124-172 | Generates `> command` format | Remove `>` notation generation | +| `create-indented-output` | 688-693 | Generates `# => ` prefix for output | Keep, but adjust calling context | + +### Supporting Changes + +| Function | Lines | Role | Change | +|----------|-------|------|--------| +| `create-execution-code` | ~360-385 | Wraps code for execution | May need adjustment for group-based execution | +| `decortate-original-code-blocks` | ~320-360 | Calls `execute-block-lines` | Update to pass groups | +| `create-highlight-command` | ~617-625 | Highlights comment lines | Review if still needed | + +### New Code Required + +- [ ] Function to split block content by blank lines into command groups +- [ ] Logic to execute each group and capture output +- [ ] Insert `# =>` lines after each group in the result + +## Implementation Tasks + +- [ ] Rewrite `execute-block-lines` for blank-line grouping +- [ ] Remove `>` stripping from `remove-comments-plus` +- [ ] Update `clear-outputs` to not preserve `>` lines +- [ ] Update `capture start` to remove `>` notation generation +- [ ] Add `separate-block` fence option support +- [ ] Update README.md documentation +- [ ] Update example files in `z_examples/` +- [ ] Add/update unit tests in `tests/test_commands.nu` +- [ ] Run integration tests and verify diff --git a/z_examples/1_simple_markdown/simple_markdown.md b/z_examples/1_simple_markdown/simple_markdown.md index b5ab3e9..15d606f 100644 --- a/z_examples/1_simple_markdown/simple_markdown.md +++ b/z_examples/1_simple_markdown/simple_markdown.md @@ -10,7 +10,7 @@ let $var1 = 'foo' ## Example 2 -```nu +```nu separate-block # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' ``` @@ -25,10 +25,10 @@ Output: ```nu # This block will output results inline -> whoami +whoami # => user -> 2 + 2 +2 + 2 # => 4 ``` diff --git a/z_examples/1_simple_markdown/simple_markdown.md_intermed.nu b/z_examples/1_simple_markdown/simple_markdown.md_intermed.nu index b7ec7e8..2fbf578 100644 --- a/z_examples/1_simple_markdown/simple_markdown.md_intermed.nu +++ b/z_examples/1_simple_markdown/simple_markdown.md_intermed.nu @@ -15,14 +15,12 @@ header_on_separator: true, abbreviated_row_count: 1000} ```nu" | print "let $var1 = 'foo'" | nu-highlight | print -"```\n```output-numd" | print - let $var1 = 'foo' - +print '' "```" | print "#code-block-marker-open-3 -```nu" | print +```nu separate-block" | print "# This block will produce some output in a separate block $var1 | path join 'baz' 'bar'" | nu-highlight | print @@ -30,20 +28,19 @@ $var1 | path join 'baz' 'bar'" | nu-highlight | print # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print "#code-block-marker-open-6 ```nu" | print -"# This block will output results inline" | nu-highlight | print - - -"> whoami" | nu-highlight | print +"# This block will output results inline +whoami" | nu-highlight | print +# This block will output results inline whoami | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 2 + 2" | nu-highlight | print +print '' +"2 + 2" | nu-highlight | print 2 + 2 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/1_simple_markdown/simple_markdown_with_no_output.md b/z_examples/1_simple_markdown/simple_markdown_with_no_output.md index b4f264e..1dafbea 100644 --- a/z_examples/1_simple_markdown/simple_markdown_with_no_output.md +++ b/z_examples/1_simple_markdown/simple_markdown_with_no_output.md @@ -10,7 +10,7 @@ let $var1 = 'foo' ## Example 2 -```nu +```nu separate-block # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' ``` @@ -19,8 +19,9 @@ $var1 | path join 'baz' 'bar' ```nu # This block will output results inline -> whoami -> 2 + 2 +whoami + +2 + 2 ``` ## Example 4 diff --git a/z_examples/2_numd_commands_explanations/numd_commands_explanations.md b/z_examples/2_numd_commands_explanations/numd_commands_explanations.md index 26fa2a8..d4476a5 100644 --- a/z_examples/2_numd_commands_explanations/numd_commands_explanations.md +++ b/z_examples/2_numd_commands_explanations/numd_commands_explanations.md @@ -5,7 +5,6 @@ In the code block below, we set settings and variables for executing this entire ```nu # This setting is for overriding the author's usual small number of `abbreviated_row_count`. $env.config.table.abbreviated_row_count = 100 - # The `$init_numd_pwd_const` constant points to the current working directory from where the `numd` command was initiated. # It is added by `numd` in every intermediate script to make it available in cases like below. # We use `path join` here to construct working paths for both Windows and Unix @@ -21,71 +20,66 @@ This command is used for parsing initial markdown to detect executable code bloc ```nu # Here we set the `$file` variable (which will be used in several commands throughout this script) to point to `z_examples/1_simple_markdown/simple_markdown.md`. let $file = $init_numd_pwd_const | path join z_examples 1_simple_markdown simple_markdown.md - let $md_orig = open -r $file | toggle-output-fences let $original_md_table = $md_orig | find-code-blocks -$original_md_table | table -e --width 120 -``` - -Output: -``` -# => ╭─block_index─┬────row_type────┬───────────────────────────────────line────────────────────────────────────┬──action───╮ -# => │ 0 │ text │ ╭───────────────────────────────────────────────────────────────────────╮ │ print-as- │ -# => │ │ │ │ # This is a simple markdown example │ │ it-is │ -# => │ │ │ │ │ │ │ -# => │ │ │ │ ## Example 1 │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ │ the block below will be executed as it is, but won't yield any output │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ ╰───────────────────────────────────────────────────────────────────────╯ │ │ -# => │ 1 │ ```nu │ ╭───────────────────╮ │ execute │ -# => │ │ │ │ ```nu │ │ │ -# => │ │ │ │ let $var1 = 'foo' │ │ │ -# => │ │ │ │ ``` │ │ │ -# => │ │ │ ╰───────────────────╯ │ │ -# => │ 2 │ text │ ╭──────────────╮ │ print-as- │ -# => │ │ │ │ │ │ it-is │ -# => │ │ │ │ ## Example 2 │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ ╰──────────────╯ │ │ -# => │ 3 │ ```nu │ ╭───────────────────────────────────────────────────────────╮ │ execute │ -# => │ │ │ │ ```nu │ │ │ -# => │ │ │ │ # This block will produce some output in a separate block │ │ │ -# => │ │ │ │ $var1 | path join 'baz' 'bar' │ │ │ -# => │ │ │ │ ``` │ │ │ -# => │ │ │ ╰───────────────────────────────────────────────────────────╯ │ │ -# => │ 4 │ ```output-numd │ ╭──────────────────╮ │ delete │ -# => │ │ │ │ ```output-numd │ │ │ -# => │ │ │ │ # => foo/baz/bar │ │ │ -# => │ │ │ │ ``` │ │ │ -# => │ │ │ ╰──────────────────╯ │ │ -# => │ 5 │ text │ ╭──────────────╮ │ print-as- │ -# => │ │ │ │ │ │ it-is │ -# => │ │ │ │ ## Example 3 │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ ╰──────────────╯ │ │ -# => │ 6 │ ```nu │ ╭─────────────────────────────────────────╮ │ execute │ -# => │ │ │ │ ```nu │ │ │ -# => │ │ │ │ # This block will output results inline │ │ │ -# => │ │ │ │ > whoami │ │ │ -# => │ │ │ │ # => user │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ │ > 2 + 2 │ │ │ -# => │ │ │ │ # => 4 │ │ │ -# => │ │ │ │ ``` │ │ │ -# => │ │ │ ╰─────────────────────────────────────────╯ │ │ -# => │ 7 │ text │ ╭──────────────╮ │ print-as- │ -# => │ │ │ │ │ │ it-is │ -# => │ │ │ │ ## Example 4 │ │ │ -# => │ │ │ │ │ │ │ -# => │ │ │ ╰──────────────╯ │ │ -# => │ 8 │ ``` │ ╭──────────────────────────────────────────────────────────────────────╮ │ print-as- │ -# => │ │ │ │ ``` │ │ it-is │ -# => │ │ │ │ # This block doesn't have a language identifier in the opening fence │ │ │ -# => │ │ │ │ ``` │ │ │ -# => │ │ │ ╰──────────────────────────────────────────────────────────────────────╯ │ │ -# => ╰─block_index─┴────row_type────┴───────────────────────────────────line────────────────────────────────────┴──action───╯ +$original_md_table | table -e --width 120 +# => ╭─block_index─┬───────row_type───────┬───────────────────────────────────line────────────────────────────────────┬─act─╮ +# => │ 0 │ text │ ╭───────────────────────────────────────────────────────────────────────╮ │ pri │ +# => │ │ │ │ # This is a simple markdown example │ │ nt- │ +# => │ │ │ │ │ │ as- │ +# => │ │ │ │ ## Example 1 │ │ it- │ +# => │ │ │ │ │ │ is │ +# => │ │ │ │ the block below will be executed as it is, but won't yield any output │ │ │ +# => │ │ │ │ │ │ │ +# => │ │ │ ╰───────────────────────────────────────────────────────────────────────╯ │ │ +# => │ 1 │ ```nu │ ╭───────────────────╮ │ exe │ +# => │ │ │ │ ```nu │ │ cut │ +# => │ │ │ │ let $var1 = 'foo' │ │ e │ +# => │ │ │ │ ``` │ │ │ +# => │ │ │ ╰───────────────────╯ │ │ +# => │ 2 │ text │ ╭──────────────╮ │ pri │ +# => │ │ │ │ │ │ nt- │ +# => │ │ │ │ ## Example 2 │ │ as- │ +# => │ │ │ │ │ │ it- │ +# => │ │ │ ╰──────────────╯ │ is │ +# => │ 3 │ ```nu separate-block │ ╭───────────────────────────────────────────────────────────╮ │ exe │ +# => │ │ │ │ ```nu separate-block │ │ cut │ +# => │ │ │ │ # This block will produce some output in a separate block │ │ e │ +# => │ │ │ │ $var1 | path join 'baz' 'bar' │ │ │ +# => │ │ │ │ ``` │ │ │ +# => │ │ │ ╰───────────────────────────────────────────────────────────╯ │ │ +# => │ 4 │ ```output-numd │ ╭──────────────────╮ │ del │ +# => │ │ │ │ ```output-numd │ │ ete │ +# => │ │ │ │ # => foo/baz/bar │ │ │ +# => │ │ │ │ ``` │ │ │ +# => │ │ │ ╰──────────────────╯ │ │ +# => │ 5 │ text │ ╭──────────────╮ │ pri │ +# => │ │ │ │ │ │ nt- │ +# => │ │ │ │ ## Example 3 │ │ as- │ +# => │ │ │ │ │ │ it- │ +# => │ │ │ ╰──────────────╯ │ is │ +# => │ 6 │ ```nu │ ╭─────────────────────────────────────────╮ │ exe │ +# => │ │ │ │ ```nu │ │ cut │ +# => │ │ │ │ # This block will output results inline │ │ e │ +# => │ │ │ │ whoami │ │ │ +# => │ │ │ │ # => user │ │ │ +# => │ │ │ │ │ │ │ +# => │ │ │ │ 2 + 2 │ │ │ +# => │ │ │ │ # => 4 │ │ │ +# => │ │ │ │ ``` │ │ │ +# => │ │ │ ╰─────────────────────────────────────────╯ │ │ +# => │ 7 │ text │ ╭──────────────╮ │ pri │ +# => │ │ │ │ │ │ nt- │ +# => │ │ │ │ ## Example 4 │ │ as- │ +# => │ │ │ │ │ │ it- │ +# => │ │ │ ╰──────────────╯ │ is │ +# => │ 8 │ ``` │ ╭──────────────────────────────────────────────────────────────────────╮ │ pri │ +# => │ │ │ │ ``` │ │ nt- │ +# => │ │ │ │ # This block doesn't have a language identifier in the opening fence │ │ as- │ +# => │ │ │ │ ``` │ │ it- │ +# => │ │ │ ╰──────────────────────────────────────────────────────────────────────╯ │ is │ +# => ╰─block_index─┴───────row_type───────┴───────────────────────────────────line────────────────────────────────────┴─act─╯ ``` ## generate-intermediate-script @@ -102,11 +96,6 @@ decortate-original-code-blocks $original_md_table | save -f $intermediate_script_path open $intermediate_script_path -``` - -Output: - -``` # => # this script was generated automatically using numd # => # https://github.com/nushell-prophet/numd # => @@ -116,14 +105,12 @@ Output: # => ```nu" | print # => "let $var1 = 'foo'" | nu-highlight | print # => -# => "```\n```output-numd" | print -# => # => let $var1 = 'foo' -# => +# => print '' # => "```" | print # => # => "#code-block-marker-open-3 -# => ```nu" | print +# => ```nu separate-block" | print # => "# This block will produce some output in a separate block # => $var1 | path join 'baz' 'bar'" | nu-highlight | print # => @@ -131,22 +118,21 @@ Output: # => # => # This block will produce some output in a separate block # => $var1 | path join 'baz' 'bar' | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' -# => +# => print '' # => "```" | print # => # => "#code-block-marker-open-6 # => ```nu" | print -# => "# This block will output results inline" | nu-highlight | print -# => -# => -# => "> whoami" | nu-highlight | print +# => "# This block will output results inline +# => whoami" | nu-highlight | print # => +# => # This block will output results inline # => whoami | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' -# => -# => "> 2 + 2" | nu-highlight | print +# => print '' +# => "2 + 2" | nu-highlight | print # => # => 2 + 2 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' -# => +# => print '' # => "```" | print ``` @@ -157,23 +143,17 @@ The `execute-intermediate-script` command runs and captures outputs of the execu ```nu # the flag `$no_fail_on_error` is set to false let $no_fail_on_error = false - let $nu_res_stdout_lines = execute-intermediate-script $intermediate_script_path $no_fail_on_error false rm $intermediate_script_path -$nu_res_stdout_lines -``` - -Output: -``` +$nu_res_stdout_lines # => #code-block-marker-open-1 # => ```nu # => let $var1 = 'foo' -# => ``` -# => ```output-numd +# => # => ``` # => #code-block-marker-open-3 -# => ```nu +# => ```nu separate-block # => # This block will produce some output in a separate block # => $var1 | path join 'baz' 'bar' # => ``` @@ -181,18 +161,21 @@ Output: # => # => foo/baz/bar # => # => +# => # => ``` # => #code-block-marker-open-6 # => ```nu # => # This block will output results inline -# => > whoami +# => whoami # => # => user # => # => -# => > 2 + 2 +# => +# => 2 + 2 # => # => 4 # => # => +# => # => ``` ``` @@ -202,17 +185,13 @@ let $md_res = $nu_res_stdout_lines | clean-markdown $md_res -``` - -Output: - -``` # => #code-block-marker-open-1 # => ```nu # => let $var1 = 'foo' +# => # => ``` # => #code-block-marker-open-3 -# => ```nu +# => ```nu separate-block # => # This block will produce some output in a separate block # => $var1 | path join 'baz' 'bar' # => ``` @@ -223,10 +202,10 @@ Output: # => #code-block-marker-open-6 # => ```nu # => # This block will output results inline -# => > whoami +# => whoami # => # => user # => -# => > 2 + 2 +# => 2 + 2 # => # => 4 # => # => ``` @@ -238,17 +217,12 @@ The `compute-change-stats` command displays stats on the changes made. ```nu compute-change-stats $file $md_orig $md_res -``` - -Output: - -``` # => ╭──────────────────┬────────────────────╮ # => │ filename │ simple_markdown.md │ # => │ nushell_blocks │ 3 │ -# => │ levenshtein_dist │ 247 │ -# => │ diff_lines │ -13 (-36.1%) │ -# => │ diff_words │ -24 (-31.6%) │ -# => │ diff_chars │ -164 (-33.4%) │ +# => │ levenshtein_dist │ 248 │ +# => │ diff_lines │ -12 (-33.3%) │ +# => │ diff_words │ -24 (-30.8%) │ +# => │ diff_chars │ -163 (-32.5%) │ # => ╰──────────────────┴────────────────────╯ ``` diff --git a/z_examples/2_numd_commands_explanations/numd_commands_explanations.md_intermed.nu b/z_examples/2_numd_commands_explanations/numd_commands_explanations.md_intermed.nu index ebd037e..f91eb03 100644 --- a/z_examples/2_numd_commands_explanations/numd_commands_explanations.md_intermed.nu +++ b/z_examples/2_numd_commands_explanations/numd_commands_explanations.md_intermed.nu @@ -15,114 +15,100 @@ header_on_separator: true, abbreviated_row_count: 1000} ```nu" | print "# This setting is for overriding the author's usual small number of `abbreviated_row_count`. $env.config.table.abbreviated_row_count = 100 - # The `$init_numd_pwd_const` constant points to the current working directory from where the `numd` command was initiated. # It is added by `numd` in every intermediate script to make it available in cases like below. # We use `path join` here to construct working paths for both Windows and Unix use ($init_numd_pwd_const | path join numd commands.nu) *" | nu-highlight | print -"```\n```output-numd" | print - # This setting is for overriding the author's usual small number of `abbreviated_row_count`. $env.config.table.abbreviated_row_count = 100 - # The `$init_numd_pwd_const` constant points to the current working directory from where the `numd` command was initiated. # It is added by `numd` in every intermediate script to make it available in cases like below. # We use `path join` here to construct working paths for both Windows and Unix use ($init_numd_pwd_const | path join numd commands.nu) * - +print '' "```" | print "#code-block-marker-open-3 ```nu" | print "# Here we set the `$file` variable (which will be used in several commands throughout this script) to point to `z_examples/1_simple_markdown/simple_markdown.md`. let $file = $init_numd_pwd_const | path join z_examples 1_simple_markdown simple_markdown.md - let $md_orig = open -r $file | toggle-output-fences -let $original_md_table = $md_orig | find-code-blocks -$original_md_table | table -e --width 120" | nu-highlight | print - -"```\n```output-numd" | print +let $original_md_table = $md_orig | find-code-blocks" | nu-highlight | print # Here we set the `$file` variable (which will be used in several commands throughout this script) to point to `z_examples/1_simple_markdown/simple_markdown.md`. let $file = $init_numd_pwd_const | path join z_examples 1_simple_markdown simple_markdown.md - let $md_orig = open -r $file | toggle-output-fences let $original_md_table = $md_orig | find-code-blocks -$original_md_table | table -e --width 120 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"$original_md_table | table -e --width 120" | nu-highlight | print +$original_md_table | table -e --width 120 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' "```" | print -"#code-block-marker-open-6 +"#code-block-marker-open-5 ```nu" | print "# Here we emulate that the `$intermed_script_path` options is not set let $intermediate_script_path = $file - | modify-path --prefix $'numd-temp-(generate-timestamp)' --suffix '.nu' - -decortate-original-code-blocks $original_md_table -| generate-intermediate-script -| save -f $intermediate_script_path - -open $intermediate_script_path" | nu-highlight | print - -"```\n```output-numd" | print + | modify-path --prefix $'numd-temp-(generate-timestamp)' --suffix '.nu'" | nu-highlight | print # Here we emulate that the `$intermed_script_path` options is not set let $intermediate_script_path = $file | modify-path --prefix $'numd-temp-(generate-timestamp)' --suffix '.nu' +print '' +"decortate-original-code-blocks $original_md_table +| generate-intermediate-script +| save -f $intermediate_script_path" | nu-highlight | print decortate-original-code-blocks $original_md_table | generate-intermediate-script -| save -f $intermediate_script_path +| save -f $intermediate_script_path | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"open $intermediate_script_path" | nu-highlight | print open $intermediate_script_path | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-9 +"#code-block-marker-open-7 ```nu" | print "# the flag `$no_fail_on_error` is set to false let $no_fail_on_error = false - let $nu_res_stdout_lines = execute-intermediate-script $intermediate_script_path $no_fail_on_error false -rm $intermediate_script_path -$nu_res_stdout_lines" | nu-highlight | print - -"```\n```output-numd" | print +rm $intermediate_script_path" | nu-highlight | print # the flag `$no_fail_on_error` is set to false let $no_fail_on_error = false - let $nu_res_stdout_lines = execute-intermediate-script $intermediate_script_path $no_fail_on_error false -rm $intermediate_script_path -$nu_res_stdout_lines | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +rm $intermediate_script_path | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"$nu_res_stdout_lines" | nu-highlight | print +$nu_res_stdout_lines | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' "```" | print -"#code-block-marker-open-12 +"#code-block-marker-open-9 ```nu" | print "let $md_res = $nu_res_stdout_lines | str join (char nl) - | clean-markdown - -$md_res" | nu-highlight | print - -"```\n```output-numd" | print + | clean-markdown" | nu-highlight | print let $md_res = $nu_res_stdout_lines | str join (char nl) | clean-markdown +print '' +"$md_res" | nu-highlight | print $md_res | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-15 +"#code-block-marker-open-11 ```nu" | print "compute-change-stats $file $md_orig $md_res" | nu-highlight | print -"```\n```output-numd" | print - compute-change-stats $file $md_orig $md_res | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/3_book_types_of_data/types_of_data.md b/z_examples/3_book_types_of_data/types_of_data.md deleted file mode 100644 index 65b41d3..0000000 --- a/z_examples/3_book_types_of_data/types_of_data.md +++ /dev/null @@ -1,536 +0,0 @@ -# Types of Data - -Traditionally, Unix shell commands have communicated with each other using strings of text: one command would write text to standard output (often abbreviated 'stdout') and the other would read text from standard input (or 'stdin'), allowing the two commands to communicate. - -Nu embraces this approach, and expands it to include other types of data, in addition to strings. - -Like many programming languages, Nu models data using a set of simple, and structured data types. Simple data types include integers, floats, strings, booleans, dates. There are also special types for filesizes and time durations. - -The [`describe`](/commands/docs/describe.md) command returns the type of a data value: - -```nushell -> 42 | describe -# => int -``` - -## Types at a glance - -| Type | Example | -| ----------------- | --------------------------------------------------------------------- | -| Integers | `-65535` | -| Decimals (floats) | `9.9999`, `Infinity` | -| Strings | "hole 18", 'hole 18', \`hole 18\`, hole18 | -| Booleans | `true` | -| Dates | `2000-01-01` | -| Durations | `2min + 12sec` | -| File sizes | `64mb` | -| Ranges | `0..4`, `0..<5`, `0..`, `..4` | -| Binary | `0x[FE FF]` | -| Lists | `[0 1 'two' 3]` | -| Records | `{name:"Nushell", lang: "Rust"}` | -| Tables | `[{x:12, y:15}, {x:8, y:9}]`, `[[x, y]; [12, 15], [8, 9]]` | -| Closures | `{\|e\| $e + 1 \| into string }`, `{ $in.name.0 \| path exists }` | -| Blocks | `if true { print "hello!" }`, `loop { print "press ctrl-c to exit" }` | -| Null | `null` | - -## Integers - -Examples of integers (i.e. "round numbers") include 1, 0, -5, and 100. -You can parse a string into an integer with the [`into int`](/commands/docs/into_int.md) command - -```nushell -> "-5" | into int -# => -5 -``` - -## Decimals (floats) - -Decimal numbers are numbers with some fractional component. Examples include 1.5, 2.0, and 15.333. -You can cast a string into a Float with the [`into float`](/commands/docs/into_float.md) command - -```nushell -> "1.2" | into float -# => 1.2 -``` - -## Strings - -A string of characters that represents text. There are a few ways these can be constructed: - -- Double quotes - - `"Line1\nLine2\n"` -- Single quotes - `'She said "Nushell is the future".'` -- Dynamic string interpolation - - `$"6 x 7 = (6 * 7)"` - - `ls | each { |it| $"($it.name) is ($it.size)" }` -- Bare strings - - `print hello` - - `[foo bar baz]` - -See [Working with strings](working_with_strings.md) and [Handling Strings](https://www.nushell.sh/book/loading_data.html#handling-strings) for details. - -## Booleans - -There are just two boolean values: `true` and `false`. Rather than writing the values directly, they often result from a comparison: - -```nushell -> let mybool = 2 > 1 -> $mybool -# => true - -> let mybool = ($nu.home-path | path exists) -> $mybool -# => true -``` - -## Dates - -Dates and times are held together in the Date value type. Date values used by the system are timezone-aware, and by default use the UTC timezone. - -Dates are in three forms, based on the RFC 3339 standard: - -- A date: - - `2022-02-02` -- A date and time (in GMT): - - `2022-02-02T14:30:00` -- A date and time with timezone: - - `2022-02-02T14:30:00+05:00` - -## Durations - -Durations represent a length of time. This chart shows all durations currently supported: - -| Duration | Length | -| -------- | --------------- | -| `1ns` | one nanosecond | -| `1us` | one microsecond | -| `1ms` | one millisecond | -| `1sec` | one second | -| `1min` | one minute | -| `1hr` | one hour | -| `1day` | one day | -| `1wk` | one week | - -You can make fractional durations: - -```nushell -> 3.14day -# => 3day 3hr 21min 36sec -``` - -And you can do calculations with durations: - -```nushell -> 30day / 1sec # How many seconds in 30 days? -# => 2592000 -``` - -## File sizes - -Nushell also has a special type for file sizes. Examples include `100b`, `15kb`, and `100mb`. - -The full list of filesize units are: - -- `b`: bytes -- `kb`: kilobytes (aka 1000 bytes) -- `mb`: megabytes -- `gb`: gigabytes -- `tb`: terabytes -- `pb`: petabytes -- `eb`: exabytes -- `kib`: kibibytes (aka 1024 bytes) -- `mib`: mebibytes -- `gib`: gibibytes -- `tib`: tebibytes -- `pib`: pebibytes -- `eib`: exbibytes - -As with durations, you can make fractional file sizes, and do calculations: - -```nushell -> 1Gb / 1b -# => 1000000000 - -> 1Gib / 1b -# => 1073741824 - -> (1Gib / 1b) == 2 ** 30 -# => true -``` - -## Ranges - -A range is a way of expressing a sequence of integer or float values from start to finish. They take the form \..\. For example, the range `1..3` means the numbers 1, 2, and 3. - -::: tip - -You can also easily create lists of characters with a form similar to ranges with the command [`seq char`](/commands/docs/seq_char.html) as well as with dates using the [`seq date`](/commands/docs/seq_date.html) command. - -::: - -### Specifying the step - -You can specify the step of a range with the form \..\..\, where the step between values in the range is the distance between the \ and \ values, which numerically is \ - \. For example, the range `2..5..11` means the numbers 2, 5, 8, and 11 because the step is \ - \ = 5 - 2 = 3. The third value is 5 + 3 = 8 and the fourth value is 8 + 3 = 11. - -[`seq`](/commands/docs/seq.md) can also create sequences of numbers, and provides an alternate way of specifying the step with three parameters. It's called with `seq $start $step $end` where the step amount is the second parameter rather than being the second parameter minus the first parameter. So `2..5..9` would be equivalent to `seq 2 3 9`. - -### Inclusive and non-inclusive ranges - -Ranges are inclusive by default, meaning that the ending value is counted as part of the range. The range `1..3` includes the number `3` as the last value in the range. - -Sometimes, you may want a range that is limited by a number but doesn't use that number in the output. For this, you can use `..<` instead of `..`. For example, `1..<5` is the numbers 1, 2, 3, and 4. - -### Open-ended ranges - -Ranges can also be open-ended. You can remove the start or the end of the range to make it open-ended. - -Let's say you wanted to start counting at 3, but you didn't have a specific end in mind. You could use the range `3..` to represent this. When you use a range that's open-ended on the right side, remember that this will continue counting for as long as possible, which could be a very long time! You'll often want to use open-ended ranges with commands like [`take`](/commands/docs/take.md), so you can take the number of elements you want from the range. - -You can also make the start of the range open. In this case, Nushell will start counting with `0`. For example, the range `..2` is the numbers 0, 1, and 2. - -::: warning - -Watch out for displaying open-ended ranges like just entering `3..` into the command line. It will keep printing out numbers very quickly until you stop it with something like Ctr + c. - -::: - -## Binary data - -Binary data, like the data from an image file, is a group of raw bytes. - -You can write binary as a literal using any of the `0x[...]`, `0b[...]`, or `0o[...]` forms: - -```nushell -> 0x[1F FF] # Hexadecimal -# => Length: 2 (0x2) bytes | printable whitespace ascii_other non_ascii -# => 00000000: 1f ff •× - -> 0b[1 1010] # Binary -# => Length: 1 (0x1) bytes | printable whitespace ascii_other non_ascii -# => 00000000: 1a • - -> 0o[377] # Octal -# => Length: 1 (0x1) bytes | printable whitespace ascii_other non_ascii -# => 00000000: ff × -``` - -Incomplete bytes will be left-padded with zeros. - -## Structured data - -Structured data builds from the simple data. For example, instead of a single integer, structured data gives us a way to represent multiple integers in the same value. Here's a list of the currently supported structured data types: records, lists and tables. - -## Records - -Records hold key-value pairs, which associate string keys with various data values. Record syntax is very similar to objects in JSON. However, commas are _not_ required to separate values if Nushell can easily distinguish them! - -```nushell -> {name: sam rank: 10} -# => ╭──────┬─────╮ -# => │ name │ sam │ -# => │ rank │ 10 │ -# => ╰──────┴─────╯ -``` - -As these can sometimes have many fields, a record is printed up-down rather than left-right. - -:::tip -A record is identical to a single row of a table (see below). You can think of a record as essentially being a "one-row table", with each of its keys as a column (although a true one-row table is something distinct from a record). - -This means that any command that operates on a table's rows _also_ operates on records. For instance, [`insert`](/commands/docs/insert.md), which adds data to each of a table's rows, can be used with records: - -```nushell -> {x:3 y:1} | insert z 0 -# => ╭───┬───╮ -# => │ x │ 3 │ -# => │ y │ 1 │ -# => │ z │ 0 │ -# => ╰───┴───╯ -``` - -::: - -You can iterate over records by first transposing it into a table: - -```nushell -> {name: sam, rank: 10} | transpose key value -# => ╭─#─┬─key──┬─value─╮ -# => │ 0 │ name │ sam │ -# => │ 1 │ rank │ 10 │ -# => ╰─#─┴─key──┴─value─╯ -``` - -Accessing records' data is done by placing a `.` before a string, which is usually a bare string: - -```nushell -> {x:12 y:4}.x -# => 12 -``` - -However, if a record has a key name that can't be expressed as a bare string, or resembles an integer (see lists, below), you'll need to use more explicit string syntax, like so: - -```nushell -> {"1":true " ":false}." " -# => false -``` - -To make a copy of a record with new fields, you can use the [spread operator](/book/operators#spread-operator) (`...`): - -```nushell -> let data = { name: alice, age: 50 } -> { ...$data, hobby: cricket } -# => ╭───────┬─────────╮ -# => │ name │ alice │ -# => │ age │ 50 │ -# => │ hobby │ cricket │ -# => ╰───────┴─────────╯ -``` - -## Lists - -Lists are ordered sequences of data values. List syntax is very similar to arrays in JSON. However, commas are _not_ required to separate values if Nushell can easily distinguish them! - -```nushell -> [sam fred george] -# => ╭───┬────────╮ -# => │ 0 │ sam │ -# => │ 1 │ fred │ -# => │ 2 │ george │ -# => ╰───┴────────╯ -``` - -:::tip -Lists are equivalent to the individual columns of tables. You can think of a list as essentially being a "one-column table" (with no column name). Thus, any command which operates on a column _also_ operates on a list. For instance, [`where`](/commands/docs/where.md) can be used with lists: - -```nushell -> [bell book candle] | where ($it =~ 'b') -# => ╭───┬──────╮ -# => │ 0 │ bell │ -# => │ 1 │ book │ -# => ╰───┴──────╯ -``` - -::: - -Accessing lists' data is done by placing a `.` before a bare integer: - -```nushell -> [a b c].1 -# => b -``` - -To get a sub-list from a list, you can use the [`range`](/commands/docs/range.md) command: - -```nushell -> [a b c d e f] | slice 1..3 -# => ╭───┬───╮ -# => │ 0 │ b │ -# => │ 1 │ c │ -# => │ 2 │ d │ -# => ╰───┴───╯ -``` - -To append one or more lists together, optionally with values interspersed in between, you can use the -[spread operator](/book/operators#spread-operator) (`...`): - -```nushell -> let x = [1 2] -> [...$x 3 ...(4..7 | take 2)] -# => ╭───┬───╮ -# => │ 0 │ 1 │ -# => │ 1 │ 2 │ -# => │ 2 │ 3 │ -# => │ 3 │ 4 │ -# => │ 4 │ 5 │ -# => ╰───┴───╯ -``` - -## Tables - -The table is a core data structure in Nushell. As you run commands, you'll see that many of them return tables as output. A table has both rows and columns. - -We can create our own tables similarly to how we create a list. Because tables also contain columns and not just values, we pass in the name of the column values: - -```nushell -> [[column1, column2]; [Value1, Value2] [Value3, Value4]] -# => ╭─#─┬─column1─┬─column2─╮ -# => │ 0 │ Value1 │ Value2 │ -# => │ 1 │ Value3 │ Value4 │ -# => ╰─#─┴─column1─┴─column2─╯ -``` - -You can also create a table as a list of records, JSON-style: - -```nushell -> [{name: sam, rank: 10}, {name: bob, rank: 7}] -# => ╭─#─┬─name─┬─rank─╮ -# => │ 0 │ sam │ 10 │ -# => │ 1 │ bob │ 7 │ -# => ╰─#─┴─name─┴─rank─╯ -``` - -:::tip -Internally, tables are simply **lists of records**. This means that any command which extracts or isolates a specific row of a table will produce a record. For example, `get 0`, when used on a list, extracts the first value. But when used on a table (a list of records), it extracts a record: - -```nushell -> [{x:12, y:5}, {x:3, y:6}] | get 0 -# => ╭───┬────╮ -# => │ x │ 12 │ -# => │ y │ 5 │ -# => ╰───┴────╯ -``` - -This is true regardless of which table syntax you use: - -```nushell -> [[x,y];[12,5],[3,6]] | get 0 -# => ╭───┬────╮ -# => │ x │ 12 │ -# => │ y │ 5 │ -# => ╰───┴────╯ -``` - -::: - -### Cell Paths - -You can combine list and record data access syntax to navigate tables. When used on tables, these access chains are called "cell paths". - -You can access individual rows by number to obtain records: - -@[code](@snippets/types_of_data/cell-paths.sh) - -Moreover, you can also access entire columns of a table by name, to obtain lists: - -```nushell -> [{x:12 y:5} {x:4 y:7} {x:2 y:2}].x -# => ╭───┬────╮ -# => │ 0 │ 12 │ -# => │ 1 │ 4 │ -# => │ 2 │ 2 │ -# => ╰───┴────╯ -``` - -Of course, these resulting lists don't have the column names of the table. To remove columns from a table while leaving it as a table, you'll commonly use the [`select`](/commands/docs/select.md) command with column names: - -```nushell -> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z -# => ╭─#─┬─y─┬─z─╮ -# => │ 0 │ 5 │ 1 │ -# => │ 1 │ 7 │ 3 │ -# => │ 2 │ 2 │ 0 │ -# => ╰─#─┴─y─┴─z─╯ -``` - -To remove rows from a table, you'll commonly use the [`select`](/commands/docs/select.md) command with row numbers, as you would with a list: - -```nushell -> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2 -# => ╭─#─┬─x─┬─y─┬─z─╮ -# => │ 0 │ 4 │ 7 │ 3 │ -# => │ 1 │ 2 │ 2 │ 0 │ -# => ╰─#─┴─x─┴─y─┴─z─╯ -``` - -#### Optional cell paths - -By default, cell path access will fail if it can't access the requested row or column. To suppress these errors, you can add `?` to a cell path member to mark it as _optional_: - -```nushell -> [{foo: 123}, {}].foo? -# => ╭───┬─────╮ -# => │ 0 │ 123 │ -# => │ 1 │ │ -# => ╰───┴─────╯ -``` - -When using optional cell path members, missing data is replaced with `null`. - -## Closures - -Closures are anonymous functions that can be passed a value through parameters and _close over_ (i.e. use) a variable outside their scope. - -For example, in the command `each { |it| print $it }` the closure is the portion contained in curly braces, `{ |it| print $it }`. -Closure parameters are specified between a pair of pipe symbols (for example, `|it|`) if necessary. -You can also use a pipeline input as `$in` in most closures instead of providing an explicit parameter: `each { print $in }` - -Closures itself can be bound to a named variable and passed as a parameter. -To call a closure directly in your code use the [`do`](/commands/docs/do.md) command. - -```nushell -# Assign a closure to a variable -let greet = { |name| print $"Hello ($name)"} -do $greet "Julian" -``` - -Output: - -``` -Hello Julian -``` - -Closures are a useful way to represent code that can be executed on each row of data. -It is idiomatic to use `$it` as a parameter name in [`each`](/commands/docs/each.md) blocks, but not required; -`each { |x| print $x }` works the same way as `each { |it| print $it }`. - -## Blocks - -Blocks don't close over variables, don't have parameters, and can't be passed as a value. -However, unlike closures, blocks can access mutable variable in the parent closure. -For example, mutating a variable inside the block used in an [`if`](/commands/docs/if.md) call is valid: - -```nushell -mut x = 1 -if true { - $x += 1000 -} -print $x -``` - -Output: - -``` -1001 -``` - -## Null - -Finally, there is `null` which is the language's "nothing" value, similar to JSON's "null". Whenever Nushell would print the `null` value (outside of a string or data structure), it prints nothing instead. Hence, most of Nushell's file system commands (like [`save`](/commands/docs/save.md) or [`cd`](/commands/docs/cd.md)) produce `null`. - -You can place `null` at the end of a pipeline to replace the pipeline's output with it, and thus print nothing: - -```nushell no-run -git checkout featurebranch | null -``` - -:::warning - -`null` is not the same as the absence of a value! It is possible for a table to be produced that has holes in some of its rows. Attempting to access this value will not produce `null`, but instead cause an error: - -```nushell try,new-instance -> [{a:1 b:2} {b:1}] -# => ╭───┬────┬───╮ -# => │ # │ a │ b │ -# => ├───┼────┼───┤ -# => │ 0 │ 1 │ 2 │ -# => │ 1 │ ❎ │ 1 │ -# => ╰───┴────┴───╯ - -> [{a:1 b:2} {b:1}].1.a -# => Error: nu::shell::column_not_found -# => -# => x Cannot find column 'a' -# => ,-[source:1:12] -# => 1 | [{a:1 b:2} {b:1}].1.a -# => : ^^|^^ | -# => : | `-- cannot find column 'a' -# => : `-- value originates here -# => `---- -# => -``` - -If you would prefer this to return `null`, mark the cell path member as _optional_ like `.1.a?`. - -The absence of a value is (as of Nushell 0.71) printed as the ❎ emoji in interactive output. -::: diff --git a/z_examples/3_book_types_of_data/types_of_data.md_intermed.nu b/z_examples/3_book_types_of_data/types_of_data.md_intermed.nu deleted file mode 100644 index e455c60..0000000 --- a/z_examples/3_book_types_of_data/types_of_data.md_intermed.nu +++ /dev/null @@ -1,308 +0,0 @@ -# this script was generated automatically using numd -# https://github.com/nushell-prophet/numd - -const init_numd_pwd_const = '/Users/user/git/numd' - -# numd config loaded from `numd_config_example1.yaml` - -$env.config.footer_mode = 'always'; -$env.config.table = {mode: rounded, index_mode: never, -show_empty: false, padding: {left: 1, right: 1}, -trim: {methodology: truncating, wrapping_try_keep_words: false, truncating_suffix: ...}, -header_on_separator: true, abbreviated_row_count: 1000} - -"#code-block-marker-open-1 -```nushell" | print -"> 42 | describe" | nu-highlight | print - -42 | describe | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-3 -```nushell" | print -"> \"-5\" | into int" | nu-highlight | print - -"-5" | into int | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-5 -```nushell" | print -"> \"1.2\" | into float" | nu-highlight | print - -"1.2" | into float | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-7 -```nushell" | print -"> let mybool = 2 > 1" | nu-highlight | print - -let mybool = 2 > 1 - -"> $mybool" | nu-highlight | print - -$mybool | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> let mybool = ($nu.home-path | path exists)" | nu-highlight | print - -let mybool = ($nu.home-path | path exists) - -"> $mybool" | nu-highlight | print - -$mybool | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-9 -```nushell" | print -"> 3.14day" | nu-highlight | print - -3.14day | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-11 -```nushell" | print -"> 30day / 1sec # How many seconds in 30 days?" | nu-highlight | print - -30day / 1sec | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-13 -```nushell" | print -"> 1Gb / 1b" | nu-highlight | print - -1Gb / 1b | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 1Gib / 1b" | nu-highlight | print - -1Gib / 1b | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> (1Gib / 1b) == 2 ** 30" | nu-highlight | print - -(1Gib / 1b) == 2 ** 30 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-15 -```nushell" | print -"> 0x[1F FF] # Hexadecimal" | nu-highlight | print - -0x[1F FF] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 0b[1 1010] # Binary" | nu-highlight | print - -0b[1 1010] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 0o[377] # Octal" | nu-highlight | print - -0o[377] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-17 -```nushell" | print -"> {name: sam rank: 10}" | nu-highlight | print - -{name: sam rank: 10} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-19 -```nushell" | print -"> {x:3 y:1} | insert z 0" | nu-highlight | print - -{x:3 y:1} | insert z 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-21 -```nushell" | print -"> {name: sam, rank: 10} | transpose key value" | nu-highlight | print - -{name: sam, rank: 10} | transpose key value | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-23 -```nushell" | print -"> {x:12 y:4}.x" | nu-highlight | print - -{x:12 y:4}.x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-25 -```nushell" | print -"> {\"1\":true \" \":false}.\" \"" | nu-highlight | print - -{"1":true " ":false}." " | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-27 -```nushell" | print -"> let data = { name: alice, age: 50 }" | nu-highlight | print - -let data = { name: alice, age: 50 } - -"> { ...$data, hobby: cricket }" | nu-highlight | print - -{ ...$data, hobby: cricket } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-29 -```nushell" | print -"> [sam fred george]" | nu-highlight | print - -[sam fred george] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-31 -```nushell" | print -"> [bell book candle] | where ($it =~ 'b')" | nu-highlight | print - -[bell book candle] | where ($it =~ 'b') | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-33 -```nushell" | print -"> [a b c].1" | nu-highlight | print - -[a b c].1 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-35 -```nushell" | print -"> [a b c d e f] | slice 1..3" | nu-highlight | print - -[a b c d e f] | slice 1..3 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-37 -```nushell" | print -"> let x = [1 2]" | nu-highlight | print - -let x = [1 2] - -"> [...$x 3 ...(4..7 | take 2)]" | nu-highlight | print - -[...$x 3 ...(4..7 | take 2)] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-39 -```nushell" | print -"> [[column1, column2]; [Value1, Value2] [Value3, Value4]]" | nu-highlight | print - -[[column1, column2]; [Value1, Value2] [Value3, Value4]] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-41 -```nushell" | print -"> [{name: sam, rank: 10}, {name: bob, rank: 7}]" | nu-highlight | print - -[{name: sam, rank: 10}, {name: bob, rank: 7}] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-43 -```nushell" | print -"> [{x:12, y:5}, {x:3, y:6}] | get 0" | nu-highlight | print - -[{x:12, y:5}, {x:3, y:6}] | get 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-45 -```nushell" | print -"> [[x,y];[12,5],[3,6]] | get 0" | nu-highlight | print - -[[x,y];[12,5],[3,6]] | get 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-47 -```nushell" | print -"> [{x:12 y:5} {x:4 y:7} {x:2 y:2}].x" | nu-highlight | print - -[{x:12 y:5} {x:4 y:7} {x:2 y:2}].x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-49 -```nushell" | print -"> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z" | nu-highlight | print - -[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-51 -```nushell" | print -"> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2" | nu-highlight | print - -[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-53 -```nushell" | print -"> [{foo: 123}, {}].foo?" | nu-highlight | print - -[{foo: 123}, {}].foo? | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-55 -```nushell" | print -"# Assign a closure to a variable -let greet = { |name| print $\"Hello ($name)\"} -do $greet \"Julian\"" | nu-highlight | print - -"```\n```output-numd" | print - -# Assign a closure to a variable -let greet = { |name| print $"Hello ($name)"} -do $greet "Julian" | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-58 -```nushell" | print -"mut x = 1 -if true { - $x += 1000 -} -print $x" | nu-highlight | print - -"```\n```output-numd" | print - -mut x = 1 -if true { - $x += 1000 -} -print $x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-63 -```nushell try,new-instance" | print -"> [{a:1 b:2} {b:1}]" | nu-highlight | print - -/opt/homebrew/bin/nu -c "[{a:1 b:2} {b:1}]" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> [{a:1 b:2} {b:1}].1.a" | nu-highlight | print - -/opt/homebrew/bin/nu -c "[{a:1 b:2} {b:1}].1.a" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print diff --git a/z_examples/3_book_types_of_data/types_of_data.md_intermed_from_readme.nu b/z_examples/3_book_types_of_data/types_of_data.md_intermed_from_readme.nu deleted file mode 100644 index 33d5816..0000000 --- a/z_examples/3_book_types_of_data/types_of_data.md_intermed_from_readme.nu +++ /dev/null @@ -1,300 +0,0 @@ -# this script was generated automatically using numd -# https://github.com/nushell-prophet/numd - -const init_numd_pwd_const = '/Users/user/git/numd' - -"#code-block-marker-open-1 -```nushell" | print -"> 42 | describe" | nu-highlight | print - -42 | describe | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-3 -```nushell" | print -"> \"-5\" | into int" | nu-highlight | print - -"-5" | into int | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-5 -```nushell" | print -"> \"1.2\" | into float" | nu-highlight | print - -"1.2" | into float | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-7 -```nushell" | print -"> let mybool = 2 > 1" | nu-highlight | print - -let mybool = 2 > 1 - -"> $mybool" | nu-highlight | print - -$mybool | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> let mybool = ($nu.home-path | path exists)" | nu-highlight | print - -let mybool = ($nu.home-path | path exists) - -"> $mybool" | nu-highlight | print - -$mybool | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-9 -```nushell" | print -"> 3.14day" | nu-highlight | print - -3.14day | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-11 -```nushell" | print -"> 30day / 1sec # How many seconds in 30 days?" | nu-highlight | print - -30day / 1sec | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-13 -```nushell" | print -"> 1Gb / 1b" | nu-highlight | print - -1Gb / 1b | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 1Gib / 1b" | nu-highlight | print - -1Gib / 1b | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> (1Gib / 1b) == 2 ** 30" | nu-highlight | print - -(1Gib / 1b) == 2 ** 30 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-15 -```nushell" | print -"> 0x[1F FF] # Hexadecimal" | nu-highlight | print - -0x[1F FF] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 0b[1 1010] # Binary" | nu-highlight | print - -0b[1 1010] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 0o[377] # Octal" | nu-highlight | print - -0o[377] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-17 -```nushell" | print -"> {name: sam rank: 10}" | nu-highlight | print - -{name: sam rank: 10} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-19 -```nushell" | print -"> {x:3 y:1} | insert z 0" | nu-highlight | print - -{x:3 y:1} | insert z 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-21 -```nushell" | print -"> {name: sam, rank: 10} | transpose key value" | nu-highlight | print - -{name: sam, rank: 10} | transpose key value | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-23 -```nushell" | print -"> {x:12 y:4}.x" | nu-highlight | print - -{x:12 y:4}.x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-25 -```nushell" | print -"> {\"1\":true \" \":false}.\" \"" | nu-highlight | print - -{"1":true " ":false}." " | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-27 -```nushell" | print -"> let data = { name: alice, age: 50 }" | nu-highlight | print - -let data = { name: alice, age: 50 } - -"> { ...$data, hobby: cricket }" | nu-highlight | print - -{ ...$data, hobby: cricket } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-29 -```nushell" | print -"> [sam fred george]" | nu-highlight | print - -[sam fred george] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-31 -```nushell" | print -"> [bell book candle] | where ($it =~ 'b')" | nu-highlight | print - -[bell book candle] | where ($it =~ 'b') | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-33 -```nushell" | print -"> [a b c].1" | nu-highlight | print - -[a b c].1 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-35 -```nushell" | print -"> [a b c d e f] | slice 1..3" | nu-highlight | print - -[a b c d e f] | slice 1..3 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-37 -```nushell" | print -"> let x = [1 2]" | nu-highlight | print - -let x = [1 2] - -"> [...$x 3 ...(4..7 | take 2)]" | nu-highlight | print - -[...$x 3 ...(4..7 | take 2)] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-39 -```nushell" | print -"> [[column1, column2]; [Value1, Value2] [Value3, Value4]]" | nu-highlight | print - -[[column1, column2]; [Value1, Value2] [Value3, Value4]] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-41 -```nushell" | print -"> [{name: sam, rank: 10}, {name: bob, rank: 7}]" | nu-highlight | print - -[{name: sam, rank: 10}, {name: bob, rank: 7}] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-43 -```nushell" | print -"> [{x:12, y:5}, {x:3, y:6}] | get 0" | nu-highlight | print - -[{x:12, y:5}, {x:3, y:6}] | get 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-45 -```nushell" | print -"> [[x,y];[12,5],[3,6]] | get 0" | nu-highlight | print - -[[x,y];[12,5],[3,6]] | get 0 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-47 -```nushell" | print -"> [{x:12 y:5} {x:4 y:7} {x:2 y:2}].x" | nu-highlight | print - -[{x:12 y:5} {x:4 y:7} {x:2 y:2}].x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-49 -```nushell" | print -"> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z" | nu-highlight | print - -[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select y z | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-51 -```nushell" | print -"> [{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2" | nu-highlight | print - -[{x:0 y:5 z:1} {x:4 y:7 z:3} {x:2 y:2 z:0}] | select 1 2 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-53 -```nushell" | print -"> [{foo: 123}, {}].foo?" | nu-highlight | print - -[{foo: 123}, {}].foo? | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-55 -```nushell" | print -"# Assign a closure to a variable -let greet = { |name| print $\"Hello ($name)\"} -do $greet \"Julian\"" | nu-highlight | print - -"```\n```output-numd" | print - -# Assign a closure to a variable -let greet = { |name| print $"Hello ($name)"} -do $greet "Julian" | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-58 -```nushell" | print -"mut x = 1 -if true { - $x += 1000 -} -print $x" | nu-highlight | print - -"```\n```output-numd" | print - -mut x = 1 -if true { - $x += 1000 -} -print $x | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print - -"#code-block-marker-open-63 -```nushell try,new-instance" | print -"> [{a:1 b:2} {b:1}]" | nu-highlight | print - -/opt/homebrew/bin/nu -c "[{a:1 b:2} {b:1}]" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> [{a:1 b:2} {b:1}].1.a" | nu-highlight | print - -/opt/homebrew/bin/nu -c "[{a:1 b:2} {b:1}].1.a" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"```" | print diff --git a/z_examples/4_book_working_with_lists/working_with_lists.md b/z_examples/4_book_working_with_lists/working_with_lists.md index b107d38..9ee25cd 100644 --- a/z_examples/4_book_working_with_lists/working_with_lists.md +++ b/z_examples/4_book_working_with_lists/working_with_lists.md @@ -4,7 +4,7 @@ Lists are equivalent to the individual columns of tables. You can think of a list as essentially being a "one-column table" (with no column name). Thus, any command which operates on a column _also_ operates on a list. For instance, [`where`](/commands/docs/where.md) can be used with lists: ```nu -> [bell book candle] | where ($it =~ 'b') +[bell book candle] | where ($it =~ 'b') # => ╭──────╮ # => │ bell │ # => │ book │ @@ -28,7 +28,7 @@ Nushell lists are similar to JSON arrays. The same `[ "Item1", "Item2", "Item3" We can [`insert`](/commands/docs/insert.md) values into lists as they flow through the pipeline, for example let's insert the value `10` into the middle of a list: ```nu -> [1, 2, 3, 4] | insert 2 10 +[1, 2, 3, 4] | insert 2 10 # => ╭────╮ # => │ 1 │ # => │ 2 │ @@ -43,7 +43,7 @@ We can [`insert`](/commands/docs/insert.md) values into lists as they flow throu We can also use [`update`](/commands/docs/update.md) to replace the 2nd element with the value `10`. ```nu -> [1, 2, 3, 4] | update 1 10 +[1, 2, 3, 4] | update 1 10 # => ╭────╮ # => │ 1 │ # => │ 10 │ @@ -66,11 +66,6 @@ let colors = ($colors | prepend red) let colors = ($colors | append purple) let colors = ("black" | append $colors) $colors # [black red yellow green purple blue] -``` - -Output: - -``` # => ╭────────╮ # => │ black │ # => │ red │ @@ -87,11 +82,6 @@ let colors = [red yellow green purple] let colors = ($colors | skip 1) let colors = ($colors | drop 2) $colors # [yellow] -``` - -Output: - -``` # => ╭────────╮ # => │ yellow │ # => ╰────────╯ @@ -103,11 +93,6 @@ We also have [`last`](/commands/docs/last.md) and [`first`](/commands/docs/first let colors = [red yellow green purple black magenta] let colors = ($colors | last 3) $colors # [purple black magenta] -``` - -Output: - -``` # => ╭─────────╮ # => │ purple │ # => │ black │ @@ -121,11 +106,6 @@ And from the beginning of a list, let colors = [yellow green purple] let colors = ($colors | first 2) $colors # [yellow green] -``` - -Output: - -``` # => ╭────────╮ # => │ yellow │ # => │ green │ @@ -138,8 +118,8 @@ To append one or more lists together, optionally with values interspersed in bet [spread operator](/book/operators#spread-operator) (`...`): ```nu -> let x = [1 2] -> [ ...$x 3 ...(4..7 | take 2) ] +let x = [1 2] +[ ...$x 3 ...(4..7 | take 2) ] # => ╭───╮ # => │ 1 │ # => │ 2 │ @@ -159,14 +139,15 @@ item, but the [`enumerate`](/commands/docs/enumerate.md) filter can be used to p let names = [Mark Tami Amanda Jeremy] $names | each { |elt| $"Hello, ($elt)!" } # Outputs "Hello, Mark!" and three more similar lines. +# => ╭────────────────╮ +# => │ Hello, Mark! │ +# => │ Hello, Tami! │ +# => │ Hello, Amanda! │ +# => │ Hello, Jeremy! │ +# => ╰────────────────╯ $names | enumerate | each { |elt| $"($elt.index + 1) - ($elt.item)" } # Outputs "1 - Mark", "2 - Tami", etc. -``` - -Output: - -``` # => ╭────────────╮ # => │ 1 - Mark │ # => │ 2 - Tami │ @@ -191,11 +172,6 @@ In this example, we keep only values higher than `7`. ```nu let scores = [7 10 8 6 7] $scores | where $it > 7 # [10 8] -``` - -Output: - -``` # => ╭────╮ # => │ 10 │ # => │ 8 │ @@ -211,17 +187,9 @@ For example: ```nu let scores = [3 8 4] $"total = ($scores | reduce { |elt, acc| $acc + $elt })" # total = 15 - $"total = ($scores | math sum)" # easier approach, same result - $"product = ($scores | reduce --fold 1 { |elt, acc| $acc * $elt })" # product = 96 - $scores | enumerate | reduce --fold 0 { |elt, acc| $acc + $elt.index * $elt.item } # 0*3 + 1*8 + 2*4 = 16 -``` - -Output: - -``` # => 16 ``` @@ -238,11 +206,6 @@ For example, the second element in the list below can be accessed with `$names.1 ```nu let names = [Mark Tami Amanda Jeremy] $names.1 # gives Tami -``` - -Output: - -``` # => Tami ``` @@ -252,11 +215,6 @@ If the index is in some variable `$index` we can use the `get` command to extrac let names = [Mark Tami Amanda Jeremy] let index = 1 $names | get $index # gives Tami -``` - -Output: - -``` # => Tami ``` @@ -269,14 +227,10 @@ It can be used with lists as follows: ```nu let colors = [red green blue] $colors | is-empty # false +# => false let colors = [] $colors | is-empty # true -``` - -Output: - -``` # => true ``` @@ -287,11 +241,6 @@ let colors = [red green blue] 'blue' in $colors # true 'yellow' in $colors # false 'gold' not-in $colors # true -``` - -Output: - -``` # => true ``` @@ -303,21 +252,19 @@ For example: let colors = [red green blue] # Do any color names end with "e"? $colors | any {|elt| $elt | str ends-with "e" } # true +# => true # Is the length of any color name less than 3? $colors | any {|elt| ($elt | str length) < 3 } # false +# => false let scores = [3 8 4] # Are any scores greater than 7? $scores | any {|elt| $elt > 7 } # true +# => true # Are any scores odd? $scores | any {|elt| $elt mod 2 == 1 } # true -``` - -Output: - -``` # => true ``` @@ -329,21 +276,19 @@ For example: let colors = [red green blue] # Do all color names end with "e"? $colors | all {|elt| $elt | str ends-with "e" } # false +# => false # Is the length of all color names greater than or equal to 3? $colors | all {|elt| ($elt | str length) >= 3 } # true +# => true let scores = [3 8 4] # Are all scores greater than 7? $scores | all {|elt| $elt > 7 } # false +# => false # Are all scores even? $scores | all {|elt| $elt mod 2 == 0 } # false -``` - -Output: - -``` # => false ``` @@ -356,13 +301,16 @@ For example: ```nu [1 [2 3] 4 [5 6]] | flatten # [1 2 3 4 5 6] +# => ╭───╮ +# => │ 1 │ +# => │ 2 │ +# => │ 3 │ +# => │ 4 │ +# => │ 5 │ +# => │ 6 │ +# => ╰───╯ [[1 2] [3 [4 5 [6 7 8]]]] | flatten | flatten | flatten # [1 2 3 4 5 6 7 8] -``` - -Output: - -``` # => ╭───╮ # => │ 1 │ # => │ 2 │ @@ -380,18 +328,12 @@ be converted to a separate row with a single column: ```nu let zones = [UTC CET Europe/Moscow Asia/Yekaterinburg] - # Show world clock for selected time zones $zones | wrap 'Zone' | upsert Time {|row| (date now | date to-timezone $row.Zone | format date '%Y.%m.%d %H:%M')} -``` - -Output: - -``` # => ╭────────Zone────────┬───────Time───────╮ -# => │ UTC │ 2025.12.10 20:16 │ -# => │ CET │ 2025.12.10 21:16 │ -# => │ Europe/Moscow │ 2025.12.10 23:16 │ -# => │ Asia/Yekaterinburg │ 2025.12.11 01:16 │ +# => │ UTC │ 2025.12.11 01:24 │ +# => │ CET │ 2025.12.11 02:24 │ +# => │ Europe/Moscow │ 2025.12.11 04:24 │ +# => │ Asia/Yekaterinburg │ 2025.12.11 06:24 │ # => ╰────────Zone────────┴───────Time───────╯ ``` diff --git a/z_examples/4_book_working_with_lists/working_with_lists.md_intermed.nu b/z_examples/4_book_working_with_lists/working_with_lists.md_intermed.nu index fdbbb90..fadafa4 100644 --- a/z_examples/4_book_working_with_lists/working_with_lists.md_intermed.nu +++ b/z_examples/4_book_working_with_lists/working_with_lists.md_intermed.nu @@ -13,18 +13,18 @@ header_on_separator: true, abbreviated_row_count: 1000} "#code-block-marker-open-1 ```nu" | print -"> [bell book candle] | where ($it =~ 'b')" | nu-highlight | print +"[bell book candle] | where ($it =~ 'b')" | nu-highlight | print [bell book candle] | where ($it =~ 'b') | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print "#code-block-marker-open-3 ```nu" | print -"> [1, 2, 3, 4] | insert 2 10" | nu-highlight | print +"[1, 2, 3, 4] | insert 2 10" | nu-highlight | print [1, 2, 3, 4] | insert 2 10 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "# [1, 2, 10, 3, 4]" | nu-highlight | print @@ -32,10 +32,10 @@ header_on_separator: true, abbreviated_row_count: 1000} "#code-block-marker-open-5 ```nu" | print -"> [1, 2, 3, 4] | update 1 10" | nu-highlight | print +"[1, 2, 3, 4] | update 1 10" | nu-highlight | print [1, 2, 3, 4] | update 1 10 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "# [1, 10, 3, 4]" | nu-highlight | print @@ -49,296 +49,253 @@ let colors = ($colors | append purple) let colors = (\"black\" | append $colors) $colors # [black red yellow green purple blue]" | nu-highlight | print -"```\n```output-numd" | print - let colors = [yellow green] let colors = ($colors | prepend red) let colors = ($colors | append purple) let colors = ("black" | append $colors) $colors | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-10 +"#code-block-marker-open-9 ```nu" | print "let colors = [red yellow green purple] let colors = ($colors | skip 1) let colors = ($colors | drop 2) $colors # [yellow]" | nu-highlight | print -"```\n```output-numd" | print - let colors = [red yellow green purple] let colors = ($colors | skip 1) let colors = ($colors | drop 2) $colors | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-13 +"#code-block-marker-open-11 ```nu" | print "let colors = [red yellow green purple black magenta] let colors = ($colors | last 3) $colors # [purple black magenta]" | nu-highlight | print -"```\n```output-numd" | print - let colors = [red yellow green purple black magenta] let colors = ($colors | last 3) $colors | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-16 +"#code-block-marker-open-13 ```nu" | print "let colors = [yellow green purple] let colors = ($colors | first 2) $colors # [yellow green]" | nu-highlight | print -"```\n```output-numd" | print - let colors = [yellow green purple] let colors = ($colors | first 2) $colors | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-19 +"#code-block-marker-open-15 ```nu" | print -"> let x = [1 2]" | nu-highlight | print +"let x = [1 2] +[ ...$x 3 ...(4..7 | take 2) ]" | nu-highlight | print let x = [1 2] - -"> [ ...$x 3 ...(4..7 | take 2) ]" | nu-highlight | print - [ ...$x 3 ...(4..7 | take 2) ] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-21 +"#code-block-marker-open-17 ```nu" | print "let names = [Mark Tami Amanda Jeremy] $names | each { |elt| $\"Hello, ($elt)!\" } -# Outputs \"Hello, Mark!\" and three more similar lines. - -$names | enumerate | each { |elt| $\"($elt.index + 1) - ($elt.item)\" } -# Outputs \"1 - Mark\", \"2 - Tami\", etc." | nu-highlight | print - -"```\n```output-numd" | print +# Outputs \"Hello, Mark!\" and three more similar lines." | nu-highlight | print let names = [Mark Tami Amanda Jeremy] -$names | each { |elt| $"Hello, ($elt)!" } -# Outputs "Hello, Mark!" and three more similar lines. +$names | each { |elt| $"Hello, ($elt)!" } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"$names | enumerate | each { |elt| $\"($elt.index + 1) - ($elt.item)\" } +# Outputs \"1 - Mark\", \"2 - Tami\", etc." | nu-highlight | print $names | enumerate | each { |elt| $"($elt.index + 1) - ($elt.item)" } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-24 +"#code-block-marker-open-19 ```nu" | print "let colors = [red orange yellow green blue purple] $colors | where ($it | str ends-with 'e') # The block passed to `where` must evaluate to a boolean. # This outputs the list [orange blue purple]." | nu-highlight | print -"```\n```output-numd" | print - let colors = [red orange yellow green blue purple] $colors | where ($it | str ends-with 'e') # The block passed to `where` must evaluate to a boolean. - +print '' "```" | print -"#code-block-marker-open-26 +"#code-block-marker-open-21 ```nu" | print "let scores = [7 10 8 6 7] $scores | where $it > 7 # [10 8]" | nu-highlight | print -"```\n```output-numd" | print - let scores = [7 10 8 6 7] $scores | where $it > 7 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-29 +"#code-block-marker-open-23 ```nu" | print "let scores = [3 8 4] $\"total = ($scores | reduce { |elt, acc| $acc + $elt })\" # total = 15 - $\"total = ($scores | math sum)\" # easier approach, same result - $\"product = ($scores | reduce --fold 1 { |elt, acc| $acc * $elt })\" # product = 96 - $scores | enumerate | reduce --fold 0 { |elt, acc| $acc + $elt.index * $elt.item } # 0*3 + 1*8 + 2*4 = 16" | nu-highlight | print -"```\n```output-numd" | print - let scores = [3 8 4] $"total = ($scores | reduce { |elt, acc| $acc + $elt })" # total = 15 - $"total = ($scores | math sum)" # easier approach, same result - $"product = ($scores | reduce --fold 1 { |elt, acc| $acc * $elt })" # product = 96 - $scores | enumerate | reduce --fold 0 { |elt, acc| $acc + $elt.index * $elt.item } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-32 +"#code-block-marker-open-25 ```nu" | print "let names = [Mark Tami Amanda Jeremy] $names.1 # gives Tami" | nu-highlight | print -"```\n```output-numd" | print - let names = [Mark Tami Amanda Jeremy] $names.1 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-35 +"#code-block-marker-open-27 ```nu" | print "let names = [Mark Tami Amanda Jeremy] let index = 1 $names | get $index # gives Tami" | nu-highlight | print -"```\n```output-numd" | print - let names = [Mark Tami Amanda Jeremy] let index = 1 $names | get $index | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-38 +"#code-block-marker-open-29 ```nu" | print "let colors = [red green blue] -$colors | is-empty # false - -let colors = [] -$colors | is-empty # true" | nu-highlight | print - -"```\n```output-numd" | print +$colors | is-empty # false" | nu-highlight | print let colors = [red green blue] -$colors | is-empty # false +$colors | is-empty | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"let colors = [] +$colors | is-empty # true" | nu-highlight | print let colors = [] $colors | is-empty | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-41 +"#code-block-marker-open-31 ```nu" | print "let colors = [red green blue] 'blue' in $colors # true 'yellow' in $colors # false 'gold' not-in $colors # true" | nu-highlight | print -"```\n```output-numd" | print - let colors = [red green blue] 'blue' in $colors # true 'yellow' in $colors # false 'gold' not-in $colors | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-44 +"#code-block-marker-open-33 ```nu" | print "let colors = [red green blue] # Do any color names end with \"e\"? -$colors | any {|elt| $elt | str ends-with \"e\" } # true - -# Is the length of any color name less than 3? -$colors | any {|elt| ($elt | str length) < 3 } # false - -let scores = [3 8 4] -# Are any scores greater than 7? -$scores | any {|elt| $elt > 7 } # true - -# Are any scores odd? -$scores | any {|elt| $elt mod 2 == 1 } # true" | nu-highlight | print - -"```\n```output-numd" | print +$colors | any {|elt| $elt | str ends-with \"e\" } # true" | nu-highlight | print let colors = [red green blue] # Do any color names end with "e"? -$colors | any {|elt| $elt | str ends-with "e" } # true +$colors | any {|elt| $elt | str ends-with "e" } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"# Is the length of any color name less than 3? +$colors | any {|elt| ($elt | str length) < 3 } # false" | nu-highlight | print # Is the length of any color name less than 3? -$colors | any {|elt| ($elt | str length) < 3 } # false +$colors | any {|elt| ($elt | str length) < 3 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"let scores = [3 8 4] +# Are any scores greater than 7? +$scores | any {|elt| $elt > 7 } # true" | nu-highlight | print let scores = [3 8 4] # Are any scores greater than 7? -$scores | any {|elt| $elt > 7 } # true +$scores | any {|elt| $elt > 7 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"# Are any scores odd? +$scores | any {|elt| $elt mod 2 == 1 } # true" | nu-highlight | print # Are any scores odd? $scores | any {|elt| $elt mod 2 == 1 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-47 +"#code-block-marker-open-35 ```nu" | print "let colors = [red green blue] # Do all color names end with \"e\"? -$colors | all {|elt| $elt | str ends-with \"e\" } # false - -# Is the length of all color names greater than or equal to 3? -$colors | all {|elt| ($elt | str length) >= 3 } # true - -let scores = [3 8 4] -# Are all scores greater than 7? -$scores | all {|elt| $elt > 7 } # false - -# Are all scores even? -$scores | all {|elt| $elt mod 2 == 0 } # false" | nu-highlight | print - -"```\n```output-numd" | print +$colors | all {|elt| $elt | str ends-with \"e\" } # false" | nu-highlight | print let colors = [red green blue] # Do all color names end with "e"? -$colors | all {|elt| $elt | str ends-with "e" } # false +$colors | all {|elt| $elt | str ends-with "e" } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"# Is the length of all color names greater than or equal to 3? +$colors | all {|elt| ($elt | str length) >= 3 } # true" | nu-highlight | print # Is the length of all color names greater than or equal to 3? -$colors | all {|elt| ($elt | str length) >= 3 } # true +$colors | all {|elt| ($elt | str length) >= 3 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"let scores = [3 8 4] +# Are all scores greater than 7? +$scores | all {|elt| $elt > 7 } # false" | nu-highlight | print let scores = [3 8 4] # Are all scores greater than 7? -$scores | all {|elt| $elt > 7 } # false +$scores | all {|elt| $elt > 7 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"# Are all scores even? +$scores | all {|elt| $elt mod 2 == 0 } # false" | nu-highlight | print # Are all scores even? $scores | all {|elt| $elt mod 2 == 0 } | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-50 +"#code-block-marker-open-37 ```nu" | print -"[1 [2 3] 4 [5 6]] | flatten # [1 2 3 4 5 6] - -[[1 2] [3 [4 5 [6 7 8]]]] | flatten | flatten | flatten # [1 2 3 4 5 6 7 8]" | nu-highlight | print - -"```\n```output-numd" | print +"[1 [2 3] 4 [5 6]] | flatten # [1 2 3 4 5 6]" | nu-highlight | print -[1 [2 3] 4 [5 6]] | flatten # [1 2 3 4 5 6] +[1 [2 3] 4 [5 6]] | flatten | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' +"[[1 2] [3 [4 5 [6 7 8]]]] | flatten | flatten | flatten # [1 2 3 4 5 6 7 8]" | nu-highlight | print [[1 2] [3 [4 5 [6 7 8]]]] | flatten | flatten | flatten | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-53 +"#code-block-marker-open-39 ```nu" | print "let zones = [UTC CET Europe/Moscow Asia/Yekaterinburg] - # Show world clock for selected time zones $zones | wrap 'Zone' | upsert Time {|row| (date now | date to-timezone $row.Zone | format date '%Y.%m.%d %H:%M')}" | nu-highlight | print -"```\n```output-numd" | print - let zones = [UTC CET Europe/Moscow Asia/Yekaterinburg] - # Show world clock for selected time zones $zones | wrap 'Zone' | upsert Time {|row| (date now | date to-timezone $row.Zone | format date '%Y.%m.%d %H:%M')} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/5_simple_nu_table/simple_nu_table.md b/z_examples/5_simple_nu_table/simple_nu_table.md index 8eeb13a..d77e57f 100644 --- a/z_examples/5_simple_nu_table/simple_nu_table.md +++ b/z_examples/5_simple_nu_table/simple_nu_table.md @@ -1,14 +1,9 @@ ```nushell -> $env.numd? +$env.numd? ``` ```nushell [[a b c]; [1 2 3]] -``` - -Output: - -``` # => ╭─a─┬─b─┬─c─╮ # => │ 1 │ 2 │ 3 │ # => ╰─a─┴─b─┴─c─╯ @@ -16,23 +11,15 @@ Output: ```nushell [[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -] -``` - -Output: - -``` + 'Elit quis pariatur, in ut anim anim ut.')]] # => ╭─column──┬─────────────────────────────────────────────────long_text──────────────────────────────────────────────────╮ # => │ value_1 │ Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.Eu aute lorem ullamco sed ipsu... │ # => │ value_2 │ Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip elit lorem, ut quis n... │ diff --git a/z_examples/5_simple_nu_table/simple_nu_table.md_intermed.nu b/z_examples/5_simple_nu_table/simple_nu_table.md_intermed.nu index bd08bf7..f0d4636 100644 --- a/z_examples/5_simple_nu_table/simple_nu_table.md_intermed.nu +++ b/z_examples/5_simple_nu_table/simple_nu_table.md_intermed.nu @@ -13,52 +13,42 @@ header_on_separator: true, abbreviated_row_count: 1000} "#code-block-marker-open-0 ```nushell" | print -"> $env.numd?" | nu-highlight | print +"$env.numd?" | nu-highlight | print $env.numd? | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print "#code-block-marker-open-2 ```nushell" | print "[[a b c]; [1 2 3]]" | nu-highlight | print -"```\n```output-numd" | print - [[a b c]; [1 2 3]] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-5 +"#code-block-marker-open-4 ```nushell" | print "[[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -]" | nu-highlight | print - -"```\n```output-numd" | print + 'Elit quis pariatur, in ut anim anim ut.')]]" | nu-highlight | print [[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - + 'Elit quis pariatur, in ut anim anim ut.')]] | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' +print '' "```" | print diff --git a/z_examples/5_simple_nu_table/simple_nu_table_customized_example_config.md b/z_examples/5_simple_nu_table/simple_nu_table_customized_example_config.md index 567e911..f08c66f 100644 --- a/z_examples/5_simple_nu_table/simple_nu_table_customized_example_config.md +++ b/z_examples/5_simple_nu_table/simple_nu_table_customized_example_config.md @@ -1,14 +1,9 @@ ```nushell -> $env.numd? +$env.numd? ``` ```nushell [[a b c]; [1 2 3]] -``` - -Output: - -``` # => ╭─#─┬─a─┬─b─┬─c─╮ # => │ 0 │ 1 │ 2 │ 3 │ # => ╰───┴───┴───┴───╯ @@ -16,23 +11,15 @@ Output: ```nushell [[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -] -``` - -Output: - -``` + 'Elit quis pariatur, in ut anim anim ut.')]] # => ╭─#─┬─column──┬───────────────────────────────────────────────long_text────────────────────────────────────────────────╮ # => │ 0 │ value_1 │ Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.Eu aute lorem ullamco sed ip… │ # => │ 1 │ value_2 │ Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip elit lorem, ut quis… │ diff --git a/z_examples/5_simple_nu_table/simple_nu_table_customized_width20.md b/z_examples/5_simple_nu_table/simple_nu_table_customized_width20.md index e7cf075..8e26467 100644 --- a/z_examples/5_simple_nu_table/simple_nu_table_customized_width20.md +++ b/z_examples/5_simple_nu_table/simple_nu_table_customized_width20.md @@ -1,14 +1,9 @@ ```nushell -> $env.numd? +$env.numd? ``` ```nushell [[a b c]; [1 2 3]] -``` - -Output: - -``` # => ╭─#─┬─a─┬─b─┬─c─╮ # => │ 0 │ 1 │ 2 │ 3 │ # => ╰─#─┴─a─┴─b─┴─c─╯ @@ -16,23 +11,15 @@ Output: ```nushell [[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -] -``` - -Output: - -``` + 'Elit quis pariatur, in ut anim anim ut.')]] # => ╭─#─┬─column─┬─...─╮ # => │ 0 │ value… │ ... │ # => │ 1 │ value… │ ... │ diff --git a/z_examples/6_edge_cases/error-with-try.md b/z_examples/6_edge_cases/error-with-try.md index 784dd72..8fd11f4 100644 --- a/z_examples/6_edge_cases/error-with-try.md +++ b/z_examples/6_edge_cases/error-with-try.md @@ -1,5 +1,5 @@ ```nushell no-run -> lssomething +lssomething ╭───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ msg │ External command failed │ │ debug │ ExternalCommand { label: "Command `lssomething` not found", help: "`lssomething` is neither a Nushell built-in or a known external command", span: Span { start: 1967901, end: 1967912 } } │ @@ -8,7 +8,7 @@ ``` ```nushell try, new-instance -> lssomething +lssomething # => Error: nu::shell::external_command # => # => x External command failed @@ -17,7 +17,6 @@ # => : ^^^^^|^^^^^ # => : `-- Command `lssomething` not found # => `---- -# => help: `lssomething` is neither a Nushell built-in or a known external -# => command +# => help: `lssomething` is neither a Nushell built-in or a known external command # => ``` diff --git a/z_examples/6_edge_cases/error-with-try.md_intermed.nu b/z_examples/6_edge_cases/error-with-try.md_intermed.nu index 1c09f93..d65860d 100644 --- a/z_examples/6_edge_cases/error-with-try.md_intermed.nu +++ b/z_examples/6_edge_cases/error-with-try.md_intermed.nu @@ -13,8 +13,8 @@ header_on_separator: true, abbreviated_row_count: 1000} "#code-block-marker-open-2 ```nushell try, new-instance" | print -"> lssomething" | nu-highlight | print +"lssomething" | nu-highlight | print /opt/homebrew/bin/nu -c "lssomething" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout} | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/6_edge_cases/error_customized.md b/z_examples/6_edge_cases/error_customized.md index d3bde64..646ab5d 100644 --- a/z_examples/6_edge_cases/error_customized.md +++ b/z_examples/6_edge_cases/error_customized.md @@ -1,3 +1,3 @@ ```nushell -> lssomething +lssomething ``` diff --git a/z_examples/6_edge_cases/raw_strings_test.md b/z_examples/6_edge_cases/raw_strings_test.md index d119e43..a62ce47 100644 --- a/z_examples/6_edge_cases/raw_strings_test.md +++ b/z_examples/6_edge_cases/raw_strings_test.md @@ -7,11 +7,6 @@ let $two_single_lines_text = r#'"High up in the mountains, a Snake crawled and l ```nu $two_single_lines_text -``` - -Output: - -``` # => "High up in the mountains, a Snake crawled and lay in a damp gorge, coiled # => into a knot, staring out at the sea. ``` diff --git a/z_examples/6_edge_cases/raw_strings_test.md_intermed.nu b/z_examples/6_edge_cases/raw_strings_test.md_intermed.nu index 3777b6f..d5de404 100644 --- a/z_examples/6_edge_cases/raw_strings_test.md_intermed.nu +++ b/z_examples/6_edge_cases/raw_strings_test.md_intermed.nu @@ -16,19 +16,15 @@ header_on_separator: true, abbreviated_row_count: 1000} "let $two_single_lines_text = r#'\"High up in the mountains, a Snake crawled and lay in a damp gorge, coiled into a knot, staring out at the sea.'#" | nu-highlight | print -"```\n```output-numd" | print - let $two_single_lines_text = r#'"High up in the mountains, a Snake crawled and lay in a damp gorge, coiled into a knot, staring out at the sea.'# - +print '' "```" | print "#code-block-marker-open-3 ```nu" | print "$two_single_lines_text" | nu-highlight | print -"```\n```output-numd" | print - $two_single_lines_text | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/6_edge_cases/simple_markdown_first_block.md b/z_examples/6_edge_cases/simple_markdown_first_block.md index 295e9a6..1b2fc44 100644 --- a/z_examples/6_edge_cases/simple_markdown_first_block.md +++ b/z_examples/6_edge_cases/simple_markdown_first_block.md @@ -7,11 +7,6 @@ let $var1 = 'foo' ```nu # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' -``` - -Output: - -``` # => foo/baz/bar ``` @@ -19,9 +14,9 @@ Output: ```nu # This block will output results inline -> whoami +whoami # => user -> 2 + 2 +2 + 2 # => 4 ``` diff --git a/z_examples/6_edge_cases/simple_markdown_first_block.md_intermed.nu b/z_examples/6_edge_cases/simple_markdown_first_block.md_intermed.nu index 2ff8907..e82127a 100644 --- a/z_examples/6_edge_cases/simple_markdown_first_block.md_intermed.nu +++ b/z_examples/6_edge_cases/simple_markdown_first_block.md_intermed.nu @@ -15,10 +15,8 @@ header_on_separator: true, abbreviated_row_count: 1000} ```nu" | print "let $var1 = 'foo'" | nu-highlight | print -"```\n```output-numd" | print - let $var1 = 'foo' - +print '' "```" | print "#code-block-marker-open-2 @@ -26,24 +24,21 @@ let $var1 = 'foo' "# This block will produce some output in a separate block $var1 | path join 'baz' 'bar'" | nu-highlight | print -"```\n```output-numd" | print - # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print -"#code-block-marker-open-5 +"#code-block-marker-open-4 ```nu" | print -"# This block will output results inline" | nu-highlight | print - - -"> whoami" | nu-highlight | print +"# This block will output results inline +whoami" | nu-highlight | print +# This block will output results inline whoami | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - -"> 2 + 2" | nu-highlight | print +print '' +"2 + 2" | nu-highlight | print 2 + 2 | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print '' - +print '' "```" | print diff --git a/z_examples/99_strip_markdown/numd_commands_explanations.nu b/z_examples/99_strip_markdown/numd_commands_explanations.nu index 4bdcaf4..b1d4733 100644 --- a/z_examples/99_strip_markdown/numd_commands_explanations.nu +++ b/z_examples/99_strip_markdown/numd_commands_explanations.nu @@ -2,7 +2,6 @@ # ```nu # This setting is for overriding the author's usual small number of `abbreviated_row_count`. $env.config.table.abbreviated_row_count = 100 - # The `$init_numd_pwd_const` constant points to the current working directory from where the `numd` command was initiated. # It is added by `numd` in every intermediate script to make it available in cases like below. # We use `path join` here to construct working paths for both Windows and Unix @@ -12,9 +11,9 @@ use ($init_numd_pwd_const | path join numd commands.nu) * # ```nu # Here we set the `$file` variable (which will be used in several commands throughout this script) to point to `z_examples/1_simple_markdown/simple_markdown.md`. let $file = $init_numd_pwd_const | path join z_examples 1_simple_markdown simple_markdown.md - let $md_orig = open -r $file | toggle-output-fences let $original_md_table = $md_orig | find-code-blocks + $original_md_table | table -e --width 120 @@ -33,9 +32,9 @@ open $intermediate_script_path # ```nu # the flag `$no_fail_on_error` is set to false let $no_fail_on_error = false - let $nu_res_stdout_lines = execute-intermediate-script $intermediate_script_path $no_fail_on_error false rm $intermediate_script_path + $nu_res_stdout_lines diff --git a/z_examples/99_strip_markdown/simple_markdown.nu b/z_examples/99_strip_markdown/simple_markdown.nu index e55af28..cba0f81 100644 --- a/z_examples/99_strip_markdown/simple_markdown.nu +++ b/z_examples/99_strip_markdown/simple_markdown.nu @@ -3,7 +3,7 @@ let $var1 = 'foo' - # ```nu + # ```nu separate-block # This block will produce some output in a separate block $var1 | path join 'baz' 'bar' @@ -11,4 +11,5 @@ $var1 | path join 'baz' 'bar' # ```nu # This block will output results inline whoami + 2 + 2 diff --git a/z_examples/99_strip_markdown/simple_markdown_first_block.nu b/z_examples/99_strip_markdown/simple_markdown_first_block.nu index e55af28..f0e0855 100644 --- a/z_examples/99_strip_markdown/simple_markdown_first_block.nu +++ b/z_examples/99_strip_markdown/simple_markdown_first_block.nu @@ -11,4 +11,5 @@ $var1 | path join 'baz' 'bar' # ```nu # This block will output results inline whoami + 2 + 2 diff --git a/z_examples/99_strip_markdown/simple_nu_table.nu b/z_examples/99_strip_markdown/simple_nu_table.nu index 60b23d8..4553924 100644 --- a/z_examples/99_strip_markdown/simple_nu_table.nu +++ b/z_examples/99_strip_markdown/simple_nu_table.nu @@ -9,15 +9,12 @@ $env.numd? # ```nushell [[column long_text]; - ['value_1' ('Veniam cillum et et. Et et qui enim magna. Qui enim, magna eu aute lorem.' + 'Eu aute lorem ullamco sed ipsum incididunt irure. Lorem ullamco sed ipsum incididunt.' + 'Sed ipsum incididunt irure, culpa. Irure, culpa labore sit sunt.')] - ['value_2' ('Irure quis magna ipsum anim. Magna ipsum anim aliquip elit lorem ut. Anim aliquip ' + 'elit lorem, ut quis nostrud. Lorem ut quis, nostrud commodo non. Nostrud commodo non ' + 'cillum exercitation dolore fugiat nulla. Non cillum exercitation dolore fugiat nulla ' + 'ut. Exercitation dolore fugiat nulla ut adipiscing laboris elit. Fugiat nulla ut ' + 'adipiscing, laboris elit quis pariatur. Adipiscing laboris elit quis pariatur. ' + - 'Elit quis pariatur, in ut anim anim ut.')] -] + 'Elit quis pariatur, in ut anim anim ut.')]] diff --git a/z_examples/99_strip_markdown/types_of_data.nu b/z_examples/99_strip_markdown/types_of_data.nu index 9e7f1ac..575a3d4 100644 --- a/z_examples/99_strip_markdown/types_of_data.nu +++ b/z_examples/99_strip_markdown/types_of_data.nu @@ -14,6 +14,7 @@ # ```nushell let mybool = 2 > 1 $mybool + let mybool = ($nu.home-path | path exists) $mybool @@ -28,13 +29,17 @@ $mybool # ```nushell 1Gb / 1b + 1Gib / 1b + (1Gib / 1b) == 2 ** 30 # ```nushell 0x[1F FF] # Hexadecimal + 0b[1 1010] # Binary + 0o[377] # Octal @@ -119,6 +124,7 @@ let x = [1 2] # ```nushell # Assign a closure to a variable let greet = { |name| print $"Hello ($name)"} + do $greet "Julian" @@ -127,9 +133,14 @@ mut x = 1 if true { $x += 1000 } + print $x +1001 +1001 +1001 # ```nushell try,new-instance [{a:1 b:2} {b:1}] + [{a:1 b:2} {b:1}].1.a diff --git a/z_examples/99_strip_markdown/working_with_lists.nu b/z_examples/99_strip_markdown/working_with_lists.nu index 44878d6..f705893 100644 --- a/z_examples/99_strip_markdown/working_with_lists.nu +++ b/z_examples/99_strip_markdown/working_with_lists.nu @@ -5,11 +5,13 @@ # ```nu [1, 2, 3, 4] | insert 2 10 + # [1, 2, 10, 3, 4] # ```nu [1, 2, 3, 4] | update 1 10 + # [1, 10, 3, 4] @@ -69,11 +71,8 @@ $scores | where $it > 7 # [10 8] # ```nu let scores = [3 8 4] $"total = ($scores | reduce { |elt, acc| $acc + $elt })" # total = 15 - $"total = ($scores | math sum)" # easier approach, same result - $"product = ($scores | reduce --fold 1 { |elt, acc| $acc * $elt })" # product = 96 - $scores | enumerate | reduce --fold 0 { |elt, acc| $acc + $elt.index * $elt.item } # 0*3 + 1*8 + 2*4 = 16 @@ -143,6 +142,5 @@ $scores | all {|elt| $elt mod 2 == 0 } # false # ```nu let zones = [UTC CET Europe/Moscow Asia/Yekaterinburg] - # Show world clock for selected time zones $zones | wrap 'Zone' | upsert Time {|row| (date now | date to-timezone $row.Zone | format date '%Y.%m.%d %H:%M')}