Remove deprecated TaintedString usage.

This commit is contained in:
Jonathan Bernard 2022-01-21 18:07:21 -06:00
parent 57bb7302b1
commit 224818f3c7
2 changed files with 14 additions and 15 deletions

View File

@ -1,10 +1,10 @@
# Package # Package
version = "0.6.5" version = "0.7.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Helper functions for writing command line interfaces." description = "Helper functions for writing command line interfaces."
license = "MIT" license = "MIT"
# Dependencies # Dependencies
requires @["nim >= 0.19.0", "docopt >= 0.6.8"] requires @["nim >= 1.6.0", "docopt >= 0.6.8"]

View File

@ -1,21 +1,20 @@
import osproc, sequtils, streams, strtabs import osproc, streams, strtabs, strutils
## Process execution ## Process execution
type HandleProcMsgCB* = proc (outMsg: TaintedString, type HandleProcMsgCB* = proc (outMsg: string, errMsg: string, cmd: string): void
errMsg: TaintedString, cmd: string): void
proc sendMsg*(h: HandleProcMsgCB, outMsg: TaintedString, proc sendMsg*(h: HandleProcMsgCB, outMsg: string,
errMsg: TaintedString = "", cmd: string = ""): void = errMsg: string = "", cmd: string = ""): void =
if h != nil: h(outMsg, errMsg, cmd) if h != nil: h(outMsg, errMsg, cmd)
proc makeProcMsgHandler*(outSink, errSink: File, prefixCmd: bool = true): HandleProcMsgCB = proc makeProcMsgHandler*(outSink, errSink: File, prefixCmd: bool = true): HandleProcMsgCB =
result = proc(outMsg, errMsg: TaintedString, cmd: string): void {.closure.} = result = proc(outMsg, errMsg: string, cmd: string): void {.closure.} =
let prefix = if cmd.len == 0 or not prefixCmd: "" else: cmd & ": " let prefix = if cmd.len == 0 or not prefixCmd: "" else: cmd & ": "
if outMsg.len > 0: outSink.writeLine(prefix & outMsg) if outMsg.len > 0: outSink.writeLine(prefix & outMsg)
if errMsg.len > 0: errSink.writeLine(prefix & errMsg) if errMsg.len > 0: errSink.writeLine(prefix & errMsg)
proc makeProcMsgHandler*(outSink, errSink: Stream, prefixCmd: bool = true): HandleProcMsgCB = proc makeProcMsgHandler*(outSink, errSink: Stream, prefixCmd: bool = true): HandleProcMsgCB =
result = proc(outMsg, errMsg: TaintedString, cmd: string): void {.closure.} = result = proc(outMsg, errMsg: string, cmd: string): void {.closure.} =
let prefix = if cmd.len == 0 or not prefixCmd: "" else: cmd & ": " let prefix = if cmd.len == 0 or not prefixCmd: "" else: cmd & ": "
if outMsg.len > 0: outSink.writeLine(prefix & outMsg) if outMsg.len > 0: outSink.writeLine(prefix & outMsg)
if errMsg.len > 0: errSink.writeLine(prefix & errMsg) if errMsg.len > 0: errSink.writeLine(prefix & errMsg)
@ -24,7 +23,7 @@ proc combineProcMsgHandlers*(a, b: HandleProcMsgCB): HandleProcMsgCB =
if a == nil: result = b if a == nil: result = b
elif b == nil: result = a elif b == nil: result = a
else: else:
result = proc(cmd: string, outMsg, errMsg: TaintedString): void = result = proc(cmd: string, outMsg, errMsg: string): void =
a(cmd, outMsg, errMsg) a(cmd, outMsg, errMsg)
b(cmd, outMsg, errMsg) b(cmd, outMsg, errMsg)
@ -33,7 +32,7 @@ proc waitFor*(p: Process, msgCB: HandleProcMsgCB, procCmd: string = ""): int =
var pout = outputStream(p) var pout = outputStream(p)
var perr = errorStream(p) var perr = errorStream(p)
var line = newStringOfCap(120).TaintedString var line = newStringOfCap(120)
while true: while true:
if pout.readLine(line): if pout.readLine(line):
msgCB.sendMsg(line, "", procCmd) msgCB.sendMsg(line, "", procCmd)
@ -57,13 +56,13 @@ proc execWithOutput*(command: string, workingDir:string = "",
args: openArray[string] = [], env: StringTableRef = nil, args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poUsePath}, options: set[ProcessOption] = {poUsePath},
msgCB: HandleProcMsgCB = nil): msgCB: HandleProcMsgCB = nil):
tuple[output, error: TaintedString, exitCode: int] = tuple[output, error: string, exitCode: int] =
result = (TaintedString"", TaintedString"", -1) result = ("", "", -1)
var outSeq, errSeq: seq[TaintedString] var outSeq, errSeq: seq[string]
outSeq = @[]; errSeq = @[] outSeq = @[]; errSeq = @[]
let outputCollector = combineProcMsgHandlers(msgCB, let outputCollector = combineProcMsgHandlers(msgCB,
proc(outMsg, errMsg: TaintedString, cmd: string): void {.closure.} = proc(outMsg, errMsg: string, cmd: string): void {.closure.} =
if outMsg.len > 0: outSeq.add(outMsg) if outMsg.len > 0: outSeq.add(outMsg)
if errMsg.len > 0: errSeq.add(errMsg)) if errMsg.len > 0: errSeq.add(errMsg))