@@ -214,35 +214,29 @@ function show(
214
214
# one line in collection, seven otherwise
215
215
get (io, :typeinfo , nothing ) === nothing && (limit *= 7 )
216
216
end
217
+ limit = max (0 , limit- 2 ) # quote chars
217
218
218
219
# early out for short strings
219
- len = ncodeunits (str)
220
- len ≤ limit - 2 && # quote chars
221
- return show (io, str)
220
+ check_textwidth (str, limit) && return show (io, str)
222
221
223
222
# these don't depend on string data
224
223
units = codeunit (str) == UInt8 ? " bytes" : " code units"
225
224
skip_text (skip) = " ⋯ $skip $units ⋯ "
226
- short = length (skip_text (" " )) + 4 # quote chars
227
- chars = max (limit, short + 1 ) - short # at least 1 digit
228
225
229
- # figure out how many characters to print in elided case
230
- chars -= d = ndigits (len - chars) # first adjustment
231
- chars += d - ndigits (len - chars) # second if needed
232
- chars = max (0 , chars)
226
+ # longest possible replacement string for omitted chars
227
+ max_replacement = skip_text (ncodeunits (str) * 100 ) # *100 for 2 inner quote chars
233
228
234
- # find head & tail, avoiding O(length(str)) computation
235
- head = nextind (str, 0 , 1 + (chars + 1 ) ÷ 2 )
236
- tail = prevind (str, len + 1 , chars ÷ 2 )
229
+ head, tail = string_truncate_boundaries (str, limit, max_replacement, Val (:center ))
237
230
238
231
# threshold: min chars skipped to make elision worthwhile
239
- t = short + ndigits (len - chars) - 1
240
- n = tail - head # skipped code units
241
- if 4 t ≤ n || t ≤ n && t ≤ length (str, head, tail- 1 )
242
- skip = skip_text (n)
243
- show (io, SubString (str, 1 : prevind (str, head)))
244
- printstyled (io, skip; color= :light_yellow , bold= true )
245
- show (io, SubString (str, tail))
232
+ afterhead = nextind (str, head)
233
+ n = tail - afterhead # skipped code units
234
+ replacement = skip_text (n)
235
+ t = ncodeunits (replacement) # length of replacement (textwidth == ncodeunits here)
236
+ @views if 4 t ≤ n || t ≤ n && t ≤ textwidth (str[afterhead: prevind (str,tail)])
237
+ show (io, str[begin : head])
238
+ printstyled (io, replacement; color= :light_yellow , bold= true )
239
+ show (io, str[tail: end ])
246
240
else
247
241
show (io, str)
248
242
end
0 commit comments