-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
with_terminal to capture stdout and stdin
Co-authored-by: Juergen Fuhrmann <[email protected]> Co-authored-by: fonsp <[email protected]>
- Loading branch information
1 parent
8042283
commit 70586ae
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
name = "PlutoUI" | ||
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8" | ||
authors = ["Fons van der Plas <[email protected]>"] | ||
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = """ | ||
<style> | ||
div.PlutoUI_terminal { | ||
border: 5px solid pink; | ||
border-radius: 12px; | ||
font-size: .65rem; | ||
background-color: #333; | ||
} | ||
div.PlutoUI_terminal pre { | ||
color: #ddd; | ||
background-color: transparent; | ||
margin-block-end: 0; | ||
} | ||
div.PlutoUI_terminal pre.err { | ||
color: #ff5f5f; | ||
} | ||
</style> | ||
""" | ||
|
||
""" | ||
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) | ||
<div class="PlutoUI_terminal"> | ||
<pre>$(htmlesc(spam_out))</pre> | ||
$(isempty(spam_err) ? "" : "<pre class='err'>" * htmlesc(spam_err) * "</pre>") | ||
</div> | ||
""") | ||
end |
70586ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
70586ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/21652
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: