Fix process lines iterator (#19605)
* Ensure lines when process done * eliminate post-EOF exit test * Recommend fixes for execCmdEx/execProcess
This commit is contained in:
parent
4c8934305c
commit
a8b5ad845c
1 changed files with 8 additions and 6 deletions
|
|
@ -451,7 +451,7 @@ proc execProcesses*(cmds: openArray[string],
|
||||||
if afterRunEvent != nil: afterRunEvent(i, p)
|
if afterRunEvent != nil: afterRunEvent(i, p)
|
||||||
close(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
|
## Convenience iterator for working with `startProcess` to read data from a
|
||||||
## background process.
|
## background process.
|
||||||
##
|
##
|
||||||
|
|
@ -474,11 +474,11 @@ iterator lines*(p: Process): string {.since: (1, 3), tags: [ReadIOEffect].} =
|
||||||
## p.close
|
## p.close
|
||||||
var outp = p.outputStream
|
var outp = p.outputStream
|
||||||
var line = newStringOfCap(120)
|
var line = newStringOfCap(120)
|
||||||
while true:
|
while outp.readLine(line):
|
||||||
if outp.readLine(line):
|
if keepNewLines:
|
||||||
yield line
|
line.add("\n")
|
||||||
else:
|
yield line
|
||||||
if p.peekExitCode != -1: break
|
discard waitForExit(p)
|
||||||
|
|
||||||
proc readLines*(p: Process): (seq[string], int) {.since: (1, 3).} =
|
proc readLines*(p: Process): (seq[string], int) {.since: (1, 3).} =
|
||||||
## Convenience function for working with `startProcess` to read data from a
|
## Convenience function for working with `startProcess` to read data from a
|
||||||
|
|
@ -514,6 +514,7 @@ when not defined(useNimRtl):
|
||||||
var outp = outputStream(p)
|
var outp = outputStream(p)
|
||||||
result = ""
|
result = ""
|
||||||
var line = newStringOfCap(120)
|
var line = newStringOfCap(120)
|
||||||
|
# consider `p.lines(keepNewLines=true)` to circumvent `running` busy-wait
|
||||||
while true:
|
while true:
|
||||||
# FIXME: converts CR-LF to LF.
|
# FIXME: converts CR-LF to LF.
|
||||||
if outp.readLine(line):
|
if outp.readLine(line):
|
||||||
|
|
@ -1622,6 +1623,7 @@ proc execCmdEx*(command: string, options: set[ProcessOption] = {
|
||||||
inputStream(p).write(input)
|
inputStream(p).write(input)
|
||||||
close inputStream(p)
|
close inputStream(p)
|
||||||
|
|
||||||
|
# consider `p.lines(keepNewLines=true)` to avoid exit code test
|
||||||
result = ("", -1)
|
result = ("", -1)
|
||||||
var line = newStringOfCap(120)
|
var line = newStringOfCap(120)
|
||||||
while true:
|
while true:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue