Skip to content

Commit 9d8e5f2

Browse files
authored
Merge pull request #113 from nushell-prophet/feat/run-once
Feat/run once
2 parents 02ae52a + 4007ca7 commit 9d8e5f2

4 files changed

Lines changed: 73 additions & 2 deletions

File tree

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Blocks support fence options (e.g., ` ```nushell try, no-output `):
8282
- `try` / `t`: Wrap in try-catch
8383
- `new-instance` / `n`: Execute in separate Nushell instance
8484
- `separate-block` / `s`: Output results in separate code block instead of inline `# =>`
85+
- `run-once`: Execute code block once, then set to `no-run`
8586

8687
### Output Format Conventions
8788

numd/commands.nu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export def decorate-original-code-blocks [
256256
| insert code {|i|
257257
$i.line
258258
| process-code-block-content ($i.row_type | extract-fence-options)
259-
| generate-block-markers $i.block_index $i.row_type
259+
| generate-block-markers $i.block_index ($i.row_type | str replace 'run-once' 'no-run')
260260
}
261261
}
262262

@@ -648,7 +648,7 @@ export def generate-block-markers [
648648
}
649649

650650
# Parse options from a code fence and return them as a list.
651-
@example "parse fence options with short forms" { '```nu no-run, t' | extract-fence-options } --result [no-run, try]
651+
@example "parse fence options with short forms" { '```nu no-run, t' | extract-fence-options } --result [no-run try]
652652
export def extract-fence-options []: string -> list<string> {
653653
str replace -r '```nu(shell)?\s*' ''
654654
| split row ','

tests/test_commands.nu

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def "classify-block-action returns print-as-it-is for no-run" [] {
8282
assert equal (classify-block-action "```nushell no-run") "print-as-it-is"
8383
}
8484

85+
@test
86+
def "classify-block-action returns execute for run-once" [] {
87+
assert equal (classify-block-action "```nushell run-once") "execute"
88+
}
89+
90+
@test
91+
def "classify-block-action returns execute for run-once combined with try" [] {
92+
assert equal (classify-block-action "```nushell run-once, try") "execute"
93+
}
94+
8595
@test
8696
def "classify-block-action returns delete for output-numd" [] {
8797
assert equal (classify-block-action "```output-numd") "delete"
@@ -126,6 +136,22 @@ def "extract-fence-options expands short options" [] {
126136
assert ("no-output" in $result)
127137
}
128138

139+
@test
140+
def "extract-fence-options parses run-once" [] {
141+
let result = "```nu run-once" | extract-fence-options
142+
143+
assert equal $result ["run-once"]
144+
}
145+
146+
@test
147+
def "extract-fence-options parses run-once combined with try" [] {
148+
let result = "```nu run-once, try" | extract-fence-options
149+
150+
assert equal ($result | length) 2
151+
assert ("run-once" in $result)
152+
assert ("try" in $result)
153+
}
154+
129155
@test
130156
def "extract-fence-options handles empty options" [] {
131157
let result = "```nushell" | extract-fence-options
@@ -375,6 +401,37 @@ def "generate-timestamp returns correct format" [] {
375401
assert ($result =~ '^\d{8}_\d{6}$')
376402
}
377403

404+
# =============================================================================
405+
# Tests for decorate-original-code-blocks (run-once rewriting)
406+
# =============================================================================
407+
408+
@test
409+
def "decorate-original-code-blocks rewrites run-once to no-run" [] {
410+
let blocks = "```nu run-once\necho hello\n```" | parse-markdown-to-blocks
411+
let result = decorate-original-code-blocks $blocks
412+
413+
assert ($result.0.code =~ '```nu no-run')
414+
assert ($result.0.code !~ 'run-once')
415+
}
416+
417+
@test
418+
def "decorate-original-code-blocks rewrites run-once preserving other options" [] {
419+
let blocks = "```nu run-once, try\necho hello\n```" | parse-markdown-to-blocks
420+
let result = decorate-original-code-blocks $blocks
421+
422+
assert ($result.0.code =~ '```nu no-run, try')
423+
assert ($result.0.code !~ 'run-once')
424+
}
425+
426+
@test
427+
def "decorate-original-code-blocks leaves plain blocks unchanged" [] {
428+
let blocks = "```nu\necho hello\n```" | parse-markdown-to-blocks
429+
let result = decorate-original-code-blocks $blocks
430+
431+
assert ($result.0.code =~ '```nu')
432+
assert ($result.0.code !~ 'no-run')
433+
}
434+
378435
# =============================================================================
379436
# Tests for strip-outputs
380437
# =============================================================================
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# run-once fence option
2+
3+
After numd run, the run-once block below should become no-run with output preserved.
4+
5+
```nu run-once
6+
2 + 2
7+
```
8+
9+
This block uses run-once combined with no-output.
10+
11+
```nu run-once, no-output
12+
3 + 3
13+
```

0 commit comments

Comments
 (0)