Add function to tabulate raw lines
This commit is contained in:
parent
602f7b5299
commit
6587fe2812
|
@ -2,6 +2,9 @@ use math
|
||||||
use re
|
use re
|
||||||
use str
|
use str
|
||||||
use ./common
|
use ./common
|
||||||
|
use ./list
|
||||||
|
use ./map
|
||||||
|
use ./num
|
||||||
|
|
||||||
fn chars {|v|
|
fn chars {|v|
|
||||||
str:split '' $v
|
str:split '' $v
|
||||||
|
@ -150,7 +153,8 @@ fn line {|&sep=' ' props line|
|
||||||
str:join $sep $data
|
str:join $sep $data
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list {|&with-header=$true &csep=' ' &hsep='-' &recompute=$true props data|
|
fn list {|&with-header=$true &csep=' ' &hsep='-' &recompute=$true props @data|
|
||||||
|
set data = (common:to-list $data)
|
||||||
if $recompute {
|
if $recompute {
|
||||||
set props = (update-props $props $data)
|
set props = (update-props $props $data)
|
||||||
}
|
}
|
||||||
|
@ -196,3 +200,22 @@ fn size {|size|
|
||||||
printf '%.1f%s' $e $m[$u]
|
printf '%.1f%s' $e $m[$u]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tabulate {|&cols=[] &sep=' ' @lines|
|
||||||
|
var cs = (count $cols)
|
||||||
|
var @data = (eawk {|_ @parts|
|
||||||
|
var ps = (count $parts)
|
||||||
|
var diff = (- $cs $ps)
|
||||||
|
if (< $diff 0) {
|
||||||
|
use builtin
|
||||||
|
set @cols = $@cols (builtin:repeat (num:neg $diff) [&size=0])
|
||||||
|
set cs = $ps
|
||||||
|
}
|
||||||
|
map:to-map $parts
|
||||||
|
} (common:to-list $@lines))
|
||||||
|
set @cols = (list:foreach {|i c|
|
||||||
|
var a = (common:cexec (has-key $c align) { put $c[align] } left)
|
||||||
|
column &align=$a $i
|
||||||
|
} $cols)
|
||||||
|
list &csep=$sep &with-header=$false $cols $@data
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue