provisioning/core/nulib/lib_provisioning/utils/interface.nu

194 lines
5.4 KiB
Plaintext
Raw Normal View History

export def _ansi [
arg?: string
--escape: record
]: nothing -> string {
if ($env | get -o PROVISIONING_NO_TERMINAL | default false) {
""
} else if (is-terminal --stdout) {
if $escape != null {
(ansi --escape $escape)
} else {
(ansi $arg)
}
} else {
""
}
}
export def format_out [
data: string
src?: string
mode?: string
]: nothing -> string {
let msg = match $src {
"json" => ($data | from json),
_ => $data,
}
match $mode {
"table" => {
($msg | table -i false)
},
_ => { $msg }
}
}
export def _print [
data: string
src?: string
context?: string
mode?: string
-n # no newline
]: nothing -> nothing {
let output = ($env | get -o PROVISIONING_OUT| default "")
if $n {
if ($output | is-empty) {
print -n $data
}
return
}
if ($output | is-empty) {
print (format_out $data $src $mode)
} else {
match $output {
"json" => {
if $context != "result" { return }
if $src == "json" {
print ($data)
} else {
print ($data | to json)
}
},
"yaml" | "yml" => {
if $context != "result" { return }
if $src == "json" {
print ($data | from json | to yaml)
} else {
print ($data | to yaml)
}
},
"toml" | "tml" => {
if $context != "result" { return }
if $src == "json" {
print ($data | from json | to toml)
} else {
print ($data)
}
},
"text" | "txt" => {
if $context != "result" { return }
print (format_out $data $src $mode)
},
_ => {
if ($output | str ends-with ".json" ) {
if $context != "result" { return }
(if $src == "json" {
($data)
} else {
($data | to json)
} | save --force $output)
} else if ($output | str ends-with ".yaml" ) {
if $context != "result" { return }
(if $src == "json" {
($data | from json | to yaml)
} else {
($data | to yaml)
} | save --force $output)
} else if ($output | str ends-with ".toml" ) {
if $context != "result" { return }
(if $src == "json" {
($data | from json | to toml)
} else {
($data)
} | save --force $output)
} else if ($output | str ends-with ".text" ) or ($output | str ends-with ".txt" ) {
if $context != "result" { return }
format_out $data $src $mode | save --force $output
} else {
format_out $data $src $mode | save --append $output
}
}
}
}
}
export def end_run [
context: string
]: nothing -> nothing {
if ($env.PROVISIONING_OUT | is-not-empty) { return }
if ($env.PROVISIONING_NO_TITLES? | default false) { return false }
if (detect_claude_code) { return false }
if $env.PROVISIONING_DEBUG {
_print $"\n(_ansi blue)----πŸŒ₯ ----πŸŒ₯ ----πŸŒ₯ ---- oOo ----πŸŒ₯ ----πŸŒ₯ ----πŸŒ₯ ---- (_ansi reset)"
} else {
let the_context = if $context != "" { $" to ($context)" } else { "" }
if (is-terminal --stdout) {
_print $"\n(_ansi cyan)Thanks for using (_ansi blue_bold)($env.PROVISIONING_URL | ansi link --text 'Provisioning')(_ansi reset)"
if $the_context != "" {
_print $"(_ansi yellow_dimmed)($the_context)(_ansi reset)"
}
_print ($env.PROVISIONING_URL | ansi link --text $"(_ansi default_dimmed)Click here for more info or visit \n($env.PROVISIONING_URL)(_ansi reset)")
} else {
_print $"\n(_ansi cyan)Thanks for using (_ansi blue_bold) Provisioning [($env.PROVISIONING_URL)](_ansi reset)($the_context)"
_print $"(_ansi default_dimmed)For more info or visit ($env.PROVISIONING_URL)(_ansi reset)"
}
}
}
export def show_clip_to [
msg: string
show: bool
]: nothing -> nothing {
if $show { _print $msg }
if (is-terminal --stdout) {
clip_copy $msg $show
}
}
export def log_debug [
msg: string
]: nothing -> nothing {
use std
std log debug $msg
# std assert (1 == 1)
}
#// Examples:
#// desktop_run_notify "Port scan" "Done" { port scan 8.8.8.8 53 }
#// desktop_run_notify "Task try" "Done" --timeout 5sec
export def desktop_run_notify [
title: string
body: string
task?: closure
--timeout: duration
--icon: string
] {
let icon_path = if $icon == null {
$env.PROVISIONING_NOTIFY_ICON
} else { $icon }
let time_out = if $timeout == null {
8sec
} else { $timeout }
if $task != null {
let start = date now
let result = do $task
let end = date now
let total = $end - $start | format duration sec
let result_typ = ($result | describe)
let msg = if $result_typ == "bool" {
(if $result { "βœ… done " } else { $"πŸ›‘ fail "})
} else if ($result_typ | str starts-with "record") {
(if $result.status { "βœ… done " } else { $"πŸ›‘ fail ($result.error)" })
} else { "" }
let time_body = $"($body) ($msg) finished in ($total) "
( notify_msg $title $body $icon_path $time_body $timeout $task )
return $result
} else {
( notify_msg $title $body $icon_path "" $timeout $task )
true
}
}
export def detect_claude_code []: nothing -> bool {
let claudecode = ($env.CLAUDECODE? | default "" | str contains "1")
let entrypoint = ($env.CLAUDE_CODE_ENTRYPOINT? | default "" | str contains "cli")
$claudecode or $entrypoint
}