-
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.
New macro
@bindname
to display variable name before bond (#305)
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -79,5 +79,6 @@ end | |
|
||
|
||
include("./ScrubbableMatrix.jl") | ||
include("./bindname.jl") | ||
|
||
end |
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,35 @@ | ||
import HypertextLiteral | ||
|
||
""" | ||
```julia | ||
@bindname name Slider(1:10) | ||
``` | ||
Like `@bind` in Pluto, but it also displays the name of the variable before the input widget. | ||
""" | ||
macro bindname(name::Symbol, ex::Expr) | ||
|
||
# Some macro magic to call the `@bind` macro without hygiene. This will use whatever `@bind` is defined in the scope of the caller of this macro. | ||
# We could just do `Main.PlutoRunner.@bind`, but then if you run the notebook.jl as a standalone script, it does not find the mock `@bind`. | ||
bindcall = Expr(:macrocall, | ||
Symbol("@bind"), | ||
__source__, | ||
name, | ||
ex | ||
) | ||
|
||
# Messy HTML to avoid unintended whitespace, which can show up in rare cases. | ||
quote | ||
bond = $(esc(bindcall)) | ||
|
||
HypertextLiteral.@htl( | ||
"""<div style='display: flex; flex-wrap: wrap; align-items: baseline;'><code style='color: var(--cm-color-var) !important; font-weight: 700;'>$( | ||
$(String(name)) | ||
) <span style="opacity: .6">=</span> </code>$( | ||
bond | ||
)</div>""" | ||
) | ||
end | ||
end | ||
|
||
export @bindname |