Skip to content

Commit ec92414

Browse files
authored
Merge pull request #33 from JuliaImages/jc/disable
provide an enable/disable interface
2 parents 4ac3380 + 8718a36 commit ec92414

10 files changed

+130
-19
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ImageInTerminal"
22
uuid = "d8c32880-2388-543b-8c61-d9f865259254"
3-
version = "0.4.3"
3+
version = "0.4.4"
44

55
[deps]
66
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Here's how images are displayed in 24-bit colors:
8080

8181
![24bit color](https://user-images.githubusercontent.com/8684355/76688541-a17a4c00-6668-11ea-9fb9-41669fbec07e.png)
8282

83+
### Enable and disable
84+
85+
If you want to temporarily disable this package, you can call `ImageInTerminal.disable_encoding()`. And
86+
restore the encoding functionality with `ImageInTerminal.enable_encoding()`(automode), `ImageInTerminal.use_24bit()`,
87+
or `ImageInTerminal.use_256()`.
8388

8489
## Troubleshooting
8590

src/ImageInTerminal.jl

+41-11
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ include("imshow.jl")
1919
# overload default show in the REPL for colorant (arrays)
2020

2121
const colormode = TermColorDepth[TermColor256()]
22+
const should_render_image = Bool[true]
2223

2324
"""
2425
use_256()
2526
2627
Triggers `imshow256` automatically if an array of colorants is to
2728
be displayed in the julia REPL. (This is the default)
2829
"""
29-
use_256() = (colormode[1] = TermColor256())
30+
use_256() = (colormode[1] = TermColor256(); should_render_image[1] = true)
3031

3132
"""
3233
use_24bit()
@@ -35,28 +36,57 @@ Triggers `imshow24bit` automatically if an array of colorants is to
3536
be displayed in the julia REPL.
3637
Call `ImageInTerminal.use_256()` to restore default behaviour.
3738
"""
38-
use_24bit() = (colormode[1] = TermColor24bit())
39+
use_24bit() = (colormode[1] = TermColor24bit(); should_render_image[1] = true)
40+
41+
"""
42+
disable_encoding()
43+
44+
Disable the image encoding feature and show images as if they are normal arrays.
45+
46+
This can be restored by calling `ImageInTerminal.enable_encoding()`.
47+
"""
48+
disable_encoding() = (should_render_image[1] = false)
49+
50+
"""
51+
enable_encoding()
52+
53+
Enable the image encoding feature and show images in terminal.
54+
55+
This can be disabled by calling `ImageInTerminal.disable_encoding()`. To choose between
56+
different encoding method, call `ImageInTerminal.use_256()` or `ImageInTerminal.use_24bit()`.
57+
"""
58+
enable_encoding() = (should_render_image[1] = true)
59+
3960

4061
# colorant arrays
4162
function Base.show(
42-
io::IO, ::MIME"text/plain",
63+
io::IO, mime::MIME"text/plain",
4364
img::AbstractArray{<:Colorant})
44-
println(io, summary(img), ":")
45-
ImageInTerminal.imshow(io, img, colormode[1])
65+
if should_render_image[1]
66+
println(io, summary(img), ":")
67+
ImageInTerminal.imshow(io, img, colormode[1])
68+
else
69+
invoke(Base.show, Tuple{typeof(io), typeof(mime), AbstractArray}, io, mime, img)
70+
end
4671
end
4772

4873
# colorant
49-
function Base.show(io::IO, ::MIME"text/plain", color::Colorant)
50-
fgcol = _colorant2ansi(color, colormode[1])
51-
chr = _charof(alpha(color))
52-
print(io, Crayon(foreground = fgcol), chr, chr, " ")
53-
print(io, Crayon(foreground = :white), color)
54-
print(io, Crayon(reset = true))
74+
function Base.show(io::IO, mime::MIME"text/plain", color::Colorant)
75+
if should_render_image[1]
76+
fgcol = _colorant2ansi(color, colormode[1])
77+
chr = _charof(alpha(color))
78+
print(io, Crayon(foreground = fgcol), chr, chr, " ")
79+
print(io, Crayon(foreground = :white), color)
80+
print(io, Crayon(reset = true))
81+
else
82+
invoke(Base.show, Tuple{typeof(io), typeof(mime), Any}, io, mime, color)
83+
end
5584
end
5685

5786
function __init__()
5887
# use 24bit if the terminal supports it
5988
lowercase(get(ENV, "COLORTERM", "")) in ("24bit", "truecolor") && use_24bit()
89+
enable_encoding()
6090
end
6191

6292
end # module

test/reference/2d_show_raw.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4×4 Array{RGB{Float64},2} with eltype RGB{Float64}:
2+
RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0)
3+
RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0)
4+
RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0)
5+
RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0) RGB{Float64}(1.0,1.0,1.0)

