Skip to content

Commit

Permalink
New macro @bindname to display variable name before bond (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Aug 25, 2024
1 parent 01877bf commit daed4df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PlutoUI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ end


include("./ScrubbableMatrix.jl")
include("./bindname.jl")

end
35 changes: 35 additions & 0 deletions src/bindname.jl
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))
)&nbsp<span style="opacity: .6">=</span>&nbsp</code>$(
bond
)</div>"""
)
end
end

export @bindname

0 comments on commit daed4df

Please sign in to comment.