Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ uuid = "19e8930c-b694-45c7-b48f-ae1a17da5712"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.1.0"

[deps]
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"

[compat]
BackendSelection = "0.1.5"
julia = "1.10"
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ julia> Pkg.add("VisualizationBase")
## Examples

````julia
using VisualizationBase: VisualizationBase
````
using VisualizationBase: @visualize
using Test: @test

Examples go here.
x = [2, 3]
y = @visualize x
@test x === y
````

---

Expand Down
8 changes: 6 additions & 2 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ julia> Pkg.add("VisualizationBase")

# ## Examples

using VisualizationBase: VisualizationBase
# Examples go here.
using VisualizationBase: @visualize
using Test: @test

x = [2, 3]
y = @visualize x
@test x === y
72 changes: 71 additions & 1 deletion src/VisualizationBase.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
module VisualizationBase

# Write your package code here.
using BackendSelection: @Backend_str, AbstractBackend, Backend

export @visualize, visualize

macro visualize(x, kwargs...)
all(kwarg -> Meta.isexpr(kwarg, :(=)), kwargs) ||
throw(ArgumentError("All keyword arguments must be of the form `kw=value`."))
kwargs′ = if all(kwarg -> kwarg.args[1] ≠ :name, kwargs)
(kwargs..., :(name=$(string(x))))
else
kwargs
end
return :(visualize($(esc(x)); $(esc.(kwargs′)...)); $(esc(x)))
end

function visualize(io::IO, x; backend=Backend"Default", name=nothing, kwargs...)
return visualize(io, to_backend(backend; kwargs...), x; name)
end

function visualize(x; backend=Backend"Default", name=nothing, io::IO=stdout, kwargs...)
return visualize(io, to_backend(backend; kwargs...), x; name)
end

function visualize(io::IO, backend::AbstractBackend, x; name=nothing)
name = get(backend, :name, name)
if isnothing(name)
visualize_unnamed(io, backend, x)
return nothing
end
visualize_named(io, backend, name, x)
return nothing
end

function visualize(backend::Backend"Default", x; name=nothing)
io = get(backend, :io, stdout)
visualize(io, backend, x; name)
return nothing
end

function visualize_named(io::IO, backend::Backend"Default", name, x)
mime = get(backend, :mime, MIME"text/plain"())
println(io, name, " = ")
show_indented(io, mime, x)
println(io)
return nothing
end

function visualize_unnamed(io::IO, ::Backend"Default", x)
show(io, MIME"text/plain"(), x)
println(io)
return nothing
end

function show_indented(io::IO, mime::MIME, x; indent=" ")
str = sprint(show, mime, x)
str = join((indent * s for s in split(str, '\n')), '\n')
print(io, str)
return nothing
end

function to_backend(::Type{B}; kwargs...) where {B}
return B(; kwargs...)
end
function to_backend(backend::Union{Symbol,String}; kwargs...)
return Backend(backend; kwargs...)
end
function to_backend(backend::AbstractBackend; kwargs...)
isempty(kwargs) ||
throw(ArgumentError("Keyword arguments must be passed as part of the backend object."))
return backend
end

end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
VisualizationBase = "19e8930c-b694-45c7-b48f-ae1a17da5712"

[compat]
Aqua = "0.8"
BackendSelection = "0.1"
SafeTestsets = "0.1"
Suppressor = "0.2"
Test = "1.10"
Expand Down
13 changes: 11 additions & 2 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
using VisualizationBase: VisualizationBase
using VisualizationBase: VisualizationBase, @visualize, visualize
using Test: @test, @testset

@testset "VisualizationBase" begin
# Tests go here.
x = [2, 3]
@test sprint(visualize, x) ==
"2-element Vector{Int64}:\n 2\n 3\n" ==
sprint((io, x) -> display(TextDisplay(io), x), x)
@test sprint((io, x) -> visualize(io, x; name="y"), x) ==
"y = \n 2-element Vector{Int64}:\n 2\n 3\n"
@test sprint((io, x) -> @visualize(x, io=io), x) ==
"x = \n 2-element Vector{Int64}:\n 2\n 3\n"
@test sprint((io, x) -> @visualize(x, io=io, name="y"), x) ==
"y = \n 2-element Vector{Int64}:\n 2\n 3\n"
end
Loading