From 6435c33d45d3923149d95fb58bb2343d0483788d Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Thu, 10 Sep 2020 11:57:22 +0200 Subject: [PATCH 01/11] Add code control (after an idea of Benjamin Lungwitz) and output grabbing --- Project.toml | 3 + src/CodeControl.jl | 156 +++++++++++++++++++++++++++++++++++++++++++++ src/PlutoUI.jl | 2 + 3 files changed, 161 insertions(+) create mode 100644 src/CodeControl.jl diff --git a/Project.toml b/Project.toml index 9d2b0575..667945c2 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,9 @@ version = "0.6.1" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" [compat] julia = "^1" +Suppressor = "^0.2" + diff --git a/src/CodeControl.jl b/src/CodeControl.jl new file mode 100644 index 00000000..cc9e8684 --- /dev/null +++ b/src/CodeControl.jl @@ -0,0 +1,156 @@ +export @with_stdout, @cond, @cond_with_stdout, @bind_cond, stdout_style, stdout_styles + +""" + Dict containing predefined style sheets for `div.stdout` +""" +_predefined_css=Dict( + + :default =>"""""", + + :nowrap =>"""""", + + :vintage =>"""""") + +# Initialize with default css +_stdout_css=_predefined_css[:default] + +""" + Set style sheet for output of stdout. Either one of + $(keys(_predefined_css)) or a string containing a css style sheet. +""" +function stdout_style(s) + global _stdout_css + if s in keys(_predefined_css) + _stdout_css=_predefined_css[s] + else + _stdout_css=s + end +end + + +""" + Return current stdout css style string +""" +stdout_style()=_stdout_css + +""" + List predefined stdout css styles +""" +stdout_styles()=keys(_predefined_css) + + +""" +Wrap code output into HTML
 element.
+"""
+format_stdout(code_output)=HTML("""$(_stdout_css)
"""*code_output*"""
""") + + +""" +Run code, grab output from println, show etc and return it in a html string as
.
+
+Example:
+
+````
+@with_stdout begin
+    x=1+1
+    println(x)
+end 
+````
+           
+"""
+macro with_stdout(expr)
+    quote
+	format_stdout(Suppressor.@capture_out($(esc(expr))))
+    end
+end
+
+
+"""
+Conditionally run code, grab output from println, show etc and return it in a html string as 
.
+
+Example:
+
+````
+md"### Test Example \$(@bind test_example CheckBox(default=false))"
+
+@with_stdout test_example begin
+    x=1+1
+    println(x)
+end 
+````
+           
+"""
+macro cond_with_stdout(run,expr)
+    # after an Idea of Benjamin Lungwitz
+    quote
+	if $(esc(run))
+	    format_stdout(Suppressor.@capture_out($(esc(expr))))
+	end
+    end
+end
+
+"""
+Conditionally run code
+
+Example:
+
+````
+md"### Test Example \$(@bind test_example CheckBox(default=false))"
+
+@cond test_example begin
+    x=1+1
+end 
+````
+           
+"""
+macro cond(run,expr)
+    # after an Idea of Benjamin Lungwitz
+    quote
+	if $(esc(run))
+	    $(esc(expr))
+	end
+    end
+end
+
+
+"""
+Wanted:
+
+Bind condition variable to checkbox and display it prefixed with label
+
+Example: 
+````
+@bind_cont test_example "### Test Example"
+````
+"""
diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl
index 9aca715a..89023831 100644
--- a/src/PlutoUI.jl
+++ b/src/PlutoUI.jl
@@ -2,11 +2,13 @@ module PlutoUI
 
 import Base: show, get
 import Markdown: htmlesc, withtag
+import Suppressor
 
 const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
 
 include("./Builtins.jl")
 include("./Clock.jl")
 include("./Resource.jl")
+include("./CodeControl.jl")
 
 end

From 05b919db2514e59c235e92a51679c5ec06aef63f Mon Sep 17 00:00:00 2001
From: Juergen Fuhrmann 
Date: Thu, 10 Sep 2020 18:08:21 +0200
Subject: [PATCH 02/11] Removed @cond_with_stdout, style setting. Added stderr
 grabbing

Keeping @cond though so far.
---
 Project.toml       |   3 -
 src/CodeControl.jl | 184 ++++++++++++++++++++-------------------------
 src/PlutoUI.jl     |   1 -
 3 files changed, 82 insertions(+), 106 deletions(-)

diff --git a/Project.toml b/Project.toml
index 667945c2..9d2b0575 100644
--- a/Project.toml
+++ b/Project.toml
@@ -7,9 +7,6 @@ version = "0.6.1"
 Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
 Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
 Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
-Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
 
 [compat]
 julia = "^1"
-Suppressor = "^0.2"
-
diff --git a/src/CodeControl.jl b/src/CodeControl.jl
index cc9e8684..16cca188 100644
--- a/src/CodeControl.jl
+++ b/src/CodeControl.jl
@@ -1,80 +1,94 @@
-export @with_stdout, @cond, @cond_with_stdout, @bind_cond, stdout_style, stdout_styles
+export @with_output, @cond, @capture
 
-"""
-     Dict containing predefined style sheets for `div.stdout`
-"""
-_predefined_css=Dict(
-
-    :default =>"""""",
-
-    :nowrap =>"""""",
-
-    :vintage =>"""""")
-
-# Initialize with default css
-_stdout_css=_predefined_css[:default]
 
-"""
-   Set style sheet for output of stdout. Either one of
-   $(keys(_predefined_css)) or a string containing a css style sheet.
-"""
-function stdout_style(s)
-    global _stdout_css
-    if s in keys(_predefined_css)
-        _stdout_css=_predefined_css[s]
-    else
-        _stdout_css=s
-    end
-end
+const _stdout_css="""
+"""
+
+const _stderr_css="""
+"""
 
 
-"""
-    Return current stdout css style string
-"""
-stdout_style()=_stdout_css
 
 """
