Skip to content

Commit 6a386ba

Browse files
authored
improve tiling display in VSCode (#25)
1 parent 98fc05e commit 6a386ba

5 files changed

Lines changed: 51 additions & 14 deletions

File tree

Project.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ version = "0.2.4"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
8+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
89
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
910
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1011
ImageShow = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
1112
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
1213
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1314
Transducers = "28d57a85-8fef-5791-bfe6-a80928e7c999"
1415

16+
[weakdeps]
17+
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
18+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
19+
20+
[extensions]
21+
MakieExtension = ["Makie", "GeometryBasics"]
22+
1523
[compat]
1624
Adapt = "4"
25+
Base64 = "1"
1726
Colors = "0.12"
1827
GeometryBasics = "0.4"
1928
ImageIO = "0.6"
@@ -23,10 +32,3 @@ Makie = "0.21"
2332
OffsetArrays = "1"
2433
Transducers = "0.4"
2534
julia = "1.9"
26-
27-
[extensions]
28-
MakieExtension = ["Makie", "GeometryBasics"]
29-
30-
[weakdeps]
31-
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
32-
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A package for generating and analyzing [Aztec diamonds](https://en.wikipedia.org
1212

1313
To generate an order-n Aztec diamond, simply call `diamond(n)`
1414

15-
```julia
15+
```julia-repl
1616
julia> D = diamond(10)
1717
10-order Tiling{Matrix{AztecDiamonds.Edge}}
1818
🬇🬋🬋🬃
@@ -37,9 +37,9 @@ julia> D = diamond(10)
3737
🬇🬋🬋🬃
3838
```
3939

40-
It is recommended that you use an interactive enviroment like Pluto, VS Code or IJulia to be able to view the generated diamonds in all their glory. Alternatively, you can also view them in a separate window using the [ImageView](https://github.com/JuliaImages/ImageView.jl) package as follows:
40+
It is recommended that you use an interactive enviroment like Pluto, VS Code or IJulia to be able to view larger diamond tilings in all their glory. Alternatively, you can also view them in a separate window using the [ImageView](https://github.com/JuliaImages/ImageView.jl) package as follows:
4141

42-
```julia
42+
```julia-repl
4343
julia> using ImageView
4444
4545
julia> imshow(AztecDiamonds.to_img(D))
@@ -48,7 +48,7 @@ julia> imshow(AztecDiamonds.to_img(D))
4848

4949
It is possible to take advantage of GPU acceleration via [KernelAbstractions.jl](https://github.com/JuliaGPU/KernelAbstractions.jl) on supported backends, e.g. CUDA:
5050

51-
```julia
51+
```julia-repl
5252
julia> using CUDA
5353
5454
julia> ka_diamond(200, CuArray)
@@ -57,7 +57,7 @@ julia> ka_diamond(200, CuArray)
5757

5858
You can extract the DR-path separating the northern arctic region from the rest of the diamond using the `dr_path` function.
5959

60-
```julia
60+
```julia-repl
6161
julia> dr_path(D)
6262
21-element OffsetArray(::Vector{Float64}, -10:10) with eltype Float64 with indices -10:10:
6363
-0.5
@@ -82,3 +82,5 @@ julia> dr_path(D)
8282
0.5
8383
-0.5
8484
```
85+
86+
To get the other DR-paths the tiling can be rotated first using the functions `rotr90`, `rotl90` or `rot180`.

src/show.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Colors
22
import ImageShow
3+
using Base64: Base64EncodePipe
34

45
Base.summary(io::IO, t::Tiling) = print(io, t.N, "-order ", typeof(t))
56

@@ -29,8 +30,14 @@ end
2930
function Base.show(io::IO, ::MIME"text/plain", t::Tiling)
3031
summary(io, t)
3132
(; N) = t
32-
displaysize(io)[2] 4N || return print(io, "\n Output too large to fit terminal. Use \
33-
`using ImageView; imshow(AztecDiamonds.to_img(D))` to display as an image instead.")
33+
if displaysize(io)[2] < 4N
34+
printstyled(
35+
io, "\n Output too large to fit terminal. \
36+
Use `using ImageView; imshow(AztecDiamonds.to_img(D))` to display as an image instead.";
37+
color = :black,
38+
)
39+
return nothing
40+
end
3441
t = adapt(Array, t)
3542
foreach(Iterators.product(inds(N)...)) do (j, i)
3643
j == 1 - N && println(io)
@@ -76,3 +83,14 @@ function Base.show(io::IO, ::MIME"image/png", t::Tiling; kw...)
7683
img = to_img(adapt(Array, t))
7784
show(io, MIME("image/png"), img; kw...)
7885
end
86+
87+
Base.showable(::MIME"juliavscode/html", (; N)::Tiling) = N > 0
88+
89+
function Base.show(io::IO, ::MIME"juliavscode/html", t::Tiling; kw...)
90+
img = to_img(adapt(Array, t))
91+
print(io, "<img src='data:image/gif;base64,")
92+
b64_io = IOContext(Base64EncodePipe(io), :full_fidelity => true)
93+
show(b64_io, MIME("image/png"), img; kw...)
94+
close(b64_io)
95+
print(io, "' style='width: 100%; max-height: 500px; object-fit: contain; image-rendering: pixelated' />")
96+
end

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[deps]
22
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
33
AztecDiamonds = "8762d9c5-fcab-4007-8fd1-c6de73397726"
4+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
5+
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
46
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
57
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
68
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"

test/show.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ end
2222
@test length(r) > 2(2N)^2
2323
r_color = repr(MIME("text/plain"), D; context = :color => true)
2424
@test length(r_color) == length(r) + 10length(AztecDiamonds.faces(D))
25+
26+
r = repr(MIME("text/plain"), D; context = :displaysize => (10, 10))
27+
@test contains(r, "Output too large to fit terminal")
2528
end
2629

2730
@testitem "printing of malformed tilings" begin
@@ -58,3 +61,13 @@ end
5861
)
5962
@test repr(MIME("text/plain"), t) == expected
6063
end
64+
65+
@testitem "VSCode show" begin
66+
using Base64
67+
68+
D = diamond(20)
69+
@test Base.showable("juliavscode/html", D)
70+
html = String(repr("juliavscode/html", D))
71+
b64_png = stringmime("image/png", D)
72+
@test contains(html, b64_png)
73+
end

0 commit comments

Comments
 (0)