about summary refs log tree commit diff stats
path: root/src/display/client.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-03 19:08:35 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-03 19:08:35 +0100
commitae3bbcae299983eac514fa803d46e65058cbee39 (patch)
tree78e2bf5a96ecdbe60eb4eb5b02c180bdfd7f2b89 /src/display/client.nim
parent9528f502e7564ee647b39ebe02be1fb54c3c499d (diff)
downloadchawan-ae3bbcae299983eac514fa803d46e65058cbee39.tar.gz
Add event loop to headless mode
Diffstat (limited to 'src/display/client.nim')
-rw-r--r--src/display/client.nim24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/display/client.nim b/src/display/client.nim
index cee12cf1..86ef999c 100644
--- a/src/display/client.nim
+++ b/src/display/client.nim
@@ -307,6 +307,28 @@ proc inputLoop(client: Client) =
     client.pager.draw()
     client.acceptBuffers()
 
+proc headlessLoop(client: Client) =
+  while client.timeouts.len + client.intervals.len != 0:
+    let events = client.selector.select(-1)
+    for event in events:
+      if Read in event.events:
+        let container = client.fdmap[event.fd]
+        if not client.pager.handleEvent(container):
+          client.quit(1)
+      if Error in event.events:
+        #TODO handle errors
+        client.alert("Error in selected fds, check console")
+        client.console.log($event)
+      if Event.Timer in event.events:
+        if event.fd in client.interval_fdis:
+          client.intervals[client.interval_fdis[event.fd]].handler()
+        elif event.fd in client.timeout_fdis:
+          let id = client.timeout_fdis[event.fd]
+          let timeout = client.timeouts[id]
+          timeout.handler()
+          client.clearTimeout(id)
+    client.acceptBuffers()
+
 #TODO this is dumb
 proc readFile(client: Client, path: string): string {.jsfunc.} =
   try:
@@ -380,6 +402,8 @@ proc launchClient*(client: Client, pages: seq[string], ctype: Option[string], du
     client.inputLoop()
   else:
     client.dumpBuffers()
+  if client.config.headless:
+    client.headlessLoop()
   client.quit()
 
 proc nimGCStats(client: Client): string {.jsfunc.} =