about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-11-19 18:45:10 +0100
committerbptato <nincsnevem662@gmail.com>2022-11-19 18:45:10 +0100
commit941f7e43b5cb7ebf6b84c3fb758ef2f1a5f641a3 (patch)
tree3e36c52d3cb8c87751b45128a3180237a3825d74
parent986522a915233dc068e00b47ca8b16e42cb50c7f (diff)
downloadchawan-941f7e43b5cb7ebf6b84c3fb758ef2f1a5f641a3.tar.gz
Fix weird screen shift behavior
(or at least I hope this fixed it)
-rw-r--r--src/buffer/container.nim38
-rw-r--r--src/display/client.nim5
-rw-r--r--src/display/pager.nim13
3 files changed, 33 insertions, 23 deletions
diff --git a/src/buffer/container.nim b/src/buffer/container.nim
index 4232ae09..a96aae5c 100644
--- a/src/buffer/container.nim
+++ b/src/buffer/container.nim
@@ -76,6 +76,7 @@ type
     hlon*: bool
     pipeto: Container
     tty: FileHandle
+    redraw*: bool
 
 proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize_t): cint {.
   importc: "setvbuf", header: "<stdio.h>", tags: [].}
@@ -206,13 +207,13 @@ func getTitle*(container: Container): string =
     return "*pipe*"
   return container.source.location.serialize(excludepassword = true)
 
-func currentLineWidth*(container: Container): int =
+func currentLineWidth(container: Container): int =
   if container.numLines == 0: return 0
   return container.currentLine.width()
 
 func maxfromy(container: Container): int = max(container.numLines - container.height, 0)
 
-func maxfromx(container: Container): int = max(container.currentLineWidth() - container.width, 0)
+func maxfromx(container: Container): int = max(container.maxScreenWidth() - container.width, 0)
 
 func atPercentOf*(container: Container): int =
   if container.numLines == 0: return 100
@@ -277,10 +278,15 @@ proc setFromY*(container: Container, y: int) =
   if container.pos.fromy != y:
     container.pos.fromy = max(min(y, container.maxfromy), 0)
     container.writeCommand(GET_LINES, container.lineWindow)
+    container.redraw = true
 
 proc setFromX*(container: Container, x: int) =
   if container.pos.fromx != x:
     container.pos.fromx = max(min(x, container.maxfromx), 0)
+    if container.pos.fromx > container.cursorx:
+      container.pos.cursorx = min(container.pos.fromx, container.currentLineWidth())
+      container.writeCommand(MOVE_CURSOR, container.cursorx, container.cursory)
+    container.redraw = true
 
 proc setFromXY*(container: Container, x, y: int) =
   container.setFromY(y)
@@ -292,19 +298,20 @@ proc setCursorX*(container: Container, x: int, refresh = true, save = true) =
     return
   container.pos.setx = -1
   let cw = container.currentLineWidth()
+  let x2 = x
   let x = max(min(x, cw - 1), 0)
-  if (not refresh) or (container.fromx <= x and x < container.fromx + container.width):
+  if not refresh or container.fromx <= x and x < container.fromx + container.width:
     container.pos.cursorx = x
-  else:
-    if refresh and container.fromx > container.cursorx:
-      container.setFromX(max(cw - 1, 0))
-      container.pos.cursorx = container.fromx
-    elif x > container.cursorx:
-      container.setFromX(max(x - container.width + 1, 0))
-      container.pos.cursorx = x
-    elif x < container.cursorx:
+  elif refresh and container.fromx > x:
+    if x2 < container.cursorx:
       container.setFromX(x)
-      container.pos.cursorx = x
+    container.pos.cursorx = container.fromx
+  elif x > container.cursorx:
+    container.setFromX(max(x - container.width + 1, container.fromx))
+    container.pos.cursorx = x
+  elif x < container.cursorx:
+    container.setFromX(x)
+    container.pos.cursorx = x
   container.writeCommand(MOVE_CURSOR, container.cursorx, container.cursory)
   if save:
     container.pos.xend = container.cursorx
