Skip to content

Commit

Permalink
Merge pull request #68 from nushell-prophet/dev
Browse files Browse the repository at this point in the history
embed `scan` from `std tier scan` 0.101, rename variables
  • Loading branch information
maxim-uvarov authored Dec 26, 2024
2 parents c7b6e08 + 9ee894c commit c43e481
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Output:
╰──────────────────name───────────────────┴─type─╯
> sys host | get boot_time
Sat, 23 Nov 2024 18:43:07 -0300 (a week ago)
Sun, 22 Dec 2024 19:11:35 -0300 (3 days ago)
> 2 + 2
4
Expand Down
67 changes: 44 additions & 23 deletions numd/commands.nu
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
# use std iter scan

export def scan [ # -> list<any>
init: any # initial value to seed the initial state
fn: closure # the closure to perform the scan
--noinit(-n) # remove the initial value from the result
] {
reduce --fold [$init] {|it, acc|
$acc ++ [(do $fn ($acc | last) $it)]
}
| if $noinit {
$in | skip
} else {
$in
}
}


# Run Nushell code blocks in a markdown file, output results back to the `.md`, and optionally to terminal
export def run [
file: path # path to a `.md` file containing Nushell code to be executed
Expand Down Expand Up @@ -279,21 +261,21 @@ export def find-code-blocks []: string -> table {
str trim --right
| if $in =~ '^```' {} else {'text'}
}
| scan --noinit 'text' {|prev_fence curr_fence|
| scan --noinit 'text' {|curr_fence prev_fence|
match $curr_fence {
'text' => { if $prev_fence == 'closing-fence' { 'text' } else { $prev_fence } }
'```' => { if $prev_fence == 'text' { '```' } else { 'closing-fence' } }
_ => { $curr_fence }
}
}
| scan --noinit 'text' {|prev_fence curr_fence|
| scan --noinit 'text' {|curr_fence prev_fence|
if $curr_fence == 'closing-fence' { $prev_fence } else { $curr_fence }
}

let $block_index = $row_type
| window --remainder 2
| scan 0 {|prev_line curr_line|
if $curr_line.0 == $curr_line.1? { $prev_line } else { $prev_line + 1 }
| scan 0 {|window index|
if $window.0 == $window.1? { $index } else { $index + 1 }
}

# Wrap lists into columns because the `window` command was used previously
Expand Down Expand Up @@ -425,7 +407,7 @@ export def extract-block-index [
-1
}
}
| scan --noinit 0 {|prev_index curr_index|
| scan --noinit 0 {|curr_index prev_index|
if $curr_index == -1 {$prev_index} else {$curr_index}
}
| wrap block_index
Expand Down Expand Up @@ -843,3 +825,42 @@ export def --env load-config [
export def generate-timestamp []: nothing -> string {
date now | format date "%Y%m%d_%H%M%S"
}

# I hardcode scan from 0.101 version here, as there were a breaking change
# to give chance for execution in previous untested nushell versions

# Returns a list of intermediate steps performed by `reduce`
# (`fold`). It takes two arguments, an initial value to seed the
# initial state and a closure that takes two arguments, the first
# being the list element in the current iteration and the second
# the internal state.
# The internal state is also provided as pipeline input.
#
# # Example
# ```
# use std ["assert equal" "iter scan"]
# let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y})
#
# assert equal $scanned [0, 1, 3, 6]
#
# # use the --noinit(-n) flag to remove the initial value from
# # the final result
# let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y} -n)
#
# assert equal $scanned [1, 3, 6]
# ```
export def scan [ # -> list<any>
init: any # initial value to seed the initial state
fn: closure # the closure to perform the scan
--noinit(-n) # remove the initial value from the result
] {
reduce --fold [$init] {|it, acc|
let acc_last = $acc | last
$acc ++ [($acc_last | do $fn $it $acc_last)]
}
| if $noinit {
$in | skip
} else {
$in
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,52 @@ $original_md_table | table -e
Output:

```
// ╭─block_index──┬────row_type─────┬───────────────────────────────────line────────────────────────────────────┬─────action─────╮
// │ 0 │ text │ ╭───────────────────────────────────────────────────────────────────────╮ │ print-as-it-is
// │ │ │ │ # This is a simple markdown example │
// │ │ │ │ │
// │ │ │ │ ## 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 │
// │ │ │ │ ``` │
// │ │ │ ╰─────────────────────────────────────────╯
// ╰─block_index──┴────row_type─────┴───────────────────────────────────line────────────────────────────────────┴─────action─────╯
// ╭─block_index──┬────row_type─────┬───────────────────────────────────line────────────────────────────────────┬─────action─────╮
// │ 0 │ text │ ╭───────────────────────────────────────────────────────────────────────╮ │ print-as-it-is │
// │ │ │ │ # This is a simple markdown example │
// │ │ │ │ │
// │ │ │ │ ## 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 │
// │ │ │ │ ``` │
// │ │ │ ╰─────────────────────────────────────────╯
// ╰─block_index──┴────row_type─────┴───────────────────────────────────line────────────────────────────────────┴─────action─────╯
```

## generate-intermediate-script
Expand Down
Loading

0 comments on commit c43e481

Please sign in to comment.