-    List predefined stdout css styles
+    @capture expr
+
+Capture the `output` and `stderr` streams for the given expression,
+return a tuple of stdout and stderr.
 """
-stdout_styles()=keys(_predefined_css)
+macro capture(block)
+    quote
+        if ccall(:jl_generating_output, Cint, ()) == 0
+            original_stdout = stdout
+            out_rd, out_wr = redirect_stdout()
+            out_reader = @async read(out_rd, String)
+
+            original_stderr = stderr
+            err_rd, err_wr = redirect_stderr()
+            err_reader = @async read(err_rd, String)
+            
+            # approach adapted from https://github.com/JuliaLang/IJulia.jl/pull/667/files
+            logstate = Base.CoreLogging._global_logstate
+            logger = logstate.logger
+            new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level))
+            Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate))
+        end
+        
+        try
+            $(esc(block))
+        finally
+            if ccall(:jl_generating_output, Cint, ()) == 0
+                redirect_stdout(original_stdout)
+                close(out_wr)
+
+                redirect_stderr(original_stderr)
+                close(err_wr)
+                Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), logstate))
+            end
+        end
+
+        if ccall(:jl_generating_output, Cint, ()) == 0
+            (fetch(out_reader),fetch(err_reader))
+        else
+            ("","")
+        end
+    end
+end
+
 
 
 """
 Wrap code output into HTML 
 element.
 """
-format_stdout(code_output)=HTML("""$(_stdout_css)
"""*code_output*"""
""") - +function format_output(code_output) + output="" + @warn code_output[1] + @warn code_output[2] + if length(code_output[1])>0 + output="""$(_stdout_css)
"""*code_output[1]*"""
""" + end + if length(code_output[2])>0 + output=output*"""$(_stderr_css)
"""*code_output[2]*"""
""" + end + HTML(output) +end """ Run code, grab output from println, show etc and return it in a html string as
.
@@ -82,43 +96,20 @@ Run code, grab output from println, show etc and return it in a html string as <
 Example:
 
 ````
-@with_stdout begin
+@with_output begin
     x=1+1
     println(x)
 end 
 ````
            
 """
-macro with_stdout(expr)
+macro with_output(expr)
     quote
-	format_stdout(Suppressor.@capture_out($(esc(expr))))
+	format_output(@capture($(esc(expr))))
     end
 end
 
 
-"""
-Conditionally run code, grab output from println, show etc and return it in a html string as 
.
-
-Example:
-
-````
-md"### Test Example \$(@bind test_example CheckBox(default=false))"
-
-@with_stdout test_example begin
-    x=1+1
-    println(x)
-end 
-````
-           
-"""
-macro cond_with_stdout(run,expr)
-    # after an Idea of Benjamin Lungwitz
-    quote
-	if $(esc(run))
-	    format_stdout(Suppressor.@capture_out($(esc(expr))))
-	end
-    end
-end
 
 """
 Conditionally run code
