aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-18 20:37:36 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-18 20:37:36 +0200
commit8a826ba92b7c744a995a9593834d63acd6d040b3 (patch)
tree2b808d8df3d2a0a0994a28b6fd96bc073ad4a24b /src
parent2f8edaffdcf22b90389d214b730ea9e28a3b2e5a (diff)
downloadchawan-8a826ba92b7c744a995a9593834d63acd6d040b3.tar.gz
term: simplify/cleanup
Diffstat (limited to 'src')
-rw-r--r--src/display/term.nim94
-rw-r--r--src/utils/twtstr.nim9
2 files changed, 26 insertions, 77 deletions
diff --git a/src/display/term.nim b/src/display/term.nim
index 60b5ff7b..aaadeab8 100644
--- a/src/display/term.nim
+++ b/src/display/term.nim
@@ -2,6 +2,7 @@ import options
import os
import posix
import streams
+import strutils
import tables
import terminal
import termios
@@ -10,13 +11,13 @@ import unicode
import bindings/termcap
import config/config
import display/window
-import io/runestream
import types/cell
import types/color
import types/opt
import utils/twtstr
import chakasu/charset
+import chakasu/decoderstream
import chakasu/encoderstream
#TODO switch from termcap...
@@ -174,30 +175,9 @@ proc endFormat(term: Terminal, flag: FormatFlags): string =
return term.cap ue
return SGR(FormatCodes[flag].e)
-#TODO get rid of these
proc setCursor*(term: Terminal, x, y: int) =
term.write(term.cursorGoto(x, y))
-proc cursorBackward*(term: Terminal, i: int) =
- if i > 0:
- if i == 1:
- term.write("\b")
- else:
- when termcap_found:
- term.write($tgoto(term.ccap LE, 0, cint(i)))
- else:
- term.outfile.cursorBackward(i)
-
-proc cursorForward*(term: Terminal, i: int) =
- if i > 0:
- when termcap_found:
- term.write($tgoto(term.ccap RI, 0, cint(i)))
- else:
- term.outfile.cursorForward(i)
-
-proc cursorBegin*(term: Terminal) =
- term.write("\r")
-
proc enableAltScreen(term: Terminal): string =
when termcap_found:
if term.hascap ti:
@@ -382,65 +362,25 @@ proc setTitle*(term: Terminal, title: string) =
if term.set_title:
term.outfile.write(XTERM_TITLE(title))
-const colorFormat = block:
- var format = newFormat()
- format.fgcolor = cellColor(ANSI_BLUE)
- format
-
-const defaultFormat = newFormat()
-
-template processOutputString0*(term: Terminal, str: iterable[Rune],
- colorctrl: static bool, w: var int): string =
- var rs0: seq[Rune]
- var ctrl = false
- var format = newFormat()
- discard ctrl
- discard format
- for r in str:
- if r.isControlChar():
- when colorctrl:
- if not ctrl:
- rs0 &= term.processFormat(format, colorFormat).toRunes()
- rs0 &= Rune('^')
- rs0 &= Rune(cast[char](r).getControlLetter())
- else:
- when colorctrl:
- if ctrl:
- rs0 &= term.processFormat(format, defaultFormat).toRunes()
- rs0 &= r
- ctrl = r.isControlChar()
- # twidth wouldn't work here, the view may start at the nth character.
- # pager must ensure tabs are converted beforehand.
- w += r.width()
- let ss = newRuneStream(toOpenArray(cast[seq[uint32]](rs0), 0, rs0.high))
- let es = newEncoderStream(ss, term.cs, errormode = ENCODER_ERROR_MODE_FATAL)
- es.readAll()
-
proc processOutputString*(term: Terminal, str: string, w: var int): string =
if str.validateUtf8() != -1:
return "?"
+ # twidth wouldn't work here, the view may start at the nth character.
+ # pager must ensure tabs are converted beforehand.
+ w += str.notwidth()
+ let str = if Controls in str:
+ str.replaceControls()
+ else:
+ str
if term.cs == CHARSET_UTF_8:
- # optimized common case
- block notfound:
- for c in str:
- if c in Controls:
- break notfound
- # No control characters, and the output encoding matches the internal
- # representation.
- w += str.width()
- return str
- var s = ""
- for c in str:
- if c in Controls:
- s &= '^'
- s &= c.getControlLetter()
- else:
- s &= c
- # no twidth, see above
- w += Rune(c).width()
- return s
- # Output is not utf-8, so we must convert back to utf-32 and then encode.
- return term.processOutputString0(str.runes, false, w)
+ # The output encoding matches the internal representation.
+ return str
+ else:
+ # Output is not utf-8, so we must convert back to utf-32 and then encode.
+ let ss = newStringStream(str)
+ let ds = newDecoderStream(ss)
+ let es = newEncoderStream(ds, term.cs, errormode = ENCODER_ERROR_MODE_FATAL)
+ return es.readAll()
proc generateFullOutput(term: Terminal, grid: FixedGrid): string =
var format = newFormat()
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index c0b68178..7589c262 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -1071,6 +1071,15 @@ func deleteChars*(s: string, todel: set[char]): string =
inc i
return rs
+func replaceControls*(s: string): string =
+ result = newStringOfCap(s.len)
+ for c in s:
+ if c in Controls:
+ result &= '^'
+ result &= c.getControlLetter()
+ else:
+ result &= c
+
#https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm
proc makeCRLF*(s: string): string =
result = newStringOfCap(s.len)