Add function to tabulate raw lines

This commit is contained in:
Benjamin VAUDOUR 2021-12-27 16:22:18 +00:00
parent 602f7b5299
commit 6587fe2812
1 changed files with 24 additions and 1 deletions

View File

@ -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
}