diff --git a/aliases/digall.elv b/aliases/digall.elv index 0bb8ff2..c7b7943 100644 --- a/aliases/digall.elv +++ b/aliases/digall.elv @@ -1,6 +1,7 @@ #alias:new digall e:dig ANY +noall +answer fn digall [@_args]{ use moi/util/list + use moi/util/strutil use str local:default_records = [ @@ -15,6 +16,11 @@ fn digall [@_args]{ NS SOA ] + local:cols = [ + [&min=24] + [&min=8] + [&min=10] + ] local:records = [] local:servers = [] local:flags = [ @@ -44,21 +50,32 @@ fn digall [@_args]{ records = $default_records } } + + fn format [results]{ + eawk [_ @args]{ + put [ $args[0] (all $args[2:]) ] + } $results | list:remove-duplicate | list:sort [l1 l2]{ + local:c = (str:compare $l1[0] $l2[0]) + if (== $c 0) { + c = (str:compare $l1[2] $l2[2]) + } + } | strutil:format &col=$cols | str:join "\n" + } fn drillr [d @args]{ local:results = [ (peach [r]{ e:drill $@args $r $d | list:filter [e]{ and (> (count $e) 2) (not (is $e[:2] ';;')) } - } $records | list:remove-duplicate) ] - echo (str:join "\n" $results) + } $records) ] + format $results } fn digr [d @args]{ local:results = [ (peach [r]{ e:dig $@args $r $d $@flags - } $records | list:remove-duplicate) ] - echo (str:join "\n" $results) + } $records) ] + format $results } fn req [d @args]{ @@ -70,21 +87,22 @@ fn digall [@_args]{ } fn digs [s d]{ - echo (styled $s bright-yellow) + put (echo (styled $s bright-yellow)) req $d $s } fn digd [d]{ - echo (styled 'Domaine: '$d bright-green) + local:results = [ (echo (styled 'Domaine: '$d bright-green)) ] if (> (count $servers) 0) { each [s]{ - digs $s $d + @results = $@results (digs $s $d) } $servers } else { - req $d + @results = $@results (req $d) } + echo (str:join "\n" $results) } init - each $digd~ $domains + peach $digd~ $domains } diff --git a/lib/moi/util/strutil.elv b/lib/moi/util/strutil.elv new file mode 100644 index 0000000..1e57f91 --- /dev/null +++ b/lib/moi/util/strutil.elv @@ -0,0 +1,115 @@ +use str +use ./list + +fn -repeat [n s]{ + repeat $n $s | str:join '' +} + +fn left [size s]{ + local:ss = (count $s) + local:d = (- $size $ss) + if (<= $d 0) { + put $s[:$size] + } else { + put $s(-repeat $d ' ') + } +} + +fn right [size s]{ + local:ss = (count $s) + local:d = (- $size $ss) + if (<= $d 0) { + put $s[:$size] + } else { + put (-repeat $d ' ')$s + } +} + +fn center [size s]{ + use math + local:ss = (count $s) + local:d = (- $size $ss) + if (<= $d 0) { + local:l = (math:ceil (/ -$d 2)) + local:r = (- (+ $ss $d) $l) + put $s[$l':'$r] + } else { + local:l = (math:ceil (/ $d 2)) + local:r = (- $d $l) + put (-repeat $l ' ')$s(-repeat $r ' ') + } +} + +fn -c2map [col]{ + if (not-eq (kind-of $col) list) { + put [] + return + } + list:loop [i c]{ + local:k = (kind-of $c) + local:cf = [&min=0] + if (is $k string) { + cf[min] = $c + } elif (is $k map) { + if (has-key $c min) { + cf[min] = $c[min] + } + if (has-key $c align) { + cf[align] = $c[align] + } + } + col[$i] = $cf + } $col + put $col +} + +fn -parsecols [col &split=$false lst]{ + col = (-c2map $col) + local:cc = (count $col) + local:lines = $lst + if $split { + lines = [ (eawk [_ @args]{ put $args } $lst) ] + } + each [l]{ + local:cl = (count $l) + if (> $cl $cc) { + col = [ $@col (range (- $cl $cc) | each [_]{ put [&align=left &min=0] }) ] + cc = $cl + } + list:loop [i c]{ + local:ccl = (+ (count $c) 1) + if (> $ccl $col[$i][min]) { + col[$i][min] = $ccl + } + } $l + } $lines + put $col +} + +fn -format [f e]{ + local:cb = $left~ + if (has-key $f align) { + local:a = $f[align] + if (is $a right) { + cb = $right~ + } elif (is $a center) { + cb = $center~ + } + } + $cb $f[min] $e +} + +fn -formatline [col line]{ + list:loop [i v]{ + -format $col[$i] $v + } $line | str:join '' +} + +fn format [&col=[] &split=$false @lst]{ + local:lines = (list:-p $@lst) + if $split { + lines = [ (eawk [l @args]{ put $args } $lines) ] + } + col = (-parsecols $col $lines) + each [l]{ -formatline $col $l } $lines +}