test/reference/colorant_show_raw.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RGB{Float64}(0.5,0.1,0.9)

test/reference/lena_show_24bit.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
256×256 Array{RGB{N0f8},2} with eltype RGB{Normed{UInt8,8}}:
1+
256×256 Array{RGB{N0f8},2} with eltype RGB{N0f8}:
22
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
33
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
44
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

test/reference/lena_show_256.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
256×256 Array{RGB{N0f8},2} with eltype RGB{Normed{UInt8,8}}:
1+
256×256 Array{RGB{N0f8},2} with eltype RGB{N0f8}:
22
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
33
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
44
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

test/reference/rgbline_show_raw.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
20-element Array{RGB{Float64},1} with eltype RGB{Float64}:
2+
RGB{Float64}(0.0,0.0,1.0)
3+
RGB{Float64}(0.05263157894736842,0.0,0.9473684210526315)
4+
RGB{Float64}(0.10526315789473684,0.0,0.8947368421052632)
5+
RGB{Float64}(0.15789473684210525,0.0,0.8421052631578947)
6+
RGB{Float64}(0.21052631578947367,0.0,0.7894736842105263)
7+
RGB{Float64}(0.2631578947368421,0.0,0.7368421052631579)
8+
RGB{Float64}(0.3157894736842105,0.0,0.6842105263157895)
9+
RGB{Float64}(0.3684210526315789,0.0,0.631578947368421)
10+
RGB{Float64}(0.42105263157894735,0.0,0.5789473684210527)
11+
RGB{Float64}(0.47368421052631576,0.0,0.5263157894736842)
12+
RGB{Float64}(0.5263157894736842,0.0,0.47368421052631576)
13+
RGB{Float64}(0.5789473684210527,0.0,0.42105263157894735)
14+
RGB{Float64}(0.631578947368421,0.0,0.3684210526315789)
15+
RGB{Float64}(0.6842105263157895,0.0,0.3157894736842105)
16+
RGB{Float64}(0.7368421052631579,0.0,0.2631578947368421)
17+
RGB{Float64}(0.7894736842105263,0.0,0.21052631578947367)
18+
RGB{Float64}(0.8421052631578947,0.0,0.15789473684210525)
19+
RGB{Float64}(0.8947368421052632,0.0,0.10526315789473684)
20+
RGB{Float64}(0.9473684210526315,0.0,0.05263157894736842)
21+
RGB{Float64}(1.0,0.0,0.0)

