diff --git a/mods/format.elv b/mods/format.elv index ffebb78..3d033ac 100644 --- a/mods/format.elv +++ b/mods/format.elv @@ -2,6 +2,9 @@ use math use re use str use ./common +use ./list +use ./map +use ./num fn chars {|v| str:split '' $v @@ -150,7 +153,8 @@ fn line {|&sep=' ' props line| 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 { set props = (update-props $props $data) } @@ -196,3 +200,22 @@ fn size {|size| 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 +}