Skip to content

Commit 6a54f1c

Browse files
authored
Fix std/random bug (#204) (#211)
* Fix std/random bug - Remove OpenSystemsLab/tempfile.nim and replace with build-in `std/tempfiles` - Remove low level stuff that might break in newer Windows update and replace with build-in `fusion/ioutils` - Add ability to name the temp file * Reorder template and change from `when` to `if` * Update nimib.nimble * Remove filename overload
1 parent 68ce786 commit 6a54f1c

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

nimib.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ srcDir = "src"
99
# Dependencies
1010

1111
requires "nim >= 1.4.0"
12-
requires "tempfile >= 0.1.6"
12+
requires "fusion >= 1.2"
1313
requires "markdown >= 0.8.1"
1414
requires "mustache >= 0.2.1"
1515
requires "parsetoml >= 0.7.0"

src/nimib/capture.nim

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
## This submodule implements only one template to temporarily redirect and capture stdout during execution of a chunk of code.
2-
import tempfile
3-
4-
# see https://stackoverflow.com/questions/64026829/how-to-temporarily-capture-stdout
5-
# Thanks, Clonk!
6-
7-
# low level, should be Posix only but they happen to work on (my) Windows, too!
8-
proc dup(oldfd: FileHandle): FileHandle {.importc, header: "unistd.h".}
9-
proc dup2(oldfd: FileHandle, newfd: FileHandle): cint {.importc, header: "unistd.h".}
2+
import fusion/ioutils
3+
import std/tempfiles
104

115
template captureStdout*(ident: untyped, body: untyped) =
12-
## redirect stdout to a temporary file and captures output of body in ident
13-
let
6+
## redirect stdout to a temporary file and captures output of body in ident
147
# Duplicate stdout
15-
stdoutFileno = stdout.getFileHandle()
16-
stdoutDupfd = dup(stdoutFileno)
17-
# Create a new temporary file
18-
(tmpFile, tmpFilename) = mkstemp(mode=fmWrite)
19-
tmpFileFd: FileHandle = tmpFile.getFileHandle()
20-
discard dup2(tmpFileFd, stdoutFileno) # writing to stdoutFileno now writes to tmpFile
21-
22-
body
23-
flushFile stdout
24-
25-
# before reading tmpFile, flush and close
26-
tmpFile.flushFile()
27-
tmpFile.close()
28-
ident = readFile(tmpFileName)
29-
# Restore stdout
30-
discard dup2(stdoutDupfd, stdoutFileno)
8+
let stdoutFileno: FileHandle = stdout.getFileHandle()
9+
let stdoutDupFd: FileHandle = stdoutFileno.duplicate()
10+
11+
# Create a new temporary file or attempt to open it
12+
let (tmpFile, _) = createTempFile("tmp", "")
13+
let tmpFileFd: FileHandle = tmpFile.getFileHandle()
14+
15+
# writing to stdoutFileno now writes to tmpFile
16+
tmpFileFd.duplicateTo(stdoutFileno)
17+
18+
# Execute body code
19+
body
20+
21+
# Flush stdout and tmpFile, read tmpFile from start to ident and then close tmpFile
22+
stdout.flushFile()
23+
tmpFile.flushFile()
24+
tmpFile.setFilePos(0)
25+
ident = tmpFile.readAll()
26+
tmpFile.close()
27+
28+
# Restore stdout
29+
stdoutDupFd.duplicateTo(stdoutFileno)

0 commit comments

Comments
 (0)