@@ -6,7 +6,9 @@ struct SmallBlocks <: ImageEncoder
66 size:: NTuple{2, Int}
77end
88
9+ const RESET = Crayon (reset = true )
910const alpha_chars = (' ⋅' , ' ░' , ' ▒' , ' ▓' , ' █' )
11+
1012function _charof (alpha)
1113 idx = round (Int, alpha * (length (alpha_chars)- 1 ))
1214 alpha_chars[clamp (idx + 1 , 1 , length (alpha_chars))]
@@ -78,13 +80,16 @@ The function returns a tuple with three elements:
7880"""
7981
8082function ascii_encode (
81- enc:: SmallBlocks ,
82- colordepth:: TermColorDepth ,
83- img:: AbstractMatrix{<:Colorant} )
83+ io:: IO ,
84+ :: SmallBlocks ,
85+ colordepth:: TermColorDepth ,
86+ img:: AbstractMatrix{<:Colorant} ;
87+ trail_nl:: Bool = false ,
88+ ret:: Bool = false
89+ )
8490 yinds, xinds = axes (img)
85- io = PipeBuffer ()
8691 for y in first (yinds): 2 : last (yinds)
87- print (io, Crayon (reset = true ) )
92+ print (io, RESET )
8893 for x in xinds
8994 fgcol = _colorant2ansi (img[y, x], colordepth)
9095 bgcol = if y+ 1 <= last (yinds)
@@ -96,74 +101,96 @@ function ascii_encode(
96101 end
97102 print (io, Crayon (foreground= fgcol, background= bgcol), " ▀" )
98103 end
99- println (io, Crayon (reset = true ))
104+ if trail_nl || y < last (yinds)
105+ println (io, RESET)
106+ else
107+ print (io, RESET)
108+ end
100109 end
101- readlines (io)
110+ ret ? readlines (io) : nothing
102111end
103112
104113function ascii_encode (
105- enc:: BigBlocks ,
106- colordepth:: TermColorDepth ,
107- img:: AbstractMatrix{<:Colorant} )
114+ io:: IO ,
115+ :: BigBlocks ,
116+ colordepth:: TermColorDepth ,
117+ img:: AbstractMatrix{<:Colorant} ;
118+ trail_nl:: Bool = false ,
119+ ret:: Bool = false ,
120+ )
108121 yinds, xinds = axes (img)
109- io = PipeBuffer ()
110122 for y in yinds
111- print (io, Crayon (reset = true ) )
123+ print (io, RESET )
112124 for x in xinds
113125 color = img[y, x]
114126 fgcol = _colorant2ansi (color, colordepth)
115127 chr = _charof (alpha (color))
116128 print (io, Crayon (foreground = fgcol), chr, chr)
117129 end
118- println (io, Crayon (reset = true ))
130+ if trail_nl || y < last (yinds)
131+ println (io, RESET)
132+ else
133+ print (io, RESET)
134+ end
119135 end
120- readlines (io)
136+ ret ? readlines (io) : nothing
121137end
122138
123139function ascii_encode (
124- enc:: SmallBlocks ,
125- colordepth:: TermColorDepth ,
126- img:: AbstractVector{<:Colorant} )
127- io = PipeBuffer ()
128- print (io, Crayon (reset = true ))
140+ io:: IO ,
141+ :: SmallBlocks ,
142+ colordepth:: TermColorDepth ,
143+ img:: AbstractVector{<:Colorant} ;
144+ ret:: Bool = false
145+ )
146+ print (io, RESET)
129147 for i in axes (img, 1 )
130148 color = img[i]
131149 fgcol = _colorant2ansi (color, colordepth)
132150 chr = _charof (alpha (color))
133151 print (io, Crayon (foreground = fgcol), chr)
134152 end
135- println (io, Crayon (reset = true ) )
136- readlines (io)
153+ print (io, RESET )
154+ ret ? readlines (io) : nothing
137155end
138156
139157function ascii_encode (
140- enc:: BigBlocks ,
141- colordepth:: TermColorDepth ,
142- img:: AbstractVector{<:Colorant} )
158+ io:: IO ,
159+ enc:: BigBlocks ,
160+ colordepth:: TermColorDepth ,
161+ img:: AbstractVector{<:Colorant} ;
162+ ret:: Bool = false
163+ )
143164 w = length (img)
144165 n = enc. size[2 ] ÷ 3 == w ? w : enc. size[2 ] ÷ 6
145- io = PipeBuffer ()
146166 # left or full
147- print (io, Crayon (reset = true ) )
167+ print (io, RESET )
148168 for i in (0 : n- 1 ) .+ firstindex (img)
149169 color = img[i]
150170 fgcol = _colorant2ansi (color, colordepth)
151171 chr = _charof (alpha (color))
152172 print (io, Crayon (foreground = fgcol), chr, chr, " " )
153173 end
154174 if n < w # right part
155- print (io, Crayon (reset = true ) , " … " )
175+ print (io, RESET , " … " )
156176 for i in (- n+ 1 : 0 ) .+ lastindex (img)
157177 color = img[i]
158178 fgcol = _colorant2ansi (color, colordepth)
159179 chr = _charof (alpha (color))
160180 print (io, Crayon (foreground = fgcol), chr, chr, " " )
161181 end
162182 end
163- println (io, Crayon (reset = true ) )
164- readlines (io)
183+ print (io, RESET )
184+ ret ? readlines (io) : nothing
165185end
166186
187+ ascii_encode (enc:: SmallBlocks , args... ) =
188+ ascii_encode (PipeBuffer (), enc, args... ; ret= true )
189+
190+ ascii_encode (enc:: BigBlocks , args... ) =
191+ ascii_encode (PipeBuffer (), enc, args... ; ret= true )
192+
193+
167194"""
168195 ascii_display([stream], img, [depth::TermColorDepth], [maxsize])
169196
@@ -184,16 +211,14 @@ function ascii_display(
184211 io:: IO ,
185212 img:: AbstractMatrix{<:Colorant} ,
186213 colordepth:: TermColorDepth ,
187- maxsize:: Tuple = displaysize (io))
214+ maxsize:: Tuple = displaysize (io);
215+ trail_nl:: Bool = false )
188216 io_h, io_w = maxsize
189217 img_h, img_w = map (length, axes (img))
190218 scale = img_h <= io_h - 4 && 2 img_w <= io_w ? downscale_big : downscale_small
191219 img, enc = scale (img, io_h - 4 , io_w)
192- str = ascii_encode (enc, colordepth, img)
193- for (idx, line) in enumerate (str)
194- print (io, line)
195- idx < length (str) && println (io)
196- end
220+ ascii_encode (io, enc, colordepth, img; trail_nl)
221+ io
197222end
198223
199224# colorant vector
@@ -206,11 +231,8 @@ function ascii_display(
206231 img_w = length (img)
207232 scale = 3 img_w <= io_w ? downscale_big : downscale_small
208233 img, enc = scale (img, io_w)
209- str = ascii_encode (enc, colordepth, img)
210- for (idx, line) in enumerate (str)
211- print (io, line)
212- idx < length (str) && println (io)
213- end
234+ ascii_encode (io, enc, colordepth, img)
235+ io
214236end
215237
216- ascii_display (io:: IO , img:: AbstractArray{<:Colorant} ) = ascii_display (io, img, colormode[])
238+ ascii_display (io:: IO , img:: AbstractArray{<:Colorant} ; kwargs ... ) = ascii_display (io, img, colormode[]; kwargs ... )
0 commit comments