1# 012 in nushell pipelines: `sheet` opens the table piped to it in 0122# and returns what you send back, with its types.3#4# Install: 012 nu --install-module, then add `use scripts/012.nu *` to5# config.nu. See https://github.com/FelineStateMachine/012 (docs/nushell).67# Open a table in 012, and return the table sent back on quitting.8#9# The table piped in opens as a sheet, or the file named instead. Edit10# it, sort it or select a range; quitting asks whether to send the11# selection or the whole sheet back as NUON, so file sizes, durations12# and dates come back as themselves. With --send, quitting sends13# without asking. Quitting without sending raises an error, so `try`14# can catch it. Text piped in (CSV, TSV, JSON, NUON) is read as 01215# reads it.16@example "Pick files in 012 and keep filtering" { ls | sheet | where size > 1kb }17@example "Edit the table and get all of it back when you quit" { ls | sheet --send sheet }18@example "Get back the range selected when you quit, its first row naming the columns" { ls | select name size | sheet --send selection }19@example "Edit a workbook and save what you send back as CSV" { sheet budget.xlsx | save -f budget.csv }20@example "Carry on when nothing is sent back" { try { ps | sheet } catch { [] } }21@search-terms [spreadsheet table edit 012]22export def sheet [23 file?: path # open this workbook or file instead of the input24 --send: string # send without asking on quitting: selection (the sheet when one cell is selected) or sheet25] {26 let input = $in27 if $send != null and $send not-in [selection sheet] {28 error make {29 msg: $"--send ($send): sheet sends selection or sheet"30 label: {text: "selection or sheet", span: (metadata $send).span}31 }32 }33 let send_args = if $send == null { [] } else { [--send $send] }34 let file_args = if $file == null { [] } else { [$file] }35 let args = [--pipe --to nuon ...$send_args ...$file_args]36 let r = if $input == null {37 ^012 ...$args | complete38 } else if ($input | describe) in [string binary] {39 $input | ^012 ...$args | complete40 } else {41 $input | to nuon | ^012 ...$args | complete42 }43 if $r.exit_code != 0 {44 let why = $r.stderr | str trim | str replace --regex '^012: ' ''45 error make {46 msg: (if $why == "" { $"012 exited with status ($r.exit_code)" } else { $why })47 help: (if $send == null { "nothing was sent back; quit with Enter or S to send a table" } else { $"nothing was sent back; Ctrl+Q sends the ($send)" })48 }49 }50 $r.stdout | from nuon51}5253# Look at a table in 012, then carry on with nothing.54#55# Like `sheet`, without sending anything back: the table stays in 01256# until you save or download it.57@example "Look at the processes" { ps | sheet view }58@search-terms [spreadsheet table view 012]59export def "sheet view" [] {60 let input = $in61 if ($input | describe) in [string binary] {62 $input | ^012 -63 } else {64 $input | to nuon | ^012 -65 }66 null67}6869# Open a nushell notebook in 012.70@example "Open a notebook" { sheet nu work.012 }71export def "sheet nu" [72 file?: path # the workbook to open73] {74 if $file == null { ^012 nu } else { ^012 nu $file }75}