diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2019-01-09 00:46:44 -0800 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-01-09 09:46:44 +0100 |
commit | 23c1ee982e2d3795001879a4527581f33875cd33 (patch) | |
tree | 799acb5b12ca6f24796213a54d4ed9baf6d70beb /compiler | |
parent | 258952832f312a25d0ab9771237a547297b336b8 (diff) | |
download | Nim-23c1ee982e2d3795001879a4527581f33875cd33.tar.gz |
add `alignTable`, `parseTableCells` to align/format a tab(etc) delimited table (#10182)
* add compiler/unittest_light.nim for easy diffing: assertEquals and mismatch * fixup * add alignTable, parseTableCells
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/asciitables.nim | 83 | ||||
-rw-r--r-- | compiler/unittest_light.nim | 37 |
2 files changed, 120 insertions, 0 deletions
diff --git a/compiler/asciitables.nim b/compiler/asciitables.nim new file mode 100644 index 000000000..c25d54bde --- /dev/null +++ b/compiler/asciitables.nim @@ -0,0 +1,83 @@ +#[ +move to std/asciitables.nim once stable, or to a nimble paackage +once compiler can depend on nimble +]# + +type Cell* = object + text*: string + width*, row*, col*, ncols*, nrows*: int + +iterator parseTableCells*(s: string, delim = '\t'): Cell = + ## iterates over all cells in a `delim`-delimited `s`, after a 1st + ## pass that computes number of rows, columns, and width of each column. + var widths: seq[int] + var cell: Cell + template update() = + if widths.len<=cell.col: + widths.setLen cell.col+1 + widths[cell.col] = cell.width + else: + widths[cell.col] = max(widths[cell.col], cell.width) + cell.width = 0 + + for a in s: + case a + of '\n': + update() + cell.col = 0 + cell.row.inc + elif a == delim: + update() + cell.col.inc + else: + # todo: consider multi-width chars when porting to non-ascii implementation + cell.width.inc + if s.len > 0 and s[^1] != '\n': + update() + + cell.ncols = widths.len + cell.nrows = cell.row + 1 + cell.row = 0 + cell.col = 0 + cell.width = 0 + + template update2() = + cell.width = widths[cell.col] + yield cell + cell.text = "" + cell.width = 0 + cell.col.inc + + template finishRow() = + for col in cell.col..<cell.ncols: + cell.col = col + update2() + cell.col = 0 + + for a in s: + case a + of '\n': + finishRow() + cell.row.inc + elif a == delim: + update2() + else: + cell.width.inc + cell.text.add a + + if s.len > 0 and s[^1] != '\n': + finishRow() + +proc alignTable*(s: string, delim = '\t', fill = ' ', sep = " "): string = + ## formats a `delim`-delimited `s` representing a table; each cell is aligned + ## to a width that's computed for each column; consecutive columns are + ## delimted by `sep`, and alignment space is filled using `fill`. + ## More customized formatting can be done by calling `parseTableCells` directly. + for cell in parseTableCells(s, delim): + result.add cell.text + for i in cell.text.len..<cell.width: + result.add fill + if cell.col < cell.ncols-1: + result.add sep + if cell.col == cell.ncols-1 and cell.row < cell.nrows - 1: + result.add '\n' diff --git a/compiler/unittest_light.nim b/compiler/unittest_light.nim new file mode 100644 index 000000000..bcba6f7c7 --- /dev/null +++ b/compiler/unittest_light.nim @@ -0,0 +1,37 @@ +# note: consider merging tests/assert/testhelper.nim here. + +proc mismatch*[T](lhs: T, rhs: T): string = + ## Simplified version of `unittest.require` that satisfies a common use case, + ## while avoiding pulling too many dependencies. On failure, diagnostic + ## information is provided that in particular makes it easy to spot + ## whitespace mismatches and where the mismatch is. + proc replaceInvisible(s: string): string = + for a in s: + case a + of '\n': result.add "\\n\n" + else: result.add a + + proc quoted(s: string): string = result.addQuoted s + + result.add "\n" + result.add "lhs:{\n" & replaceInvisible( + $lhs) & "}\nrhs:{\n" & replaceInvisible($rhs) & "}\n" + when compiles(lhs.len): + if lhs.len != rhs.len: + result.add "lhs.len: " & $lhs.len & " rhs.len: " & $rhs.len & "\n" + when compiles(lhs[0]): + var i = 0 + while i < lhs.len and i < rhs.len: + if lhs[i] != rhs[i]: break + i.inc + result.add "first mismatch index: " & $i & "\n" + if i < lhs.len and i < rhs.len: + result.add "lhs[i]: {" & quoted($lhs[i]) & "} rhs[i]: {" & quoted( + $rhs[i]) & "}" + result.add "lhs[0..<i]:{\n" & replaceInvisible($lhs[ + 0..<i]) & "}\nrhs[0..<i]:{\n" & replaceInvisible($rhs[0..<i]) & "}" + +proc assertEquals*[T](lhs: T, rhs: T) = + when false: # can be useful for debugging to see all that's fed to this. + echo "----" & $lhs + doAssert lhs==rhs, mismatch(lhs, rhs) |