@@ -343,7 +350,10 @@ proc cursorUp*(container: Container) =
   container.setCursorY(container.cursory - 1)
 
 proc cursorLeft*(container: Container) =
-  container.setCursorX(container.cursorx - container.prevWidth())
+  var w = container.prevWidth()
+  if w == 0:
+    w = 1
+  container.setCursorX(container.cursorx - w)
 
 proc cursorRight*(container: Container) =
   container.setCursorX(container.cursorx + container.currentWidth())
@@ -427,11 +437,9 @@ proc pageUp*(container: Container) =
 
 proc pageLeft*(container: Container) =
   container.setFromX(container.fromx - container.width)
-  container.setCursorX(container.cursorx - container.width)
 
 proc pageRight*(container: Container) =
   container.setFromX(container.fromx + container.width)
-  container.setCursorX(container.cursorx + container.width)
 
 proc halfPageUp*(container: Container) =
   container.setFromY(container.fromy - container.height div 2 + 1)
diff --git a/src/display/client.nim b/src/display/client.nim
index a45e64e3..cf5842b1 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -208,10 +208,11 @@ proc inputLoop(client: Client) =
           for msg in client.pager.status:
             eprint msg
           client.quit(1)
-    if client.pager.lineedit.isNone and client.pager.switched:
+    if client.pager.lineedit.isNone and client.pager.redraw or client.pager.container.redraw:
       client.pager.refreshDisplay(client.pager.container)
       client.pager.displayPage()
-      client.pager.switched = false
+      client.pager.redraw = false
+      client.pager.container.redraw = false
     if client.pager.command != "":
       client.command(client.pager.command)
       client.pager.command = ""
diff --git a/src/display/pager.nim b/src/display/pager.nim
index e34d94eb..f8e012eb 100644
--- a/src/display/pager.nim
+++ b/src/display/pager.nim
@@ -38,7 +38,6 @@ type
     reverseSearch: bool
     status*: seq[string]
     statusmsg*: FixedGrid
-    switched*: bool
     tty: File
     selector*: Selector[Container]
     fdmap*: Table[FileHandle, Container]
@@ -46,6 +45,7 @@ type
     display: FixedGrid
     bheight*: int
     bwidth*: int
+    redraw*: bool
 
 iterator containers*(pager: Pager): Container =
   if pager.container != nil:
@@ -60,7 +60,7 @@ iterator containers*(pager: Pager): Container =
 
 proc setContainer*(pager: Pager, c: Container) =
   pager.container = c
-  pager.switched = true
+  pager.redraw = true
 
 proc cursorDown(pager: Pager) {.jsfunc.} = pager.container.cursorDown()
 proc cursorUp(pager: Pager) {.jsfunc.} = pager.container.cursorUp()
@@ -283,6 +283,9 @@ proc displayPage*(pager: Pager) =
   stdout.flushFile()
 
 proc redraw(pager: Pager) {.jsfunc.} =
+  pager.redraw = true
+
+proc draw*(pager: Pager) =
   pager.refreshDisplay()
   pager.refreshStatusMsg()
   pager.displayPage()
@@ -491,7 +494,7 @@ proc updateReadLineISearch(pager: Pager, linemode: LineMode) =
       pager.regex = pager.iregex
     pager.reverseSearch = linemode == ISEARCH_B
     pager.container.clearSearchHighlights()
-    pager.redraw()
+    pager.redraw = true
 
 proc updateReadLine*(pager: Pager) =
   let lineedit = pager.lineedit.get
@@ -600,9 +603,7 @@ proc handleEvent*(pager: Pager, container: Container): bool =
     pager.displayCursor()
   of UPDATE:
     if container == pager.container:
-      pager.refreshDisplay()
-      pager.refreshStatusMsg()
-      pager.displayPage()
+      pager.redraw = true
   of JUMP:
     if container == pager.container:
       pager.refreshStatusMsg()