2020-07-22 14:35:25 +00:00
|
|
|
use str
|
|
|
|
use ./list
|
|
|
|
|
|
|
|
fn -repeat [n s]{
|
|
|
|
repeat $n $s | str:join ''
|
|
|
|
}
|
|
|
|
|
|
|
|
fn left [size s]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var ss = (count $s)
|
|
|
|
var d = (- $size $ss)
|
2020-07-22 14:35:25 +00:00
|
|
|
if (<= $d 0) {
|
|
|
|
put $s[:$size]
|
|
|
|
} else {
|
|
|
|
put $s(-repeat $d ' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn right [size s]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var ss = (count $s)
|
|
|
|
var d = (- $size $ss)
|
2020-07-22 14:35:25 +00:00
|
|
|
if (<= $d 0) {
|
|
|
|
put $s[:$size]
|
|
|
|
} else {
|
|
|
|
put (-repeat $d ' ')$s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn center [size s]{
|
|
|
|
use math
|
2021-08-27 20:06:49 +00:00
|
|
|
var ss = (count $s)
|
|
|
|
var d = (- $size $ss)
|
2020-07-22 14:35:25 +00:00
|
|
|
if (<= $d 0) {
|
2021-08-27 20:06:49 +00:00
|
|
|
var l = (math:ceil (/ -$d 2))
|
|
|
|
var r = (- (+ $ss $d) $l)
|
2020-07-22 14:35:25 +00:00
|
|
|
put $s[$l':'$r]
|
|
|
|
} else {
|
2021-08-27 20:06:49 +00:00
|
|
|
var l = (math:ceil (/ $d 2))
|
|
|
|
var r = (- $d $l)
|
2020-07-22 14:35:25 +00:00
|
|
|
put (-repeat $l ' ')$s(-repeat $r ' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn -c2map [col]{
|
|
|
|
if (not-eq (kind-of $col) list) {
|
|
|
|
put []
|
|
|
|
return
|
|
|
|
}
|
|
|
|
list:loop [i c]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var k = (kind-of $c)
|
|
|
|
var cf = [&min=0]
|
2020-07-22 14:35:25 +00:00
|
|
|
if (is $k string) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set cf[min] = $c
|
2020-07-22 14:35:25 +00:00
|
|
|
} elif (is $k map) {
|
|
|
|
if (has-key $c min) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set cf[min] = $c[min]
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
if (has-key $c align) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set cf[align] = $c[align]
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 18:49:48 +00:00
|
|
|
set col[$i] = $cf
|
2020-07-22 14:35:25 +00:00
|
|
|
} $col
|
|
|
|
put $col
|
|
|
|
}
|
|
|
|
|
|
|
|
fn -parsecols [col &split=$false lst]{
|
2021-10-13 18:49:48 +00:00
|
|
|
set col = (-c2map $col)
|
2021-08-27 20:06:49 +00:00
|
|
|
var cc lines = (count $col) $lst
|
2020-07-22 14:35:25 +00:00
|
|
|
if $split {
|
2021-10-13 18:49:48 +00:00
|
|
|
set lines = [ (eawk [_ @args]{ put $args } $lst) ]
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
each [l]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var cl = (count $l)
|
2020-07-22 14:35:25 +00:00
|
|
|
if (> $cl $cc) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set col = [ $@col (range (- $cl $cc) | each [_]{ put [&align=left &min=0] }) ]
|
|
|
|
set cc = $cl
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
list:loop [i c]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var ccl = (+ (count $c) 1)
|
2020-07-22 14:35:25 +00:00
|
|
|
if (> $ccl $col[$i][min]) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set col[$i][min] = $ccl
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
} $l
|
|
|
|
} $lines
|
|
|
|
put $col
|
|
|
|
}
|
|
|
|
|
|
|
|
fn -format [f e]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var cb = $left~
|
2020-07-22 14:35:25 +00:00
|
|
|
if (has-key $f align) {
|
2021-08-27 20:06:49 +00:00
|
|
|
var a = $f[align]
|
2020-07-22 14:35:25 +00:00
|
|
|
if (is $a right) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set cb = $right~
|
2020-07-22 14:35:25 +00:00
|
|
|
} elif (is $a center) {
|
2021-10-13 18:49:48 +00:00
|
|
|
set cb = $center~
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$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]{
|
2021-08-27 20:06:49 +00:00
|
|
|
var lines = (list:-p $@lst)
|
2020-07-22 14:35:25 +00:00
|
|
|
if $split {
|
2021-10-13 18:49:48 +00:00
|
|
|
set lines = [ (eawk [l @args]{ put $args } $lines) ]
|
2020-07-22 14:35:25 +00:00
|
|
|
}
|
2021-10-13 18:49:48 +00:00
|
|
|
set col = (-parsecols $col $lines)
|
2020-07-22 14:35:25 +00:00
|
|
|
each [l]{ -formatline $col $l } $lines
|
|
|
|
}
|