about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-17 18:49:26 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-17 19:49:20 +0100
commitff1382caa19bfc8e0d7825d1b71ea1e5356aeda4 (patch)
treeb922e6bec720f513f24a514193ca3eb2db6a3d41 /src
parente8313fc3add0137c9a9d3857ef92b60236f4295a (diff)
downloadchawan-ff1382caa19bfc8e0d7825d1b71ea1e5356aeda4.tar.gz
term: rework input buffer
heh
Diffstat (limited to 'src')
-rw-r--r--src/io/dynstream.nim6
-rw-r--r--src/local/pager.nim4
-rw-r--r--src/local/term.nim28
-rw-r--r--src/server/buffer.nim2
4 files changed, 25 insertions, 15 deletions
diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim
index 55a73061..959c212a 100644
--- a/src/io/dynstream.nim
+++ b/src/io/dynstream.nim
@@ -60,10 +60,6 @@ proc write*(s: DynStream; buffer: openArray[char]) {.inline.} =
 proc write*(s: DynStream; c: char) {.inline.} =
   s.sendDataLoop(unsafeAddr c, 1)
 
-proc sreadChar*(s: DynStream): char =
-  let n = s.recvData(addr result, 1)
-  assert n == 1
-
 proc recvDataLoop*(s: DynStream; buffer: pointer; len: int) =
   var n = 0
   while n < len:
@@ -134,7 +130,7 @@ method recvData*(s: PosixStream; buffer: pointer; len: int): int =
     s.setEnd()
   return n
 
-proc sreadChar*(s: PosixStream): char =
+proc readChar*(s: PosixStream): char =
   let n = read(s.fd, addr result, 1)
   assert n == 1
 
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 04f5fbe1..aee3c157 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -444,9 +444,9 @@ proc interruptHandler(rt: JSRuntime; opaque: pointer): cint {.cdecl.} =
       var buf = [char(0)]
       let n = pager.term.istream.recvData(buf)
       if n == 1 and buf[0] == char(3): #C-c
-        pager.term.ibuf = ""
+        pager.term.resetInputBuffer()
         return 1
-      pager.term.ibuf &= buf[0]
+      pager.term.bufferInputChar(buf[0])
     except ErrorAgain:
       discard
   return 0
diff --git a/src/local/term.nim b/src/local/term.nim
index 77bd4ead..00c60e2b 100644
--- a/src/local/term.nim
+++ b/src/local/term.nim
@@ -136,7 +136,9 @@ type
     origTermios: Termios
     defaultBackground: RGBColor
     defaultForeground: RGBColor
-    ibuf*: string # buffer for chars when we can't process them
+    ibuf: array[256, char] # buffer for chars when we can't process them
+    ibufLen: int # len of ibuf
+    ibufn: int # position in ibuf
     sixelRegisterNum*: int
     sixelMaxWidth*: int
     sixelMaxHeight: int
@@ -254,11 +256,23 @@ proc write(term: Terminal; s: cstring) =
   term.outfile.write(s)
 
 proc readChar*(term: Terminal): char =
-  if term.ibuf.len == 0:
-    result = term.istream.sreadChar()
-  else:
-    result = term.ibuf[0]
-    term.ibuf.delete(0..0)
+  if term.ibufn == term.ibufLen:
+    term.ibufn = 0
+    term.ibufLen = term.istream.recvData(term.ibuf)
+  result = term.ibuf[term.ibufn]
+  inc term.ibufn
+
+proc bufferInputChar*(term: Terminal; c: char) =
+  if term.ibufn == term.ibuf.len:
+    return # can't help it, sorry :P
+  term.ibuf[term.ibufn] = c
+  inc term.ibufn
+  if term.ibufn >= term.ibufLen:
+    term.ibufLen = term.ibufn
+
+proc resetInputBuffer*(term: Terminal) =
+  term.ibufn = 0
+  term.ibufLen = 0
 
 proc flush*(term: Terminal) =
   term.outfile.flushFile()
@@ -292,7 +306,7 @@ proc anyKey*(term: Terminal; msg = "[Hit any key]") =
   if term.isatty():
     term.write(term.clearEnd() & msg)
     term.flush()
-    discard term.istream.sreadChar()
+    discard term.istream.readChar()
 
 proc resetFormat(term: Terminal): string =
   when TermcapFound:
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index c80f1b07..9258b293 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -1025,7 +1025,7 @@ proc clone*(buffer: Buffer; newurl: URL): int {.proxy.} =
     discard close(pipefd[1]) # close write
     # We must wait for child to tee its ongoing streams.
     let ps = newPosixStream(pipefd[0])
-    let c = ps.sreadChar()
+    let c = ps.readChar()
     assert c == char(0)
     ps.sclose()
     buffer.loader.resume(ids)