@@ -143,14 +134,3 @@ macro cond(run,expr)
     end
 end
 
-
-"""
-Wanted:
-
-Bind condition variable to checkbox and display it prefixed with label
-
-Example: 
-````
-@bind_cont test_example "### Test Example"
-````
-"""
diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl
index 89023831..5012d9a0 100644
--- a/src/PlutoUI.jl
+++ b/src/PlutoUI.jl
@@ -2,7 +2,6 @@ module PlutoUI
 
 import Base: show, get
 import Markdown: htmlesc, withtag
-import Suppressor
 
 const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
 

From 8dab15025132b8a7de58c41aa270915ba791ca95 Mon Sep 17 00:00:00 2001
From: Juergen Fuhrmann 
Date: Thu, 10 Sep 2020 21:15:51 +0200
Subject: [PATCH 03/11] Renamed CodeControl.jl to Capture.jl

---
 src/{CodeControl.jl => Capture.jl} | 0
 src/PlutoUI.jl                     | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename src/{CodeControl.jl => Capture.jl} (100%)

diff --git a/src/CodeControl.jl b/src/Capture.jl
similarity index 100%
rename from src/CodeControl.jl
rename to src/Capture.jl
diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl
index 5012d9a0..463bf5c1 100644
--- a/src/PlutoUI.jl
+++ b/src/PlutoUI.jl
@@ -8,6 +8,6 @@ const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
 include("./Builtins.jl")
 include("./Clock.jl")
 include("./Resource.jl")
-include("./CodeControl.jl")
+include("./Capture.jl")
 
 end

From 3a1e811f0c597dcbac9227a8e69658f3b3df8afe Mon Sep 17 00:00:00 2001
From: Juergen Fuhrmann 
Date: Thu, 10 Sep 2020 22:55:33 +0200
Subject: [PATCH 04/11] Oops - removed the test output

---
 src/Capture.jl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Capture.jl b/src/Capture.jl
index 16cca188..205fe082 100644
--- a/src/Capture.jl
+++ b/src/Capture.jl
@@ -32,6 +32,8 @@ const _stderr_css="""
 
 Capture the `output` and `stderr` streams for the given expression,
 return a tuple of stdout and stderr.
+
+Taken from Suppressor.jl and modified. 
 """
 macro capture(block)
     quote
@@ -79,8 +81,6 @@ Wrap code output into HTML 
 element.
 """
 function format_output(code_output)
     output=""
-    @warn code_output[1]
-    @warn code_output[2]
     if length(code_output[1])>0
         output="""$(_stdout_css)
