about summary refs log tree commit diff stats
path: root/src/display
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-07 16:10:18 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-07 16:10:18 +0200
commit93848e0c93ef0203393c48c8d9b48a2b51743107 (patch)
tree35113b8c73e831ab76d5de9bf7897857cdd8ce6a /src/display
parentd78298ae3369374bf7bf31592513d112c75a9601 (diff)
downloadchawan-93848e0c93ef0203393c48c8d9b48a2b51743107.tar.gz
Add XTERM title renaming
Diffstat (limited to 'src/display')
-rw-r--r--src/display/pager.nim5
-rw-r--r--src/display/term.nim48
2 files changed, 26 insertions, 27 deletions
diff --git a/src/display/pager.nim b/src/display/pager.nim
index 85381c48..fde58c92 100644
--- a/src/display/pager.nim
+++ b/src/display/pager.nim
@@ -93,6 +93,7 @@ iterator containers*(pager: Pager): Container {.inline.} =
 proc setContainer*(pager: Pager, c: Container) {.jsfunc.} =
   pager.container = c
   pager.redraw = true
+  pager.term.setTitle(c.getTitle())
 
 proc hasprop(pager: Pager, s: string): bool {.jshasprop.} =
   if pager.container != nil:
@@ -854,6 +855,10 @@ proc handleEvent0(pager: Pager, container: Container, event: ContainerEvent): bo
   of STATUS:
     if pager.container == container:
       pager.refreshStatusMsg()
+  of TITLE:
+    if pager.container == container:
+      pager.refreshStatusMsg()
+      pager.term.setTitle(container.getTitle())
   of ALERT:
     if pager.container == container:
       pager.alert(event.msg)
diff --git a/src/display/term.nim b/src/display/term.nim
index e665faee..4f33ee1a 100644
--- a/src/display/term.nim
+++ b/src/display/term.nim
@@ -61,35 +61,24 @@ func hascap(term: Terminal, c: TermcapCap): bool = term.tc.caps[c] != nil
 func cap(term: Terminal, c: TermcapCap): string = $term.tc.caps[c]
 func ccap(term: Terminal, c: TermcapCap): cstring = term.tc.caps[c]
 
-template CSI*(s: varargs[string, `$`]): string =
-  var r = "\e["
-  var first = true
-  for x in s:
-    if not first:
-      r &= ";"
-    first = false
-    r &= x
-  r
+# control sequence introducer
+template CSI(s: varargs[string, `$`]): string =
+  "\e[" & s.join(';')
+
+# OS command
+template OSC(s: varargs[string, `$`]): string =
+  "\e]" & s.join(';') & '\a'
+
+template XTERM_TITLE(s: string): string =
+  OSC(0, s)
 
 when not termcap_found:
+  # DEC set
   template DECSET(s: varargs[string, `$`]): string =
-    var r = "\e[?"
-    var first = true
-    for x in s:
-      if not first:
-        r &= ";"
-      first = false
-      r &= x
-    r & "h"
+    "\e[?" & s.join(';') & 'h'
+  # DEC reset
   template DECRST(s: varargs[string, `$`]): string =
-    var r = "\e[?"
-    var first = true
-    for x in s:
-      if not first:
-        r &= ";"
-      first = false
-      r &= x
-    r & "l"
+    "\e[?" & s.join(';') & 'l'
   template SMCUP(): string = DECSET(1049)
   template RMCUP(): string = DECRST(1049)
   template HVP(s: varargs[string, `$`]): string =
@@ -324,6 +313,10 @@ proc windowChange*(term: Terminal, attrs: WindowAttributes) =
   term.canvas = newFixedGrid(attrs.width, attrs.height)
   term.cleared = false
 
+proc setTitle*(term: Terminal, title: string) =
+  if term.config.display.set_title:
+    term.outfile.write(XTERM_TITLE(title))
+
 proc processOutputString*(term: Terminal, str: string, w: var int): string =
   if str.validateUtf8() != -1:
     return "?"
@@ -447,8 +440,9 @@ proc applyConfig(term: Terminal) =
   for fm in FormatFlags:
     if fm in term.config.display.no_format_mode:
       term.formatmode.excl(fm)
-  if term.isatty() and term.config.display.alt_screen.isSome:
-    term.smcup = term.config.display.alt_screen.get
+  if term.isatty():
+    if term.config.display.alt_screen.isSome:
+      term.smcup = term.config.display.alt_screen.get
   term.mincontrast = term.config.display.minimum_contrast
   if term.config.encoding.display_charset.isSome:
     term.cs = term.config.encoding.display_charset.get