@@ -4,7 +4,7 @@ import Random
4
4
5
5
"""
6
6
IOCapture.capture(
7
- f; rethrow=Any, color=false, passthrough=false, capture_buffer=IOBuffer()
7
+ f; rethrow=Any, color=false, passthrough=false, capture_buffer=IOBuffer(), io_context=[],
8
8
)
9
9
10
10
Runs the function `f` and captures the `stdout` and `stderr` outputs, without printing
@@ -39,6 +39,10 @@ The behaviour can be customized with the following keyword arguments:
39
39
* `capture_buffer`: The internal buffer used to capture the combined `stdout`
40
40
and `stderr`.
41
41
42
+ * `io_context`: An optional vector of `IOContext` key/value pairs that are passed to
43
+ the `IOContext` that wraps the redirected `stdout` and `stderr` streams. This only
44
+ has an effect on Julia v1.6 and later.
45
+
42
46
# Extended help
43
47
44
48
`capture` works by temporarily redirecting the standard output and error streams
@@ -98,8 +102,13 @@ function capture(
98
102
rethrow:: Type = Any,
99
103
color:: Bool = false ,
100
104
passthrough:: Bool = false ,
101
- capture_buffer= IOBuffer ()
105
+ capture_buffer= IOBuffer (),
106
+ io_context:: AbstractVector = [],
102
107
)
108
+ if any (x -> ! isa (x, Pair{Symbol,<: Any }), io_context)
109
+ throw (ArgumentError (" `io_context` must be a `Vector` of `Pair{Symbol,<:Any}`." ))
110
+ end
111
+
103
112
# Original implementation from Documenter.jl (MIT license)
104
113
# Save the default output streams.
105
114
default_stdout = stdout
@@ -109,8 +118,8 @@ function capture(
109
118
pipe = Pipe ()
110
119
Base. link_pipe! (pipe; reader_supports_async = true , writer_supports_async = true )
111
120
@static if VERSION >= v " 1.6.0-DEV.481" # https://github.com/JuliaLang/julia/pull/36688
112
- pe_stdout = IOContext (pipe. in, :color => get (stdout , :color , false ) & color)
113
- pe_stderr = IOContext (pipe. in, :color => get (stderr , :color , false ) & color)
121
+ pe_stdout = IOContext (pipe. in, :color => get (stdout , :color , false ) & color, io_context ... )
122
+ pe_stderr = IOContext (pipe. in, :color => get (stderr , :color , false ) & color, io_context ... )
114
123
else
115
124
pe_stdout = pipe. in
116
125
pe_stderr = pipe. in
0 commit comments