"""*code_output[1]*"""
""" end From 9cbf04e4eff9e8f8780c53a0f0c0dc415d584991 Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Sat, 12 Sep 2020 00:26:18 +0200 Subject: [PATCH 05/11] capture macro without undocumented voodoo --- src/Capture.jl | 61 +++++++++++++++++++------------------------------- src/PlutoUI.jl | 1 + 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/Capture.jl b/src/Capture.jl index 205fe082..75b29315 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -31,46 +31,31 @@ const _stderr_css=""" @capture expr Capture the `output` and `stderr` streams for the given expression, -return a tuple of stdout and stderr. - -Taken from Suppressor.jl and modified. +return a tuple of stdout and stderr results collected while executing +`expr` """ -macro capture(block) +macro capture(expr) quote - if ccall(:jl_generating_output, Cint, ()) == 0 - original_stdout = stdout - out_rd, out_wr = redirect_stdout() - out_reader = @async read(out_rd, String) - - original_stderr = stderr - err_rd, err_wr = redirect_stderr() - err_reader = @async read(err_rd, String) - - # approach adapted from https://github.com/JuliaLang/IJulia.jl/pull/667/files - logstate = Base.CoreLogging._global_logstate - logger = logstate.logger - new_logstate = Base.CoreLogging.LogState(typeof(logger)(err_wr, logger.min_level)) - Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), new_logstate)) - end - - try - $(esc(block)) - finally - if ccall(:jl_generating_output, Cint, ()) == 0 - redirect_stdout(original_stdout) - close(out_wr) - - redirect_stderr(original_stderr) - close(err_wr) - Core.eval(Base.CoreLogging, Expr(:(=), :(_global_logstate), logstate)) - end - end - - if ccall(:jl_generating_output, Cint, ()) == 0 - (fetch(out_reader),fetch(err_reader)) - else - ("","") - end + original_stdout = stdout + out_rd, out_wr = redirect_stdout() + original_stderr = stderr + err_rd, err_wr = redirect_stderr() + # write just one character into the streams in order to + # prevent readavailable from blocking if they would stay empty + print(stderr," ") + print(stdout," ") + logger=SimpleLogger() + with_logger(logger) do + $(esc(expr)) + end + result_out=String(readavailable(out_rd)) + result_err=String(readavailable(err_rd)) + redirect_stdout(original_stdout) + redirect_stderr(original_stderr) + close(out_wr) + close(err_wr) + # ignore the first character... + (result_out[2:end],result_err[2:end]) end end diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl index 463bf5c1..bdf55d71 100644 --- a/src/PlutoUI.jl +++ b/src/PlutoUI.jl @@ -2,6 +2,7 @@ module PlutoUI import Base: show, get import Markdown: htmlesc, withtag +import Logging: SimpleLogger, with_logger const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, "..")) From 9fa75556b32f3119bd44b1b038280f7db0454d29 Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Sun, 13 Sep 2020 19:37:13 +0200 Subject: [PATCH 06/11] Redirect stderr and stdout to the same buffer --- Project.toml | 1 + src/Capture.jl | 48 ++++++++++++++---------------------------------- 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/Project.toml b/Project.toml index 9d2b0575..ee24e496 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.6.1" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" diff --git a/src/Capture.jl b/src/Capture.jl index 75b29315..e89977b6 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -1,10 +1,10 @@ export @with_output, @cond, @capture -const _stdout_css=""" +const _output_css=""" """ -const _stderr_css=""" -""" @@ -31,31 +20,25 @@ const _stderr_css=""" @capture expr Capture the `output` and `stderr` streams for the given expression, -return a tuple of stdout and stderr results collected while executing -`expr` +return results collected while executing `expr` """ macro capture(expr) quote original_stdout = stdout out_rd, out_wr = redirect_stdout() - original_stderr = stderr - err_rd, err_wr = redirect_stderr() - # write just one character into the streams in order to - # prevent readavailable from blocking if they would stay empty - print(stderr," ") - print(stdout," ") - logger=SimpleLogger() - with_logger(logger) do - $(esc(expr)) + # Write just one character into the streams in order to + # prevent readavailable from blocking if if stays empty + print(stdout," ") + # Redirect both logging output and print(stderr,...) + # to stdout + with_logger(SimpleLogger(stdout)) do + redirect_stderr(()->$(esc(expr)),stdout) end result_out=String(readavailable(out_rd)) - result_err=String(readavailable(err_rd)) redirect_stdout(original_stdout) - redirect_stderr(original_stderr) close(out_wr) - close(err_wr) # ignore the first character... - (result_out[2:end],result_err[2:end]) + result_out[2:end] end end @@ -66,11 +49,8 @@ Wrap code output into HTML
 element.
 """
 function format_output(code_output)
     output=""
-    if length(code_output[1])>0
-        output="""$(_stdout_css)
"""*code_output[1]*"""
""" - end - if length(code_output[2])>0 - output=output*"""$(_stderr_css)
"""*code_output[2]*"""
""" + if length(code_output)>0 + output="""$(_output_css)
"""*code_output*"""
""" end HTML(output) end From 65a271f4daee015346faa2e2632063cb61df2e89 Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Mon, 14 Sep 2020 11:33:57 +0200 Subject: [PATCH 07/11] Use async read + fetch, collect stderr and stdout into one string --- src/Capture.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Capture.jl b/src/Capture.jl index e89977b6..fbee4df2 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -1,4 +1,4 @@ -export @with_output, @cond, @capture +export @with_output, @cond, @capture, @xcapture const _output_css=""" @@ -26,19 +26,15 @@ macro capture(expr) quote original_stdout = stdout out_rd, out_wr = redirect_stdout() - # Write just one character into the streams in order to - # prevent readavailable from blocking if if stays empty - print(stdout," ") + reader = @async read(out_rd) # Redirect both logging output and print(stderr,...) # to stdout with_logger(SimpleLogger(stdout)) do redirect_stderr(()->$(esc(expr)),stdout) end - result_out=String(readavailable(out_rd)) redirect_stdout(original_stdout) close(out_wr) - # ignore the first character... - result_out[2:end] + String(fetch(reader)) end end From 1a194cc982198334af9794fc8d80a5a9992ff9ab Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Mon, 14 Sep 2020 11:42:17 +0200 Subject: [PATCH 08/11] Add try/finally to @capture --- src/Capture.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Capture.jl b/src/Capture.jl index fbee4df2..a4202b79 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -27,13 +27,16 @@ macro capture(expr) original_stdout = stdout out_rd, out_wr = redirect_stdout() reader = @async read(out_rd) - # Redirect both logging output and print(stderr,...) - # to stdout - with_logger(SimpleLogger(stdout)) do - redirect_stderr(()->$(esc(expr)),stdout) - end - redirect_stdout(original_stdout) - close(out_wr) + try + # Redirect both logging output and print(stderr,...) + # to stdout + with_logger(SimpleLogger(stdout)) do + redirect_stderr(()->$(esc(expr)),stdout) + end + finally + redirect_stdout(original_stdout) + close(out_wr) + end String(fetch(reader)) end end From df891107f46b968df17c77c736753cf4ac821475 Mon Sep 17 00:00:00 2001 From: Juergen Fuhrmann Date: Tue, 15 Sep 2020 00:10:35 +0200 Subject: [PATCH 09/11] use ConsoleLogger instead of SimpleLogger ConsoleLogger defaults to not printing context info with @info messages Also use now read(stream, String). --- src/Capture.jl | 10 +++++----- src/PlutoUI.jl | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Capture.jl b/src/Capture.jl index a4202b79..bd28104a 100644 --- a/src/Capture.jl +++ b/src/Capture.jl @@ -1,4 +1,4 @@ -export @with_output, @cond, @capture, @xcapture +export @with_output, @cond, @capture const _output_css=""" @@ -26,18 +26,18 @@ macro capture(expr) quote original_stdout = stdout out_rd, out_wr = redirect_stdout() - reader = @async read(out_rd) + reader = @async read(out_rd, String) try # Redirect both logging output and print(stderr,...) # to stdout - with_logger(SimpleLogger(stdout)) do + with_logger(ConsoleLogger(stdout)) do redirect_stderr(()->$(esc(expr)),stdout) end finally redirect_stdout(original_stdout) close(out_wr) end - String(fetch(reader)) + fetch(reader) end end @@ -49,7 +49,7 @@ Wrap code output into HTML
 element.
 function format_output(code_output)
     output=""
     if length(code_output)>0
