RosettaCodeData/Task/Align-columns/Zkl/align-columns-1.zkl

17 lines
523 B
Plaintext

fcn format(text,how){
words:=text.split("$").apply("split").flatten();
max:=words.reduce(fcn(p,n){ n=n.len(); n>p and n or p },0);
wordsPerCol:=80/(max+1);
fmt:=(switch(how){
case(-1){ "%%-%ds ".fmt(max).fmt }
case(0) { fcn(max,w){
a:=(max-w.len())/2; b:=max-w.len() - a; String(" "*a,w," "*b);
}.fp(max)
}
case(1){ "%%%ds ".fmt(max).fmt }
});
w:=words.walker(); d:=Data(0,Int);
do{ w.pump(wordsPerCol,d,fmt).append("\n") } while(not w.atEnd);
d.text;
}