diff options
author | Dominic Ward <dom@deeuu.me> | 2022-03-23 06:50:36 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-23 07:50:36 +0100 |
commit | a8b5ad845c4218b4f20595df097c593acee53d50 (patch) | |
tree | 86d5d05fdd2487c617ade8390d6236f1ef4c6e81 /lib | |
parent | 4c8934305c48bb52ed0e5a59b47229521cb333ce (diff) | |
download | Nim-a8b5ad845c4218b4f20595df097c593acee53d50.tar.gz |
Fix process lines iterator (#19605)
* Ensure lines when process done * eliminate post-EOF exit test * Recommend fixes for execCmdEx/execProcess
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/osproc.nim | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 6a0ac9a8b..3da9737ec 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -451,7 +451,7 @@ proc execProcesses*(cmds: openArray[string], if afterRunEvent != nil: afterRunEvent(i, p) close(p) -iterator lines*(p: Process): string {.since: (1, 3), tags: [ReadIOEffect].} = +iterator lines*(p: Process, keepNewLines = false): string {.since: (1, 3), tags: [ReadIOEffect].} = ## Convenience iterator for working with `startProcess` to read data from a ## background process. ## @@ -474,11 +474,11 @@ iterator lines*(p: Process): string {.since: (1, 3), tags: [ReadIOEffect].} = ## p.close var outp = p.outputStream var line = newStringOfCap(120) - while true: - if outp.readLine(line): - yield line - else: - if p.peekExitCode != -1: break + while outp.readLine(line): + if keepNewLines: + line.add("\n") + yield line + discard waitForExit(p) proc readLines*(p: Process): (seq[string], int) {.since: (1, 3).} = ## Convenience function for working with `startProcess` to read data from a @@ -514,6 +514,7 @@ when not defined(useNimRtl): var outp = outputStream(p) result = "" var line = newStringOfCap(120) + # consider `p.lines(keepNewLines=true)` to circumvent `running` busy-wait while true: # FIXME: converts CR-LF to LF. if outp.readLine(line): @@ -1622,6 +1623,7 @@ proc execCmdEx*(command: string, options: set[ProcessOption] = { inputStream(p).write(input) close inputStream(p) + # consider `p.lines(keepNewLines=true)` to avoid exit code test result = ("", -1) var line = newStringOfCap(120) while true: |