about summary refs log tree commit diff stats
path: root/src/display
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-11-29 22:32:38 +0100
committerbptato <nincsnevem662@gmail.com>2022-11-29 22:32:52 +0100
commit6c4b7671df166bec49b1a175f759974ff5962a38 (patch)
tree3c31ced89b880df1c3870145667503bea24ee5e3 /src/display
parentf2daba3542cabb4741e2dc38d1bf36a2d1564f60 (diff)
downloadchawan-6c4b7671df166bec49b1a175f759974ff5962a38.tar.gz
Add siteconf, fix lineedit bugs
This enables rule-based dynamic url rewriting.
Also, lineedit is a bit less broken now (though it's still less than
ideal.)
Diffstat (limited to 'src/display')
-rw-r--r--src/display/client.nim4
-rw-r--r--src/display/pager.nim26
2 files changed, 24 insertions, 6 deletions
diff --git a/src/display/client.nim b/src/display/client.nim
index 2fd8bb89..364a4963 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -85,7 +85,7 @@ proc doRequest(client: Client, req: Request): Response {.jsfunc.} =
 
 proc interruptHandler(rt: JSRuntime, opaque: pointer): int {.cdecl.} =
   let client = cast[Client](opaque)
-  if client.console.tty == nil: return
+  if client.console == nil or client.console.tty == nil: return
   try:
     let c = client.console.tty.readChar()
     if c == char(3): #C-c
@@ -410,11 +410,11 @@ proc newClient*(config: Config, dispatcher: Dispatcher): Client =
   result.dispatcher = dispatcher
   result.attrs = getWindowAttributes(stdout)
   result.loader = dispatcher.forkserver.newFileLoader()
-  result.pager = newPager(config, result.attrs, dispatcher)
   result.jsrt = newJSRuntime()
   result.jsrt.setInterruptHandler(interruptHandler, cast[pointer](result))
   let ctx = result.jsrt.newJSContext()
   result.jsctx = ctx
+  result.pager = newPager(config, result.attrs, dispatcher, result.config.getSiteConfig(ctx))
   var global = ctx.getGlobalObject()
   ctx.registerType(Client, asglobal = true)
   global.setOpaque(result)
diff --git a/src/display/pager.nim b/src/display/pager.nim
index 2abcd6c6..614974c2 100644
--- a/src/display/pager.nim
+++ b/src/display/pager.nim
@@ -53,6 +53,9 @@ type
     redraw*: bool
     term*: Terminal
     linehist: array[LineMode, LineHistory]
+    siteconf: seq[SiteConfig]
+
+func attrs(pager: Pager): WindowAttributes = pager.term.attrs
 
 iterator containers*(pager: Pager): Container =
   if pager.container != nil:
@@ -115,8 +118,6 @@ proc searchPrev(pager: Pager) {.jsfunc.} =
     else:
       pager.container.cursorNextMatch(pager.regex.get, true)
 
-func attrs(pager: Pager): WindowAttributes = pager.term.attrs
-
 proc getLineHist(pager: Pager, mode: LineMode): LineHistory =
   if pager.linehist[mode] == nil:
     pager.linehist[mode] = newLineHistory()
@@ -143,14 +144,18 @@ proc isearchBackward(pager: Pager) {.jsfunc.} =
   pager.container.pushCursorPos()
   pager.setLineEdit("?", ISEARCH_B)
 
-proc newPager*(config: Config, attrs: WindowAttributes, dispatcher: Dispatcher): Pager =
+proc newPager*(config: Config, attrs: WindowAttributes, dispatcher: Dispatcher, siteconf: seq[SiteConfig]): Pager =
   let pager = Pager(
     dispatcher: dispatcher,
     config: config,
     display: newFixedGrid(attrs.width, attrs.height - 1),
     statusgrid: newFixedGrid(attrs.width),
-    term: newTerminal(stdout, config, attrs)
+    term: newTerminal(stdout, config, attrs),
   )
+  for sc in siteconf:
+    # not sure why but normal copies don't seem to work here...
+    pager.siteconf.add(sc)
+    pager.siteconf[^1].subst = sc.subst
   return pager
 
 proc launchPager*(pager: Pager, tty: File) =
@@ -412,8 +417,21 @@ proc windowChange*(pager: Pager, attrs: WindowAttributes) =
   for container in pager.containers:
     container.windowChange(attrs)
 
+# ugh...
+proc substituteUrl(pager: Pager, request: Request) =
+  let surl = $request.url
+  for sc in pager.siteconf:
+    if sc.url.exec(surl).success:
+      let s = sc.subst(surl)
+      if s.isSome:
+        let nurl = parseURL(s.get)
+        if nurl.isSome:
+          request.url = nurl.get
+      break
+
 # Load request in a new buffer.
 proc gotoURL*(pager: Pager, request: Request, prevurl = none(URL), ctype = none(string), replace: Container = nil) =
+  pager.substituteUrl(request)
   if prevurl.isnone or not prevurl.get.equals(request.url, true) or
       request.url.hash == "" or request.httpmethod != HTTP_GET:
     # Basically, we want to reload the page *only* when