diff options
Diffstat (limited to 'src/soon/columnize.nim')
| -rw-r--r-- | src/soon/columnize.nim | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/src/soon/columnize.nim b/src/soon/columnize.nim index 0fba176..a9074f1 100644 --- a/src/soon/columnize.nim +++ b/src/soon/columnize.nim @@ -1,18 +1,22 @@ -import std/[strutils, enumerate, sequtils] +import std/[strutils, enumerate, sequtils, terminal] + +type Column* = object + text*: string + color*: ForegroundColor type Line* = object - columns*: seq[string] + columns*: seq[Column] attachments*: seq[string] -proc prefixSpaceCount(s: seq[string]): int = - return foldl(s[0..s.len-2], a + b.len, 0) + (2 * s.len-2) +proc prefixSpaceCount(s: seq[Column]): int = + return foldl(s[0..s.len-2], a + b.text.len, 0) + (2 * s.len-2) proc pad(table: seq[Line], max: seq[int]): seq[Line] = for line in table: - var resLine = newSeq[string]() + var resLine = newSeq[Column]() var resAttach = newSeq[string]() for i, field in enumerate(line.columns): - resLine.add(field & repeat(' ', max[i] - field.len)) + resLine.add(Column(text:field.text & repeat(' ', max[i] - field.text.len), color: field.color)) if line.attachments.len > 0: let spaces = prefixSpaceCount(resLine) for attachment in line.attachments: @@ -23,13 +27,19 @@ proc columnize(table: seq[Line]): seq[Line] = var max = newSeq[int](table[0].columns.len) for line in table: for i, field in enumerate(line.columns): - if field.len > max[i]: - max[i] = field.len + if field.text.len > max[i]: + max[i] = field.text.len result = pad(table, max) +proc writeColumns(line: Line) = + for column in line.columns[0 .. ^2]: + stdout.styledWrite(column.color, column.text & " ") + stdout.styledWrite(line.columns[^1].color, line.columns[^1].text) + proc echo*(table: seq[Line]) = let lines = columnize(table) for line in lines: - echo line.columns.join(" ") + writeColumns(line) + stdout.writeLine("") for attachment in line.attachments: - echo attachment + stdout.styledWriteLine(fgMagenta, attachment) |
