Skip to content

Commit 244a7eb

Browse files
authored
Initialize implementation of visualize/@visualize (#1)
1 parent 38b9214 commit 244a7eb

File tree

6 files changed

+100
-8
lines changed

6 files changed

+100
-8
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ uuid = "19e8930c-b694-45c7-b48f-ae1a17da5712"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
44
version = "0.1.0"
55

6+
[deps]
7+
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
8+
69
[compat]
10+
BackendSelection = "0.1.5"
711
julia = "1.10"

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ julia> Pkg.add("VisualizationBase")
4242
## Examples
4343

4444
````julia
45-
using VisualizationBase: VisualizationBase
46-
````
45+
using VisualizationBase: @visualize
46+
using Test: @test
4747

48-
Examples go here.
48+
x = [2, 3]
49+
y = @visualize x
50+
@test x === y
51+
````
4952

5053
---
5154

examples/README.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ julia> Pkg.add("VisualizationBase")
4343

4444
# ## Examples
4545

46-
using VisualizationBase: VisualizationBase
47-
# Examples go here.
46+
using VisualizationBase: @visualize
47+
using Test: @test
48+
49+
x = [2, 3]
50+
y = @visualize x
51+
@test x === y

src/VisualizationBase.jl

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,75 @@
11
module VisualizationBase
22

3-
# Write your package code here.
3+
using BackendSelection: @Backend_str, AbstractBackend, Backend
4+
5+
export @visualize, visualize
6+
7+
macro visualize(x, kwargs...)
8+
all(kwarg -> Meta.isexpr(kwarg, :(=)), kwargs) ||
9+
throw(ArgumentError("All keyword arguments must be of the form `kw=value`."))
10+
kwargs′ = if all(kwarg -> kwarg.args[1] :name, kwargs)
11+
(kwargs..., :(name=$(string(x))))
12+
else
13+
kwargs
14+
end
15+
return :(visualize($(esc(x)); $(esc.(kwargs′)...)); $(esc(x)))
16+
end
17+
18+
function visualize(io::IO, x; backend=Backend"Default", name=nothing, kwargs...)
19+
return visualize(io, to_backend(backend; kwargs...), x; name)
20+
end
21+
22+
function visualize(x; backend=Backend"Default", name=nothing, io::IO=stdout, kwargs...)
23+
return visualize(io, to_backend(backend; kwargs...), x; name)
24+
end
25+
26+
function visualize(io::IO, backend::AbstractBackend, x; name=nothing)
27+
name = get(backend, :name, name)
28+
if isnothing(name)
29+
visualize_unnamed(io, backend, x)
30+
return nothing
31+
end
32+
visualize_named(io, backend, name, x)
33+
return nothing
34+
end
35+
36+
function visualize(backend::Backend"Default", x; name=nothing)
37+
io = get(backend, :io, stdout)
38+
visualize(io, backend, x; name)
39+
return nothing
40+
end
41+
42+
function visualize_named(io::IO, backend::Backend"Default", name, x)
43+
mime = get(backend, :mime, MIME"text/plain"())
44+
println(io, name, " = ")
45+
show_indented(io, mime, x)
46+
println(io)
47+
return nothing
48+
end
49+
50+
function visualize_unnamed(io::IO, ::Backend"Default", x)
51+
show(io, MIME"text/plain"(), x)
52+
println(io)
53+
return nothing
54+
end
55+
56+
function show_indented(io::IO, mime::MIME, x; indent=" ")
57+
str = sprint(show, mime, x)
58+
str = join((indent * s for s in split(str, '\n')), '\n')
59+
print(io, str)
60+
return nothing
61+
end
62+
63+
function to_backend(::Type{B}; kwargs...) where {B}
64+
return B(; kwargs...)
65+
end
66+
function to_backend(backend::Union{Symbol,String}; kwargs...)
67+
return Backend(backend; kwargs...)
68+
end
69+
function to_backend(backend::AbstractBackend; kwargs...)
70+
isempty(kwargs) ||
71+
throw(ArgumentError("Keyword arguments must be passed as part of the backend object."))
72+
return backend
73+
end
474

575
end

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[deps]
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
3+
BackendSelection = "680c2d7c-f67a-4cc9-ae9c-da132b1447a5"
34
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
45
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
56
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
67
VisualizationBase = "19e8930c-b694-45c7-b48f-ae1a17da5712"
78

89
[compat]
910
Aqua = "0.8"
11+
BackendSelection = "0.1"
1012
SafeTestsets = "0.1"
1113
Suppressor = "0.2"
1214
Test = "1.10"

test/test_basics.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
using VisualizationBase: VisualizationBase
1+
using VisualizationBase: VisualizationBase, @visualize, visualize
22
using Test: @test, @testset
33

44
@testset "VisualizationBase" begin
5-
# Tests go here.
5+
x = [2, 3]
6+
@test sprint(visualize, x) ==
7+
"2-element Vector{Int64}:\n 2\n 3\n" ==
8+
sprint((io, x) -> display(TextDisplay(io), x), x)
9+
@test sprint((io, x) -> visualize(io, x; name="y"), x) ==
10+
"y = \n 2-element Vector{Int64}:\n 2\n 3\n"
11+
@test sprint((io, x) -> @visualize(x, io=io), x) ==
12+
"x = \n 2-element Vector{Int64}:\n 2\n 3\n"
13+
@test sprint((io, x) -> @visualize(x, io=io, name="y"), x) ==
14+
"y = \n 2-element Vector{Int64}:\n 2\n 3\n"
615
end

0 commit comments

Comments
 (0)