# Unified output formatter: --fmt and --clip flags. # All provisioning CLI commands feed their final output through fmt-output. # # Formats: text (default, table rendering), json, yaml, toml, md (markdown table) # --clip: copies rendered string to system clipboard (pbcopy / xclip / clip.exe) # Render a value according to --fmt, optionally copy to clipboard. # value : any Nushell value (record, list, string, table) # fmt : "text" | "json" | "yaml" | "toml" | "md" # clip : when true, copy rendered output to clipboard export def fmt-output [ value: any fmt: string clip: bool ]: nothing -> any { let rendered = (render-fmt $value $fmt) if $clip { copy-to-clipboard $rendered } $rendered } # Render only (no clipboard side-effect). export def render-fmt [value: any, fmt: string]: nothing -> string { match $fmt { "json" => { $value | to json } "yaml" => { $value | to yaml } "toml" => { $value | to toml } "md" => { value-to-md $value } _ => { value-to-text $value } } } def value-to-text [value: any]: nothing -> string { match ($value | describe) { $t if ($t | str starts-with "list") => { $value | table | into string } $t if ($t | str starts-with "record") => { $value | table -e | into string } _ => { $value | into string } } } def value-to-md [value: any]: nothing -> string { match ($value | describe) { $t if ($t | str starts-with "list") => { if ($value | is-empty) { return "" } let cols = ($value | first | columns) let header = ($cols | str join " | ") let sep = ($cols | each { "---" } | str join " | ") let rows = ($value | each {|row| $cols | each {|c| $row | get $c | into string } | str join " | " }) ([$header $sep] ++ $rows) | str join "\n" } $t if ($t | str starts-with "record") => { let keys = ($value | columns) let rows = ($keys | each {|k| $"($k) | ($value | get $k | into string)"}) (["Key | Value" "--- | ---"] ++ $rows) | str join "\n" } _ => { $value | into string } } } def copy-to-clipboard [text: string]: nothing -> nothing { if ($nu.os-info.name == "macos") { let r = (do { ^pbcopy } | complete) if $r.exit_code != 0 { print "(ansi yellow)clipboard copy failed(ansi reset)" } else { $text | ^pbcopy } return } # Linux — try xclip then xsel let r_xclip = (do { ^which xclip } | complete) if $r_xclip.exit_code == 0 { $text | ^xclip -selection clipboard return } let r_xsel = (do { ^which xsel } | complete) if $r_xsel.exit_code == 0 { $text | ^xsel --clipboard --input return } print "(ansi yellow)no clipboard utility found (install xclip or xsel)(ansi reset)" }