-        output="""$(_output_css)
"""*code_output*"""
""" + output=_output_css*"""
"""*code_output*"""
""" end HTML(output) end diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl index bdf55d71..3e61ca3c 100644 --- a/src/PlutoUI.jl +++ b/src/PlutoUI.jl @@ -2,7 +2,7 @@ module PlutoUI import Base: show, get import Markdown: htmlesc, withtag -import Logging: SimpleLogger, with_logger +import Logging: ConsoleLogger, with_logger const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, "..")) From 12570e70c6aa434b254f13ddb410031be3b4590a Mon Sep 17 00:00:00 2001 From: fonsp Date: Sun, 20 Sep 2020 12:12:49 +0000 Subject: [PATCH 10/11] function instead of macro, HTML escaping, removed @cond, using Suppressor --- Project.toml | 2 + src/Capture.jl | 100 ------------------------------------------------ src/PlutoUI.jl | 2 +- src/Terminal.jl | 57 +++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 101 deletions(-) delete mode 100644 src/Capture.jl create mode 100644 src/Terminal.jl diff --git a/Project.toml b/Project.toml index ee24e496..b3b544c5 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,8 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" 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/Capture.jl b/src/Capture.jl deleted file mode 100644 index bd28104a..00000000 --- a/src/Capture.jl +++ /dev/null @@ -1,100 +0,0 @@ -export @with_output, @cond, @capture - - -const _output_css=""" -""" - - - - -""" - @capture expr - -Capture the `output` and `stderr` streams for the given expression, -return results collected while executing `expr` -""" -macro capture(expr) - quote - original_stdout = stdout - out_rd, out_wr = redirect_stdout() - reader = @async read(out_rd, String) - try - # Redirect both logging output and print(stderr,...) - # to stdout - with_logger(ConsoleLogger(stdout)) do - redirect_stderr(()->$(esc(expr)),stdout) - end - finally - redirect_stdout(original_stdout) - close(out_wr) - end - fetch(reader) - end -end - - - -""" -Wrap code output into HTML
 element.
