Skip to content

Commit d8521be

Browse files
authored
add optional callback (#72)
1 parent 54ec9ea commit d8521be

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/ImageInTerminal.jl

+8-7
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,25 @@ Supported encoding:
107107
- ascii (`XTermColors` backend)
108108
"""
109109

110-
function imshow(io::IO, img::AbstractArray{<:Colorant}, maxsize::Tuple=displaysize(io))
110+
function imshow(
111+
io::IO, img::AbstractArray{<:Colorant}, maxsize::Tuple=displaysize(io); kw...
112+
)
111113
buf = IOContext(PipeBuffer(), :color => get(io, :color, false))
112114
if choose_sixel(img)
113115
sixel_encode(buf, img)
114116
else
117+
print_func = (io, x) -> ascii_show(io, x, COLORMODE[], :auto, maxsize; kw...)
115118
if ndims(img) > 2
116-
Base.show_nd(
117-
buf, img, (buf, x) -> ascii_show(buf, x, COLORMODE[], :auto, maxsize), true
118-
)
119+
Base.show_nd(buf, img, print_func, true)
119120
else
120-
ascii_show(buf, img, COLORMODE[], :auto, maxsize)
121+
print_func(buf, img)
121122
end
122123
end
123124
write(io, read(buf, String))
124125
end
125126

126-
imshow(img::AbstractArray{<:Colorant}, args...) = imshow(stdout, img, args...)
127-
imshow(img, args...) =
127+
imshow(img::AbstractArray{<:Colorant}, args...; kw...) = imshow(stdout, img, args...; kw...)
128+
imshow(img, args...; kw...) =
128129
throw(ArgumentError("imshow only supports colorant arrays with 1 or 2 dimensions"))
129130

130131
sixel_encode(args...; kwargs...) = Sixel.sixel_encode(args...; kwargs...)

test/tst_imshow.jl

+13
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,17 @@ end
9292
end
9393
end
9494

95+
@testset "callback" begin
96+
img = imresize(mandril, 10, 10)
97+
io = PipeBuffer()
98+
fgcols, bgcols = [], []
99+
callback(I, fgcol, bgcol, chars...) = begin
100+
push!(fgcols, fgcol)
101+
push!(bgcols, bgcol)
102+
end
103+
@ensurecolor imshow(io, img; callback=callback)
104+
@test length(fgcols) == prod(size(img))
105+
@test all(ismissing.(bgcols))
106+
end
107+
95108
set_colormode(8) # reset to default state

0 commit comments

Comments
 (0)