From 70586ae907aed067358e8c31139df083465771eb Mon Sep 17 00:00:00 2001 From: j-fu Date: Sun, 20 Sep 2020 14:30:19 +0200 Subject: [PATCH] with_terminal to capture stdout and stdin Co-authored-by: Juergen Fuhrmann Co-authored-by: fonsp --- Project.toml | 5 +++- src/PlutoUI.jl | 1 + src/Terminal.jl | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/Terminal.jl diff --git a/Project.toml b/Project.toml index a3908ab8..771feea7 100644 --- a/Project.toml +++ b/Project.toml @@ -1,13 +1,16 @@ name = "PlutoUI" uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8" authors = ["Fons van der Plas "] -version = "0.6.2" +version = "0.6.3" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" [compat] julia = "^1" +Suppressor = "^0.2.0" \ No newline at end of file diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl index 9aca715a..9e05d633 100644 --- a/src/PlutoUI.jl +++ b/src/PlutoUI.jl @@ -8,5 +8,6 @@ const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, "..")) include("./Builtins.jl") include("./Clock.jl") include("./Resource.jl") +include("./Terminal.jl") end diff --git a/src/Terminal.jl b/src/Terminal.jl new file mode 100644 index 00000000..e2b927b2 --- /dev/null +++ b/src/Terminal.jl @@ -0,0 +1,65 @@ +export with_terminal + +import Suppressor: @color_output, @capture_out, @capture_err +import Logging: ConsoleLogger, with_logger +import Markdown: htmlesc + +const terminal_css = """ + +""" + +""" +Run the function, and capture all messages to `stdout`. The result will be a small terminal displaying the captured text. + +This allows you to to see the messages from `println`, `dump`, `Pkg.status`, etc. + +Example: + +```julia +with_terminal() do + x=1+1 + println(x) + @warn "Oopsie!" +end +``` + +```julia +with_terminal(dump, [1,2,[3,4]]) +``` + +""" +function with_terminal(f::Function, args...; kwargs...) + local spam_out, spam_err + @color_output false begin + spam_out = @capture_out begin + spam_err = @capture_err begin + with_logger(ConsoleLogger(stdout)) do + f(args...; kwargs...) + end + end + end + end + + HTML(""" + $(terminal_css) +
+
$(htmlesc(spam_out))
+ $(isempty(spam_err) ? "" : "
" * htmlesc(spam_err) * "
") +
+ """) +end \ No newline at end of file