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.

129 lines
3.1 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
}
export def `follow nuon-lines` [] {
lines | each {|it| try { $it | from nuon } catch { {msg: $it} } | table --expand | print} | ignore
}
export def `diff-all` [...files] {
if ($files | length) < 2 {
error make { msg: "you have to provide at least two files"}
}
let diffs = $files | window 2
mut index = 0
loop {
difft --context 100 ...($diffs | get $index)
match (input -n 1) {
"n" => {
$index = $index + 1
},
"p" => {
$index = $index - 1
},
"q" => {
break
}
}
if $index > ($diffs | length) - 1 {
$index -= 1;
} else if $index < 0 {
$index = 0
}
}
}
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
}
}
}