@@ -214,35 +214,29 @@ function show(
214214 # one line in collection, seven otherwise
215215 get (io, :typeinfo , nothing ) === nothing && (limit *= 7 )
216216 end
217+ limit = max (0 , limit- 2 ) # quote chars
217218
218219 # 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)
222221
223222 # these don't depend on string data
224223 units = codeunit (str) == UInt8 ? " bytes" : " code units"
225224 skip_text (skip) = " ⋯ $skip $units ⋯ "
226- short = length (skip_text (" " )) + 4 # quote chars
227- chars = max (limit, short + 1 ) - short # at least 1 digit
228225
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
233228
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 ))
237230
238231 # 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 ])
246240 else
247241 show (io, str)
248242 end
0 commit comments