Skip to content
57 changes: 34 additions & 23 deletions numd/commands.nu
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export def run [
let original_md = open -r $file

let intermediate_script_path = $save_intermed_script
| default ($file | build-modified-path --prefix $'numd-temp-(generate-timestamp)' --extension '.nu')
| default ($file | build-modified-path --suffix $'-numd-temp-(generate-timestamp)' --extension '.nu')

let result = parse-file $file
| execute-blocks --eval $eval --no-fail-on-error=$no_fail_on_error --print-block-results=$print_block_results --save-intermed-script $intermediate_script_path --use-host-config=$use_host_config
Expand Down Expand Up @@ -71,8 +71,8 @@ export def clear-outputs [
# Extract pure Nushell script from blocks table (strip markdown fences)
export def to-numd-script []: table -> string {
where action == 'execute'
| each {|block|
$block.line
| each {
$in.line
| update 0 { $'(char nl) # ($in)' } # keep infostring as comment
| drop # remove closing fence
| str join (char nl)
Expand Down Expand Up @@ -110,6 +110,7 @@ export def execute-blocks [

# Update original table with execution results
let result_indices = $results | get block_index

$original
| each {|block|
if $block.block_index in $result_indices {
Expand Down Expand Up @@ -398,9 +399,9 @@ export def compute-change-stats [
($change_value / $metric_stats.old) * 100
| math round --precision 1
| if $in < 0 {
$"(ansi red)($change_value) \(($in)%\)(ansi reset)"
$"($change_value) \(($in)%\)"
} else if ($in > 0) {
$"(ansi blue)+($change_value) \(($in)%\)(ansi reset)"
$"+($change_value) \(($in)%\)"
} else { '0%' }
}
| update metric { $'diff_($in)' }
Expand Down Expand Up @@ -456,6 +457,17 @@ export def quote-for-print []: string -> string {
| $'"($in)"'
}

# Append a pipeline to a command string by extracting the closure body.
export def pipe-to [closure: closure]: string -> string {
let $input = $in

view source $closure
| str substring 1..(-2)
| str replace -r '^\s+' ''
| str replace -r '\s+$' ''
| $input + " | " + $in
}

# Run the intermediate script and return its output as a string.
export def execute-intermediate-script [
intermed_script_path: path # path to the generated intermediate script
Expand Down Expand Up @@ -506,7 +518,8 @@ export def code-block-marker [
} --result "\"ls\" | nu-highlight | print\n\n"
export def generate-highlight-print []: string -> string {
quote-for-print
| $"($in) | nu-highlight | print(char nl)(char nl)"
| pipe-to { nu-highlight | print }
| $in + "\n\n"
}

# Trim comments and extra whitespace from code blocks for use in the generated script.
Expand All @@ -525,8 +538,8 @@ export def trim-trailing-comments []: string -> string {
export def get-last-span [
command: string
]: nothing -> string {
let command = $command | str trim
let spans = ast $command --json
let trimmed = $command | str trim
let spans = ast $trimmed --json
| get block
| from json
| to yaml
Expand All @@ -544,7 +557,7 @@ export def get-last-span [

let offset = $longest_last_span_start - $last_span_end

$command
$trimmed
| str substring $offset..
}

Expand All @@ -567,24 +580,24 @@ export def can-append-print [
# Generate a pipeline that captures command output with `# =>` prefix for inline display.
@example "generate inline output capture pipeline" {
'ls' | generate-inline-output-pipeline
} --result "ls | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\\s*$' \"\\n\""
export def generate-inline-output-pipeline [
--indent: string = '# => ' # prefix string for each output line
]: string -> string {
} --result "ls | table --width 120 | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\\s*$' (char nl)"
export def generate-inline-output-pipeline []: string -> string {
generate-table-statement
| $"($in) | default '' | into string | lines | each {$'($indent)\($in\)' | str trim --right} | str join \(char nl\) | str replace -r '\\s*$' \"\\n\""
| pipe-to {
default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl)
}
}

# Generate a print statement for capturing command output.
@example "wrap command with print" { 'ls' | generate-print-statement } --result "ls | print; print ''"
export def generate-print-statement []: string -> string {
$"($in) | print; print ''" # The last `print ''` is for newlines after executed commands
pipe-to { print; print '' } # The last `print ''` is for newlines after executed commands
}

# Generate a table statement with width evaluated at runtime from $env.numd.table-width.
@example "default table width" { 'ls' | generate-table-statement } --result 'ls | table --width ($env.numd?.table-width? | default 120)'
export def generate-table-statement []: string -> string {
$in + ' | table --width ($env.numd?.table-width? | default 120)'
pipe-to { table --width ($env.numd?.table-width? | default 120) }
}

# Wrap code in a try-catch block to handle errors gracefully.
Expand All @@ -594,10 +607,8 @@ export def wrap-in-try-catch [
]: string -> string {
if $new_instance {
quote-for-print
| (
$'($nu.current-exe) -c ($in)' +
" | complete | if ($in.exit_code != 0) {get stderr} else {get stdout}"
)
| $'($nu.current-exe) -c ($in)'
| pipe-to { complete | if ($in.exit_code != 0) { get stderr } else { get stdout } }
} else {
$"try {($in)} catch {|error| $error}"
}
Expand All @@ -613,7 +624,7 @@ export def generate-separate-block-fence []: string -> string {
export def join-and-print []: list<string> -> string {
str join (char nl)
| quote-for-print
| $'($in) | print'
| pipe-to { print }
}

# Generate marker tags and code block delimiters for tracking output in the intermediate script.
Expand Down Expand Up @@ -656,7 +667,7 @@ export def build-modified-path [
| update stem { $'($prefix)($in)($suffix)' }
| if $extension != null { update extension { $in + $extension } } else { }
| if $parent_dir != null {
update parent { path join $parent_dir | $'(mkdir $in)($in)' }
update parent { path join $parent_dir | tee { mkdir $in } }
} else { }
| path join
}
Expand Down Expand Up @@ -697,7 +708,7 @@ export def --env load-config [

# Preserve existing $env.numd fields, only update prepend-code
let base = $env.numd? | default {}
$env.numd = $base | merge { prepend-code: $code }
$env.numd = $base | merge {prepend-code: $code}
}

# Generate a timestamp string in the format YYYYMMDD_HHMMSS.
Expand Down
6 changes: 3 additions & 3 deletions z_examples/1_simple_markdown/simple_markdown.md_intermed.nu
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $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 ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
$var1 | path join 'baz' 'bar' | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print

Expand All @@ -43,10 +43,10 @@ print ''
whoami" | nu-highlight | print

# This block will output results inline
whoami | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
whoami | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"2 + 2" | nu-highlight | print

2 + 2 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
2 + 2 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ open $intermediate_script_path
# => "```\n```output-numd" | print
# =>
# => # This block will produce some output in a separate block
# => $var1 | path join 'baz' 'bar' | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
# => $var1 | path join 'baz' 'bar' | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
# => print ''
# => "```" | print
# =>
Expand All @@ -127,11 +127,11 @@ open $intermediate_script_path
# => whoami" | nu-highlight | print
# =>
# => # This block will output results inline
# => whoami | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
# => whoami | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
# => print ''
# => "2 + 2" | nu-highlight | print
# =>
# => 2 + 2 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
# => 2 + 2 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
# => print ''
# => "```" | print
```
Expand Down Expand Up @@ -221,8 +221,8 @@ compute-change-stats $file $md_orig $md_res
# => │ filename │ simple_markdown.md │
# => │ nushell_blocks │ 3 │
# => │ levenshtein_dist │ 248 │
# => │ diff_lines │ -12 (-33.3%)
# => │ diff_words │ -24 (-30.8%)
# => │ diff_chars │ -163 (-32.5%)
# => │ diff_lines │ --12 (-33.3%) │
# => │ diff_words │ --24 (-30.8%) │
# => │ diff_chars │ --163 (-32.5%) │
# => ╰──────────────────┴────────────────────╯
```
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let $original_md_table = $md_orig | parse-markdown-to-blocks
print ''
"$original_md_table | table -e --width 120" | nu-highlight | print

$original_md_table | table -e --width 120 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
$original_md_table | table -e --width 120 | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print

Expand All @@ -69,11 +69,11 @@ print ''

decorate-original-code-blocks $original_md_table
| generate-intermediate-script
| save -f $intermediate_script_path | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
| save -f $intermediate_script_path | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"open $intermediate_script_path" | nu-highlight | print

open $intermediate_script_path | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
open $intermediate_script_path | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print

Expand All @@ -87,11 +87,11 @@ 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 false
rm $intermediate_script_path | table --width ($env.numd?.table-width? | default 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 ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"$nu_res_stdout_lines" | nu-highlight | print

$nu_res_stdout_lines | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
$nu_res_stdout_lines | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print

Expand All @@ -107,14 +107,14 @@ let $md_res = $nu_res_stdout_lines
print ''
"$md_res" | nu-highlight | print

$md_res | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
$md_res | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print

"#code-block-marker-open-11
```nu" | print
"compute-change-stats $file $md_orig $md_res" | nu-highlight | print

compute-change-stats $file $md_orig $md_res | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each {$'# => ($in)' | str trim --right} | str join (char nl) | str replace -r '\s*$' "\n" | print; print ''
compute-change-stats $file $md_orig $md_res | table --width ($env.numd?.table-width? | default 120) | default '' | into string | lines | each { $'# => ($in)' | str trim --right } | str join (char nl) | str replace -r '\s*$' (char nl) | print; print ''
print ''
"```" | print
Loading