about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/io/buffer.nim42
-rw-r--r--src/io/cell.nim10
-rw-r--r--src/main.nim1
3 files changed, 27 insertions, 26 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index 62c68486..d212deac 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -244,16 +244,14 @@ proc refreshDisplay*(buffer: Buffer) =
                            buffer.lastVisibleLine - 1]:
     var w = 0
     var i = 0
-    var j = 0
     while w < buffer.fromx and i < line.str.len:
       fastRuneAt(line.str, i, r)
       w += r.width()
-      inc j
 
     let dls = y * buffer.width
     var k = 0
-    var cf = line.findFormat(j)
-    var nf = line.findNextFormat(j)
+    var cf = line.findFormat(i)
+    var nf = line.findNextFormat(i)
     if w > buffer.fromx:
       while k < w - buffer.fromx:
         buffer.display[dls + k].runes.add(Rune(' '))
@@ -261,6 +259,7 @@ proc refreshDisplay*(buffer: Buffer) =
         inc k
 
     while i < line.str.len:
+      let j = i
       fastRuneAt(line.str, i, r)
       w += r.width()
       if w > buffer.fromx + buffer.width:
@@ -277,7 +276,6 @@ proc refreshDisplay*(buffer: Buffer) =
       while k < tk and k < buffer.width - 1:
         buffer.display[dls + k].ow += r.width()
         inc k
-      inc j
 
     inc y
 
@@ -617,19 +615,16 @@ proc setRowBox(buffer: Buffer, line: CSSRowBox) =
   while buffer.lines.len <= y:
     buffer.addLine()
 
-  var j = 0
   var cx = 0
   while cx < x and i < buffer.lines[y].str.len:
     fastRuneAt(buffer.lines[y].str, i, r)
     cx += r.width()
-    inc j
 
   let ostr = buffer.lines[y].str.substr(i)
-  let oformats = buffer.lines[y].formats.subformats(j)
-  buffer.lines[y].str.setLen(i)
-  buffer.lines[y].setLen(j)
+  let oformats = buffer.lines[y].formats.subformats(i)
+  buffer.lines[y].setLen(i)
 
-  buffer.lines.addFormat(y, j, line.formatFromLine(), line.nodes)
+  buffer.lines.addFormat(y, i, line.formatFromLine(), line.nodes)
 
   var nx = cx
   if nx < x:
@@ -640,14 +635,12 @@ proc setRowBox(buffer: Buffer, line: CSSRowBox) =
   nx += linestr.width()
 
   i = 0
-  j = 0
   while cx < nx and i < ostr.len:
     fastRuneAt(ostr, i, r)
     cx += r.width()
-    inc j
 
   if i < ostr.len:
-    let oline = FlexibleLine(str: ostr.substr(i), formats: oformats.subformats(j))
+    let oline = FlexibleLine(str: ostr.substr(i), formats: oformats.subformats(i))
     buffer.lines[y].add(oline)
 
 proc updateCursor(buffer: Buffer) =
@@ -691,35 +684,44 @@ proc updateHover(buffer: Buffer) =
   buffer.prevnodes = nodes
 
 proc renderPlainText*(buffer: Buffer, text: string) =
+  var format = newFormatting()
+  template add_format() =
+    if af:
+      af = false
+      buffer.lines.addFormat(y, buffer.lines[y].str.len, format)
+
   buffer.clearText()
   var i = 0
   var x = 0
   var y = 0
   var r: Rune
-  var format = newFormatting()
+  var af = false
   while i < text.len:
     if text[i] == '\n':
       if i != text.len - 1:
+        add_format
         buffer.addLine()
-        buffer.lines.addFormat(buffer.lines.len - 1, format)
         inc y
         x = 0
       inc i
     elif text[i] == '\r':
       inc i
     elif text[i] == '\t':
+      add_format
       for i in 0..8:
         buffer.lines[^1].str &= ' '
       inc i
     elif text[i] == '\e':
       i = format.parseAnsiCode(text, i)
+      af = true
     elif text[i].isControlChar():
-      buffer.lines.addCell(Rune('^'))
-      buffer.lines.addCell(Rune(text[i].getControlLetter()))
+      add_format
+      buffer.lines[y].str &= '^' & text[i].getControlLetter()
       inc i
     else:
-      fastRuneAt(text, i, r)
-      buffer.lines.addCell(r)
+      add_format
+      buffer.lines[y].str &= text[i]
+      inc i
   buffer.updateCursor()
 
 
diff --git a/src/io/cell.nim b/src/io/cell.nim
index 5bdbfb50..9e657bb6 100644
--- a/src/io/cell.nim
+++ b/src/io/cell.nim
@@ -91,9 +91,6 @@ func width*(line: FlexibleLine): int =
 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)
 
@@ -143,18 +140,19 @@ proc setLen*(line: var FlexibleLine, len: int) =
     if line.formats[i].pos >= len:
       line.formats.setLen(i)
       break
+  line.str.setLen(len)
   #line.formats = line.formats.filter((x) => x.pos < len)
 
 proc add*(a: var FlexibleLine, b: FlexibleLine) =
-  let l = a.len
+  let l = a.str.len
   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) =
   grid.add(FlexibleLine())
 
-proc addFormat*(grid: var FlexibleGrid, y: int, format: Formatting) =
-  grid[y].formats.add(FormattingCell(formatting: format, pos: grid[y].len))
+proc addFormat*(grid: var FlexibleGrid, y, pos: int, format: Formatting) =
+  grid[y].formats.add(FormattingCell(formatting: format, pos: grid[y].str.len))
 
 proc addFormat*(grid: var FlexibleGrid, y, pos: int, format: Formatting, nodes: seq[Node]) =
   grid[y].formats.add(FormattingCell(formatting: format, nodes: nodes, pos: pos))
diff --git a/src/main.nim b/src/main.nim
index d9d10efa..1bb8cf1e 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -47,6 +47,7 @@ proc main*() =
   var lastUri: Uri
   if paramCount() < 1:
     if not isatty(stdin):
+      buffer.showsource = true
       try:
         while true:
           buffer.source &= stdin.readChar()