test/tst_baseshow.jl

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
1-
_tostring(io) = replace.(replace.(readlines(seek(io,0)), Ref("\n" => "")), Ref("$Int" => "Int64"))
1+
function _tostring(io)
2+
contents = map(readlines(seek(io,0))) do line
3+
replace(strip(line), "$Int" => "Int64")
4+
end
5+
6+
# ignore summary changes from upstream
7+
contents[1] = replace(contents[1], "Normed{UInt8,8}"=>"N0f8")
8+
contents
9+
end
10+
11+
@testset "enable/disable encoding" begin
12+
old_colormode = ImageInTerminal.colormode[1]
13+
old_should_render_image = ImageInTerminal.should_render_image[1]
14+
15+
ImageInTerminal.enable_encoding()
16+
@test ImageInTerminal.colormode[1] == old_colormode
17+
@test ImageInTerminal.should_render_image[1] == true
18+
19+
ImageInTerminal.enable_encoding()
20+
ImageInTerminal.disable_encoding()
21+
@test ImageInTerminal.colormode[1] == old_colormode
22+
@test ImageInTerminal.should_render_image[1] == false
23+
24+
ImageInTerminal.disable_encoding()
25+
ImageInTerminal.use_256()
26+
@test ImageInTerminal.colormode[1] == ImageInTerminal.TermColor256()
27+
@test ImageInTerminal.should_render_image[1] == true
28+
29+
ImageInTerminal.disable_encoding()
30+
ImageInTerminal.use_24bit()
31+
@test ImageInTerminal.colormode[1] == ImageInTerminal.TermColor24bit()
32+
@test ImageInTerminal.should_render_image[1] == true
33+
34+
ImageInTerminal.colormode[1] = old_colormode
35+
ImageInTerminal.should_render_image[1] = old_should_render_image
36+
end
37+
38+
@testset "no encoding" begin
39+
ImageInTerminal.disable_encoding()
40+
io = IOBuffer()
41+
img = fill(RGB(1.0, 1.0, 1.0), 4, 4)
42+
show(io, MIME"text/plain"(), img)
43+
@test_reference "reference/2d_show_raw.txt" _tostring(io)
44+
io = IOBuffer()
45+
show(io, MIME"text/plain"(), collect(rgb_line))
46+
@test_reference "reference/rgbline_show_raw.txt" _tostring(io)
47+
io = IOBuffer()
48+
show(io, MIME"text/plain"(), RGB(0.5,0.1,0.9))
49+
@test_reference "reference/colorant_show_raw.txt" _tostring(io)
50+
end
251

352
@testset "256 colors" begin
453
ImageInTerminal.use_256()

test/tst_imshow.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ end
1717
@testset "lena" begin
1818
img = imresize(lena, 10, 10)
1919
io = IOBuffer()
20-
ensurecolor(imshow, io, img)
20+
ensurecolor(imshow256, io, img)
2121
res = replace.(readlines(seek(io,0)), Ref("\n" => ""))
2222
@test_reference "reference/lena_big_imshow.txt" res
2323
end
2424
@testset "ndarray" begin
2525
img = rgb_line_4d
2626
io = IOBuffer()
27-
ensurecolor(imshow, io, img)
27+
ensurecolor(imshow256, io, img)
2828
res = readlines(seek(io,0))
2929
@test_reference "reference/ndarray_imshow.txt" res
3030
end
@@ -112,15 +112,15 @@ end
112112
@testset "lena" begin
113113
img = OffsetArray(imresize(lena, 10, 10), (-10,5))
114114
io = IOBuffer()
115-
ensurecolor(imshow, io, img)
115+
ensurecolor(imshow256, io, img)
116116
res = replace.(readlines(seek(io,0)), Ref("\n" => ""))
117117
@test_reference "reference/lena_big_imshow.txt" res
118118
end
119119
@testset "rotation" begin
120120
tfm = recenter(RotMatrix(-pi/4), center(lighthouse))
121121
lhr = ImageTransformations.warp(lighthouse, tfm)
122122
io = IOBuffer()
123-
ensurecolor(imshow, io, lhr)
123+
ensurecolor(imshow256, io, lhr)
124124
res = replace.(readlines(seek(io,0)), Ref("\n" => ""))
125125
@test_reference "reference/lighthouse_rotated.txt" res
126126
end

0 commit comments

Comments
 (0)