diff options
Diffstat (limited to 'src/soon/columnize.nim')
| -rw-r--r-- | src/soon/columnize.nim | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/soon/columnize.nim b/src/soon/columnize.nim new file mode 100644 index 0000000..4ec87af --- /dev/null +++ b/src/soon/columnize.nim @@ -0,0 +1,20 @@ +import std/[strutils, enumerate] + +proc pad(table: seq[seq[string]], max: seq[int]): seq[seq[string]] = + for line in table: + var resLine = newSeq[string]() + for i, field in enumerate(line): + resLine.add(field & repeat(' ', max[i] - field.len)) + result.add(resLine) + +proc columnize(table: seq[seq[string]]): seq[seq[string]] = + var max = newSeq[int](table[0].len) + for line in table: + for i, field in enumerate(line): + if field.len > max[i]: + max[i] = field.len + result = pad(table, max) + +proc echo*(table: seq[seq[string]]) = + for line in columnize(table): + echo line.join(" ") |
