diff options
author | bptato <nincsnevem662@gmail.com> | 2021-11-16 19:23:31 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2021-11-19 21:52:19 +0100 |
commit | cd28af13fc6753792b3b9571abba5943f90d021e (patch) | |
tree | 5b7c829f70fe8bf81b11b959c1ea92e34db125d5 /src/io/cell.nim | |
parent | bbd416f5ffba5ba5e5d41869042cef1f85323146 (diff) | |
download | chawan-cd28af13fc6753792b3b9571abba5943f90d021e.tar.gz |
Various performance optimizations (...part two)
Diffstat (limited to 'src/io/cell.nim')
-rw-r--r-- | src/io/cell.nim | 81 |
1 files changed, 52 insertions, 29 deletions
diff --git a/src/io/cell.nim b/src/io/cell.nim index 40ce2c57..74b07353 100644 --- a/src/io/cell.nim +++ b/src/io/cell.nim @@ -21,15 +21,12 @@ type formatting*: Formatting nodes*: seq[Node] - FlexibleCell* = object of Cell - rune*: Rune - - FormattingCell = object of Cell - pos: int + FormattingCell* = object of Cell + pos*: int FlexibleLine* = object str*: string - formats: seq[FormattingCell] + formats*: seq[FormattingCell] FlexibleGrid* = seq[FlexibleLine] @@ -38,6 +35,12 @@ type FixedGrid* = seq[FixedCell] +#TODO ????? +func `==`*(a: FixedCell, b: FixedCell): bool = + return a.formatting == b.formatting and + a.runes == b.runes and + a.nodes == b.nodes + func newFixedGrid*(w: int, h: int = 1): FixedGrid = return newSeq[FixedCell](w * h) @@ -53,8 +56,7 @@ func len*(line: FlexibleLine): int = func newFormatting*(): Formatting = return Formatting(fgcolor: defaultColor, bgcolor: defaultColor) -func `[]`*(line: FlexibleLine, pos: int): FlexibleCell = - result.rune = line.str.runeAtPos(pos) +func findFormat*(line: FlexibleLine, pos: int): FormattingCell = var i = 0 while i < line.formats.len: if line.formats[i].pos > pos: @@ -62,26 +64,49 @@ func `[]`*(line: FlexibleLine, pos: int): FlexibleCell = inc i dec i if i != -1: - result.formatting = line.formats[i].formatting + result = line.formats[i] else: - result.formatting = newFormatting() + result.pos = -1 -func subLine*(line: FlexibleLine, i: int): FlexibleLine = - for f in line.formats: - if f.pos >= i: - result.formats.add(f) - result.str = line.str.runeSubstr(i) +func findNextFormat*(line: FlexibleLine, pos: int): FormattingCell = + var i = 0 + while i < line.formats.len: + if line.formats[i].pos > pos: + break + inc i + if i < line.formats.len: + result = line.formats[i] + else: + result.pos = -1 -proc setLen*(line: var FlexibleLine, i: int) = - var nf = newSeq[FormattingCell]() - for f in line.formats: - if f.pos < i: - nf.add(f) - line.str = line.str.runeSubstr(0, i) +func subformats*(formats: seq[FormattingCell], pos: int): seq[FormattingCell] = + var i = 0 + while i < formats.len: + if formats[i].pos >= pos: + if result.len == 0 and i > 0: + var f = formats[i - 1] + f.pos = 0 + result.add(f) + var f = formats[i] + f.pos -= pos + result.add(f) + inc i + + if result.len == 0 and i > 0: + var f = formats[i - 1] + f.pos = 0 + result.add(f) + +proc setLen*(line: var FlexibleLine, len: int) = + for i in 0 ..< line.formats.len: + if line.formats[i].pos >= len: + line.formats.setLen(i) + break + #line.formats = line.formats.filter((x) => x.pos < len) proc add*(a: var FlexibleLine, b: FlexibleLine) = let l = a.len - a.formats.add(b.formats.map((x) => FormattingCell(formatting: x.formatting, pos: l + x.pos))) + a.formats.add(b.formats.map((x) => FormattingCell(formatting: x.formatting, nodes: x.nodes, pos: l + x.pos))) a.str &= b.str proc addLine*(grid: var FlexibleGrid) = @@ -90,16 +115,14 @@ proc addLine*(grid: var FlexibleGrid) = proc addFormat*(grid: var FlexibleGrid, y: int, format: Formatting) = grid[y].formats.add(FormattingCell(formatting: format, pos: grid[y].len)) -proc addCell*(grid: var FlexibleGrid, y: int, r: Rune) = - grid[y].str &= $r +proc addFormat*(grid: var FlexibleGrid, y: int, pos: int, format: Formatting, nodes: seq[Node]) = + grid[y].formats.add(FormattingCell(formatting: format, nodes: nodes, pos: pos)) -proc addCell*(grid: var FlexibleGrid, y: int, r: Rune, format: Formatting) = - if grid[y].formats.len == 0 or grid[y].formats[^1].formatting != format: - grid[y].formats.add(FormattingCell(formatting: format, pos: grid[y].len)) +proc addCell*(grid: var FlexibleGrid, y: int, r: Rune) = grid[y].str &= $r -proc addCell*(grid: var FlexibleGrid, r: Rune, format: Formatting) = - grid.addCell(grid.len - 1, r, format) +proc addCell*(grid: var FlexibleGrid, r: Rune) = + grid.addCell(grid.len - 1, r) template inc_check(i: int) = inc i |