-"""
-function format_output(code_output)
-    output=""
-    if length(code_output)>0
-        output=_output_css*"""
"""*code_output*"""
""" - end - HTML(output) -end - -""" -Run code, grab output from println, show etc and return it in a html string as
.
-
-Example:
-
-````
-@with_output begin
-    x=1+1
-    println(x)
-end 
-````
-           
-"""
-macro with_output(expr)
-    quote
-	format_output(@capture($(esc(expr))))
-    end
-end
-
-
-
-"""
-Conditionally run code
-
-Example:
-
-````
-md"### Test Example \$(@bind test_example CheckBox(default=false))"
-
-@cond test_example begin
-    x=1+1
-end 
-````
-           
-"""
-macro cond(run,expr)
-    # after an Idea of Benjamin Lungwitz
-    quote
-	if $(esc(run))
-	    $(esc(expr))
-	end
-    end
-end
-
diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl
index 3e61ca3c..e84ed673 100644
--- a/src/PlutoUI.jl
+++ b/src/PlutoUI.jl
@@ -9,6 +9,6 @@ const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, ".."))
 include("./Builtins.jl")
 include("./Clock.jl")
 include("./Resource.jl")
-include("./Capture.jl")
+include("./Terminal.jl")
 
 end
diff --git a/src/Terminal.jl b/src/Terminal.jl
new file mode 100644
index 00000000..e3c9a732
--- /dev/null
+++ b/src/Terminal.jl
@@ -0,0 +1,57 @@
+export with_terminal
+
+import Suppressor: @color_output, @capture_out, @capture_err
+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)
+end 
+```
+           
+"""
+function with_terminal(f::Function)
+    local spam_out, spam_err
+	@color_output false begin
+		spam_out = @capture_out begin
+			spam_err = @capture_err begin
+				f()
+			end
+		end
+    end
+	
+	HTML("""
+		$(terminal_css)
+        
+
$(htmlesc(spam_out))
+ $(isempty(spam_err) ? "" : "
" * htmlesc(spam_err) * "
") +
+ """) +end \ No newline at end of file From 46995c051b4f87a8fc5bf41435c722ee3120e429 Mon Sep 17 00:00:00 2001 From: fonsp Date: Sun, 20 Sep 2020 12:27:19 +0000 Subject: [PATCH 11/11] Added back with_logger --- src/PlutoUI.jl | 1 - src/Terminal.jl | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl index e84ed673..9e05d633 100644 --- a/src/PlutoUI.jl +++ b/src/PlutoUI.jl @@ -2,7 +2,6 @@ module PlutoUI import Base: show, get import Markdown: htmlesc, withtag -import Logging: ConsoleLogger, with_logger const PKG_ROOT_DIR = normpath(joinpath(@__DIR__, "..")) diff --git a/src/Terminal.jl b/src/Terminal.jl index e3c9a732..e2b927b2 100644 --- a/src/Terminal.jl +++ b/src/Terminal.jl @@ -1,6 +1,7 @@ export with_terminal import Suppressor: @color_output, @capture_out, @capture_err +import Logging: ConsoleLogger, with_logger import Markdown: htmlesc const terminal_css = """ @@ -33,16 +34,23 @@ Example: with_terminal() do x=1+1 println(x) + @warn "Oopsie!" end ``` + +```julia +with_terminal(dump, [1,2,[3,4]]) +``` """ -function with_terminal(f::Function) +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 - f() + spam_err = @capture_err begin + with_logger(ConsoleLogger(stdout)) do + f(args...; kwargs...) + end end end end