You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB
Plaintext

# convert json log entries into an array of nu log entries
export def `from json-lines` [] {
lines | each {|it| try { $it | from json } catch { {msg: $it} }}
}
export def `follow json-lines` [] {
lines | each {|it| try { $it | from json } catch { {msg: $it} } | table --expand | print} | ignore
}
7 months ago
export def `follow nuon-lines` [] {
lines | each {|it| try { $it | from nuon } catch { {msg: $it} } | table --expand | print} | ignore
}
7 months ago
def --wrapped with-flag [...flag] {
if ($in | is-empty) { [] } else { [...$flag $in] }
}
def --wrapped with-switch [...flag] {
if ($in) { [...$flag] } else { [] }
}
def --wrapped with-name [...name] {
if not $in or ($in | is-empty) { [] } else { [...$name]}
}
# returns the word count of the given file
export def wc [
--bytes (-c) # print the byte counts
--lines (-l) # count lines instead of words
--chars (-m) # print the character counts
--max-line-length (-L) # print the maximum display length
--words (-w) # print the word counts
...files
] {
let c = $bytes | with-switch -c
let l = $lines | with-switch -l
let m = $chars | with-switch -m
let L = $max_line_length | with-switch -L
let w = $words | with-switch -w
( ^wc ...([$c $l $m $L $w] | flatten) ...$files
| detect columns --no-headers
| rename ...([
($bytes | with-name "bytes")
($lines | with-name "lines")
($chars | with-name "chars")
($max_line_length | with-name "max_line_length")
($words | with-name "words")
file
] | flatten )
)
}
export def --wrapped df [...$args] {
if (["--help", "--version"] | any {|it| $it in $args }) {
^df ...$args
} else {
^df ...$args | detect columns --guess
}
}
export def --env dotenv [file=".env"] {
if not ($file | path exists) {
err make { msg: $"env file ($file) not found "}
}
( open $file
| lines
| each { parse "{key}={value}" | first | { $in.key: $in.value} }
| reduce --fold {} {|it, acc| $acc | merge $it }
| load-env $in
)
}
export def brute-force [
closure: closure, # closure to execute
--delay(-d): duration = 1sec, # delay between trys
--max-trys: number # maximum number of trys. Default unlimited
--print-errors, # print errors to stderr
] {
mut count = 0;
loop {
$count += 1
let iteration = $count
try {
return (do -c $closure)
} catch {|e|
if $print_errors {
print -e $e.msg
}
if $max_trys != null and $iteration >= $max_trys {
error make $e
}
sleep $delay
}
}
}