diff options
author | bptato <nincsnevem662@gmail.com> | 2021-11-15 23:16:03 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2021-11-19 21:51:47 +0100 |
commit | bbd416f5ffba5ba5e5d41869042cef1f85323146 (patch) | |
tree | d0ea32286e9cd3633d84c7a1a64a33ecde8db68e /src/io | |
parent | d3d9139dd6aa535ed1d7f2c78accd6ac5fa89b3e (diff) | |
download | chawan-bbd416f5ffba5ba5e5d41869042cef1f85323146.tar.gz |
Operation "get rid of FlexibleCell" part one
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/buffer.nim | 12 | ||||
-rw-r--r-- | src/io/cell.nim | 72 |
2 files changed, 55 insertions, 29 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim index 7fa926f7..9196504b 100644 --- a/src/io/buffer.nim +++ b/src/io/buffer.nim @@ -3,7 +3,6 @@ import uri import strutils import unicode -import types/color import types/enums import css/style import utils/twtstr @@ -221,7 +220,6 @@ proc refreshDisplay*(buffer: Buffer) = inc n buffer.display[dls + j - n].runes.add(line[i].rune) buffer.display[dls + j - n].formatting = line[i].formatting - buffer.display[dls + j - n].nodes = line[i].nodes j += line[i].rune.width() inc i @@ -534,8 +532,6 @@ proc refreshTermAttrs*(buffer: Buffer): bool = return true return false -proc setText*(buffer: Buffer, x: int, y: int, text: seq[Rune]) = buffer.lines.setText(x, y, text) - func formatFromLine(line: CSSRowBox): Formatting = result.fgcolor = line.color.cellColor() if line.fontstyle in { FONT_STYLE_ITALIC, FONT_STYLE_OBLIQUE }: @@ -566,7 +562,7 @@ proc setRowBox(buffer: Buffer, line: CSSRowBox) = cx += buffer.lines[y][i].rune.width() inc i - let oline = buffer.lines[y][i..high(buffer.lines[y])] + var oline = buffer.lines[y].subline(i) buffer.lines[y].setLen(i) var j = 0 var nx = cx @@ -576,8 +572,9 @@ proc setRowBox(buffer: Buffer, line: CSSRowBox) = buffer.lines.addCell(y, Rune(' ')) inc nx + buffer.lines.addFormat(y, format) while j < line.runes.len: - buffer.lines.addCell(y, line.runes[j], format, line.nodes) + buffer.lines.addCell(y, line.runes[j]) nx += line.runes[j].width() inc j @@ -587,7 +584,8 @@ proc setRowBox(buffer: Buffer, line: CSSRowBox) = inc i if i < oline.len: - buffer.lines[y].add(oline[i..high(oline)]) + let nol = oline.subLine(i) + buffer.lines[y].add(nol) proc updateCursor(buffer: Buffer) = if buffer.fromy > buffer.lastVisibleLine - 1: diff --git a/src/io/cell.nim b/src/io/cell.nim index e1b2aef4..40ce2c57 100644 --- a/src/io/cell.nim +++ b/src/io/cell.nim @@ -24,7 +24,12 @@ type FlexibleCell* = object of Cell rune*: Rune - FlexibleLine* = seq[FlexibleCell] + FormattingCell = object of Cell + pos: int + + FlexibleLine* = object + str*: string + formats: seq[FormattingCell] FlexibleGrid* = seq[FlexibleLine] @@ -37,41 +42,64 @@ func newFixedGrid*(w: int, h: int = 1): FixedGrid = return newSeq[FixedCell](w * h) func width*(line: FlexibleLine): int = - for c in line: - result += c.rune.width() + return line.str.width() func width*(cell: FixedCell): int = return cell.runes.width() +func len*(line: FlexibleLine): int = + return line.str.runeLen() + func newFormatting*(): Formatting = return Formatting(fgcolor: defaultColor, bgcolor: defaultColor) +func `[]`*(line: FlexibleLine, pos: int): FlexibleCell = + result.rune = line.str.runeAtPos(pos) + var i = 0 + while i < line.formats.len: + if line.formats[i].pos > pos: + break + inc i + dec i + if i != -1: + result.formatting = line.formats[i].formatting + else: + result.formatting = newFormatting() + +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) + +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) + +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.str &= b.str + proc addLine*(grid: var FlexibleGrid) = - grid.add(newSeq[FlexibleCell]()) + grid.add(FlexibleLine()) + +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].add(FlexibleCell(rune: r)) + grid[y].str &= $r proc addCell*(grid: var FlexibleGrid, y: int, r: Rune, format: Formatting) = - grid[y].add(FlexibleCell(rune: r, formatting: format)) - -proc addCell*(grid: var FlexibleGrid, y: int, r: Rune, format: Formatting, nodes: seq[Node]) = - grid[y].add(FlexibleCell(rune: r, formatting: format, nodes: nodes)) + if grid[y].formats.len == 0 or grid[y].formats[^1].formatting != format: + grid[y].formats.add(FormattingCell(formatting: format, pos: grid[y].len)) + grid[y].str &= $r proc addCell*(grid: var FlexibleGrid, r: Rune, format: Formatting) = - grid[^1].add(FlexibleCell(rune: r, formatting: format)) - -proc setText*(grid: var FlexibleGrid, x: int, y: int, text: seq[Rune]) = - while grid.len <= y: - grid.add(newSeq[FlexibleCell]()) - - while grid[y].len < x + text.len: - grid[y].add(FlexibleCell()) - - var i = 0 - while i < text.len: - grid[y][i].rune = text[i] - inc i + grid.addCell(grid.len - 1, r, format) template inc_check(i: int) = inc i |