about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-08-28 23:15:46 +0200
committerbptato <nincsnevem662@gmail.com>2021-08-28 23:15:46 +0200
commit9996586865f931b6da19779520eb7671eddc6c4d (patch)
tree626f98684ffc9b8aba51f863cec72ad4962c0d01 /src/io
parent672ceca5730f6ff1b17a715f88214ff2a8e895c3 (diff)
downloadchawan-9996586865f931b6da19779520eb7671eddc6c4d.tar.gz
Rewrite renderer (still non-functional)
Diffstat (limited to 'src/io')
-rw-r--r--src/io/buffer.nim39
-rw-r--r--src/io/cell.nim11
2 files changed, 43 insertions, 7 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index c299e79c..cfea36e3 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -67,9 +67,9 @@ func newBuffer*(attrs: TermAttributes): Buffer =
   result.height = attrs.termHeight - 1
   result.attrs = attrs
 
-  result.display = newSeq[FixedCell](result.width * result.height)
-  result.prevdisplay = newSeq[FixedCell](result.width * result.height)
-  result.statusmsg = newSeq[FixedCell](result.width)
+  result.display = newFixedGrid(result.width, result.height)
+  result.prevdisplay = newFixedGrid(result.width, result.height)
+  result.statusmsg = newFixedGrid(result.width)
 
 func generateFullOutput*(buffer: Buffer): seq[string] =
   var x = 0
@@ -510,9 +510,25 @@ proc refreshTermAttrs*(buffer: Buffer): bool =
 
 proc setText*(buffer: Buffer, x: int, y: int, text: seq[Rune]) = buffer.lines.setText(x, y, text)
 
+proc setLine*(buffer: Buffer, x: int, y: int, line: FlexibleLine) =
+  while buffer.lines.len <= y:
+    buffer.lines.add(newSeq[FlexibleCell]())
+
+  var i = 0
+  var cx = 0
+  while cx < x and i < buffer.lines[y].len:
+    cx += buffer.lines[y][i].rune.width()
+    inc i
+
+  buffer.lines[y].setLen(i)
+  i = 0
+  while i < line.len:
+    buffer.lines[y].add(line[i])
+    inc i
+
 proc reshape*(buffer: Buffer) =
-  buffer.display = newSeq[FixedCell](buffer.width * buffer.height)
-  buffer.statusmsg = newSeq[FixedCell](buffer.width)
+  buffer.display = newFixedGrid(buffer.width, buffer.height)
+  buffer.statusmsg = newFixedGrid(buffer.width)
 
 proc clearDisplay*(buffer: Buffer) =
   var i = 0
@@ -524,6 +540,9 @@ proc refreshDisplay*(buffer: Buffer) =
   var y = 0
   buffer.prevdisplay = buffer.display
   buffer.clearDisplay()
+  if buffer.fromy > buffer.lastVisibleLine - 1:
+    buffer.fromy = 0
+    buffer.cursory = buffer.lastVisibleLine - 1
   for line in buffer.lines[buffer.fromy..buffer.lastVisibleLine - 1]:
     var w = 0
     var i = 0
@@ -572,7 +591,15 @@ proc renderDocument*(buffer: Buffer) =
   stack.add(buffer.rootbox)
   while stack.len > 0:
     let box = stack.pop()
-    buffer.setText(box.innerEdge.x1, box.innerEdge.y1, box.content)
+    if box of CSSInlineBox:
+      let inline = CSSInlineBox(box)
+      var i = 0
+      for line in inline.content:
+        var x = inline.innerEdge.x1
+        if i == 0:
+          x = inline.fromx
+        var y = box.innerEdge.y1 + i
+        buffer.setLine(x, y, line)
 
     for child in box.children:
       stack.add(child)
diff --git a/src/io/cell.nim b/src/io/cell.nim
index d4a55b14..6ea887e3 100644
--- a/src/io/cell.nim
+++ b/src/io/cell.nim
@@ -1,6 +1,7 @@
 import unicode
 
 import types/color
+import utils/twtstr
 
 type
   Cell* = object of RootObj
@@ -13,7 +14,9 @@ type
   FlexibleCell* = object of Cell
     rune*: Rune
 
-  FlexibleGrid* = seq[seq[FlexibleCell]]
+  FlexibleLine* = seq[FlexibleCell]
+
+  FlexibleGrid* = seq[FlexibleLine]
 
   FixedCell* = object of Cell
     runes*: seq[Rune]
@@ -32,3 +35,9 @@ proc setText*(grid: var FlexibleGrid, x: int, y: int, text: seq[Rune]) =
     grid[y][i].rune = text[i]
     inc i
 
+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()