about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/extern/runproc.nim13
-rw-r--r--src/local/pager.nim11
2 files changed, 21 insertions, 3 deletions
diff --git a/src/extern/runproc.nim b/src/extern/runproc.nim
index c49d98db..fa1ed9bc 100644
--- a/src/extern/runproc.nim
+++ b/src/extern/runproc.nim
@@ -1,3 +1,4 @@
+import streams
 import posix
 
 import display/term
@@ -27,3 +28,15 @@ proc runProcess*(term: Terminal, cmd: string, wait = false): bool =
   if wait:
     term.anyKey()
   term.restart()
+
+# Run process, and capture its output.
+proc runProcessCapture*(term: Terminal, cmd: string, outs: var string): bool =
+  let file = popen(cmd, "r")
+  if file == nil:
+    return false
+  let fs = newFileStream(file)
+  outs = fs.readAll()
+  let rv = pclose(file)
+  if rv == -1:
+    return false
+  return rv == 0
diff --git a/src/local/pager.nim b/src/local/pager.nim
index d4853d31..dce66505 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -854,9 +854,7 @@ type ExternDict = object of JSDict
   suspend: Opt[bool]
   wait: bool
 
-#TODO this could be handled much better.
-# * suspend, setenv, wait as dict flags
-# * retval as int?
+#TODO we should have versions with retval as int?
 proc extern(pager: Pager, cmd: string, t = ExternDict()): bool {.jsfunc.} =
   if t.setenv.get(true):
     pager.setEnvVars()
@@ -865,6 +863,13 @@ proc extern(pager: Pager, cmd: string, t = ExternDict()): bool {.jsfunc.} =
   else:
     return runProcess(cmd)
 
+proc externCapture(pager: Pager, cmd: string): Opt[string] {.jsfunc.} =
+  pager.setEnvVars()
+  var s: string
+  if not runProcessCapture(pager.term, cmd, s):
+    return err()
+  return ok(s)
+
 proc authorize(pager: Pager) =
   pager.setLineEdit("Username: ", USERNAME)