about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-11-10 18:26:18 +0100
committerbptato <nincsnevem662@gmail.com>2021-11-10 18:32:25 +0100
commitfcd3a5b204e15fdfc739fd04975977d288e892e0 (patch)
tree363f3bfd60570ce5fb1f4fbc6c62557609ccc6ea /src/io
parente6f7cc72ba3343fb81c4f8196446c58eca59191e (diff)
downloadchawan-fcd3a5b204e15fdfc739fd04975977d288e892e0.tar.gz
Layout engine improvements, use author style sheet
Diffstat (limited to 'src/io')
-rw-r--r--src/io/buffer.nim123
1 files changed, 54 insertions, 69 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index a3dde1b7..9775a267 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -1,7 +1,6 @@
 import options
 import terminal
 import uri
-import tables
 import strutils
 import unicode
 
@@ -47,11 +46,6 @@ type
     xend*: int
     fromx*: int
     fromy*: int
-    nodes*: seq[Node]
-    links*: seq[Node]
-    elements*: seq[Element]
-    idelements*: Table[string, Element]
-    selectedlink*: Node
     attrs*: TermAttributes
     document*: Document
     displaycontrols*: bool
@@ -182,6 +176,12 @@ func width(line: seq[FlexibleCell]): int =
   for c in line:
     result += c.rune.width()
 
+func acursorx(buffer: Buffer): int =
+  return max(0, buffer.cursorx - buffer.fromx)
+
+func acursory(buffer: Buffer): int =
+  return buffer.cursory - buffer.fromy
+
 func cellOrigin(buffer: Buffer, x: int, y: int): int =
   let row = y * buffer.width
   var ox = x
@@ -190,7 +190,11 @@ func cellOrigin(buffer: Buffer, x: int, y: int): int =
   return ox
 
 func currentCellOrigin(buffer: Buffer): int =
-  return buffer.cellOrigin(buffer.cursorx - buffer.fromx, buffer.cursory - buffer.fromy)
+  return buffer.cellOrigin(buffer.acursorx, buffer.acursory)
+
+func currentRune(buffer: Buffer): Rune =
+  let row = (buffer.cursory - buffer.fromy) * buffer.width
+  return buffer.display[row + buffer.currentCellOrigin()].runes[0]
 
 func cellWidthOverlap*(buffer: Buffer, x: int, y: int): int =
   let ox = buffer.cellOrigin(x, y)
@@ -213,9 +217,6 @@ func atPercentOf*(buffer: Buffer): int =
   if buffer.lines.len == 0: return 100
   return (100 * (buffer.cursory + 1)) div buffer.numLines
 
-func lastNode*(buffer: Buffer): Node =
-  return buffer.nodes[^1]
-
 func cursorOnNode*(buffer: Buffer, node: Node): bool =
   if node.y == node.ey and node.y == buffer.cursory:
     return buffer.cursorx >= node.x and buffer.cursorx < node.ex
@@ -230,14 +231,6 @@ func findSelectedElement*(buffer: Buffer): Option[HtmlElement] =
 func canScroll*(buffer: Buffer): bool =
   return buffer.numLines >= buffer.height
 
-func getElementById*(buffer: Buffer, id: string): Element =
-  if buffer.idelements.hasKey(id):
-    return buffer.idelements[id]
-  return nil
-
-proc findSelectedNode*(buffer: Buffer): Option[Node] =
-  discard #TODO
-
 proc addLine(buffer: Buffer) =
   buffer.lines.add(newSeq[FlexibleCell]())
 
@@ -245,21 +238,13 @@ proc clearText*(buffer: Buffer) =
   buffer.lines.setLen(0)
   buffer.addLine()
 
-proc clearNodes*(buffer: Buffer) =
-  buffer.nodes.setLen(0)
-  buffer.links.setLen(0)
-  buffer.elements.setLen(0)
-  buffer.idelements.clear()
-
 proc clearBuffer*(buffer: Buffer) =
   buffer.clearText()
-  buffer.clearNodes()
   buffer.cursorx = 0
   buffer.cursory = 0
   buffer.fromx = 0
   buffer.fromy = 0
   buffer.hovertext = ""
-  buffer.selectedlink = nil
 
 proc restoreCursorX(buffer: Buffer) =
   buffer.cursorx = max(min(buffer.currentLineWidth() - 1, buffer.xend), 0)
@@ -308,18 +293,24 @@ proc cursorUp*(buffer: Buffer) =
 
 proc cursorRight*(buffer: Buffer) =
   let cellwidth = buffer.currentCellWidth()
-  let cellorigin = buffer.currentCellOrigin()
+  let cellorigin = buffer.fromx + buffer.currentCellOrigin()
   let lw = buffer.currentLineWidth()
   if buffer.cursorx < lw - 1:
     buffer.cursorx = min(lw - 1, cellorigin + cellwidth)
+    assert buffer.cursorx >= 0
     buffer.xend = buffer.cursorx
     if buffer.cursorx - buffer.width >= buffer.fromx:
       inc buffer.fromx
       buffer.redraw = true
+    if buffer.cursorx == buffer.fromx:
+      inc buffer.cursorx
 
 proc cursorLeft*(buffer: Buffer) =
-  let cellorigin = buffer.currentCellOrigin()
+  eprint "??", buffer.currentCellOrigin(), buffer.fromx
+  let cellorigin = buffer.fromx + buffer.currentCellOrigin()
+  let lw = buffer.currentLineWidth()
   if buffer.fromx > buffer.cursorx:
+    buffer.cursorx = min(max(lw - 1, 0), cellorigin - 1)
     buffer.fromx = buffer.cursorx
     buffer.redraw = true
   elif buffer.cursorx > 0:
@@ -338,58 +329,46 @@ proc cursorLineBegin*(buffer: Buffer) =
     buffer.redraw = true
 
 proc cursorLineEnd*(buffer: Buffer) =
-  buffer.cursorx = buffer.currentLineWidth() - 1
+  buffer.cursorx = max(buffer.currentLineWidth() - 1, 0)
   buffer.xend = buffer.cursorx
   buffer.fromx = max(buffer.cursorx - buffer.width + 1, 0)
   buffer.redraw = buffer.fromx > 0
 
-iterator revnodes*(buffer: Buffer): Node {.inline.} =
-  var i = buffer.nodes.len - 1
-  while i >= 0:
-    yield buffer.nodes[i]
-    dec i
-
 proc cursorNextWord*(buffer: Buffer) =
   let llen = buffer.currentLineWidth() - 1
-  var x = buffer.cursorx
-  var y = buffer.cursory
   if llen >= 0:
 
-    while not buffer.lines[y][x].rune.breaksWord():
-      if x >= llen:
+    while not buffer.currentRune().breaksWord():
+      if buffer.cursorx >= llen:
         break
-      inc x
+      buffer.cursorRight()
 
-    while buffer.lines[y][x].rune.breaksWord():
-      if x >= llen:
+    while buffer.currentRune().breaksWord():
+      if buffer.cursorx >= llen:
         break
-      inc x
+      buffer.cursorRight()
 
-  if x >= llen:
-    if y < buffer.numLines:
-      inc y
-      x = 0
-  buffer.cursorTo(x, y)
+  if buffer.cursorx >= buffer.currentLineWidth() - 1:
+    if buffer.cursory < buffer.numLines - 1:
+      buffer.cursorDown()
+      buffer.cursorLineBegin()
 
 proc cursorPrevWord*(buffer: Buffer) =
-  var x = buffer.cursorx
-  var y = buffer.cursory
   if buffer.currentLineWidth() > 0:
-    while not buffer.lines[y][x].rune.breaksWord():
-      if x == 0:
+    while not buffer.currentRune().breaksWord():
+      if buffer.cursorx == 0:
         break
-      dec x
+      buffer.cursorLeft()
 
-    while buffer.lines[y][x].rune.breaksWord():
-      if x == 0:
+    while buffer.currentRune().breaksWord():
+      if buffer.cursorx == 0:
         break
-      dec x
+      buffer.cursorLeft()
 
-  if x == 0:
-    if y > 0:
-      dec y
-      x = buffer.lines[y].len - 1
-  buffer.cursorTo(x, y)
+  if buffer.cursorx == 0:
+    if buffer.cursory > 0:
+      buffer.cursorUp()
+      buffer.cursorLineEnd()
 
 proc cursorNextLink*(buffer: Buffer) =
   #TODO
@@ -425,7 +404,7 @@ proc cursorMiddle*(buffer: Buffer) =
   buffer.restoreCursorX()
 
 proc cursorBottom*(buffer: Buffer) =
-  buffer.cursory = min(buffer.fromy + buffer.height - 1, buffer.numLines)
+  buffer.cursory = min(buffer.fromy + buffer.height - 1, buffer.numLines - 1)
   buffer.restoreCursorX()
 
 proc centerLine*(buffer: Buffer) =
@@ -505,10 +484,12 @@ proc scrollLeft*(buffer: Buffer) =
     buffer.redraw = true
 
 proc gotoAnchor*(buffer: Buffer): bool =
-  if buffer.location.anchor != "":
-    let node =  buffer.getElementById(buffer.location.anchor)
-    if node != nil:
-      buffer.scrollTo(max(node.y - buffer.height div 2, 0))
+  discard
+  #TODO
+  #if buffer.location.anchor != "":
+  #  let node =  buffer.getElementById(buffer.location.anchor)
+  #  if node != nil:
+  #    buffer.scrollTo(max(node.y - buffer.height div 2, 0))
 
 proc setLocation*(buffer: Buffer, uri: Uri) =
   buffer.location = uri
@@ -587,9 +568,11 @@ proc updateCursor(buffer: Buffer) =
     buffer.fromy = 0
     buffer.cursory = buffer.lastVisibleLine - 1
 
+  if buffer.cursorx >= buffer.currentLineWidth() - 1:
+    buffer.cursorLineEnd()
+
   if buffer.lines.len == 0:
     buffer.cursory = 0
-    return
 
 proc clearDisplay*(buffer: Buffer) =
   var i = 0
@@ -602,7 +585,8 @@ proc refreshDisplay*(buffer: Buffer) =
   buffer.prevdisplay = buffer.display
   buffer.clearDisplay()
 
-  for line in buffer.lines[buffer.fromy..buffer.lastVisibleLine - 1]:
+  for line in buffer.lines[buffer.fromy..
+                           buffer.lastVisibleLine - 1]:
     var w = 0
     var i = 0
     while w < buffer.fromx and i < line.len:
@@ -641,6 +625,7 @@ proc renderPlainText*(buffer: Buffer, text: string) =
     inc i
   if line.len > 0:
     buffer.setText(0, y, line.toRunes())
+  buffer.updateCursor()
 
 proc renderDocument*(buffer: Buffer) =
   buffer.clearText()
@@ -653,7 +638,6 @@ proc renderDocument*(buffer: Buffer) =
     let box = stack.pop()
     if box of CSSInlineBox:
       let inline = CSSInlineBox(box)
-      var i = 0
       eprint "NEW BOX", inline.context.conty
       for line in inline.content:
         eprint line
@@ -665,6 +649,7 @@ proc renderDocument*(buffer: Buffer) =
     while i >= 0:
       stack.add(box.children[i])
       dec i
+  buffer.updateCursor()
 
 proc cursorBufferPos(buffer: Buffer) =
   let x = max(buffer.cursorx - buffer.fromx, 0)