From 6587fe28126cb43cd6f3526b35b0ea1c4afc5cac Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR <60374-benjamin.vaudour@users.noreply.framagit.org> Date: Mon, 27 Dec 2021 16:22:18 +0000 Subject: [PATCH] Add function to tabulate raw lines --- mods/format.elv | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 +}