This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -37,8 +37,8 @@ def format(object)
37
37
if formatted_object . length < max_formatted_output_length
38
38
return formatted_object
39
39
else
40
- beginning = formatted_object [ 0 .. max_formatted_output_length / 2 ]
41
- ending = formatted_object [ -max_formatted_output_length / 2 ..- 1 ]
40
+ beginning = truncate_string formatted_object , 0 , max_formatted_output_length / 2
41
+ ending = truncate_string formatted_object , -max_formatted_output_length / 2 , - 1
42
42
return beginning + ELLIPSIS + ending
43
43
end
44
44
end
@@ -244,6 +244,20 @@ def inspect
244
244
DelegatorInspector ,
245
245
InspectableObjectInspector
246
246
]
247
+
248
+ private
249
+
250
+ # Returns the substring defined by the start_index and end_index
251
+ # If the string ends with a partial ANSI code code then that
252
+ # will be removed as printing partial ANSI
253
+ # codes to the terminal can lead to corruption
254
+ def truncate_string ( str , start_index , end_index )
255
+ cut_str = str [ start_index ..end_index ]
256
+
257
+ # ANSI color codes are like: \e[33m so anything with \e[ and a
258
+ # number without a 'm' is an incomplete color code
259
+ cut_str . sub ( /\e \[ \d +$/ , '' )
260
+ end
247
261
end
248
262
end
249
263
end
Original file line number Diff line number Diff line change @@ -310,6 +310,18 @@ def self.to_s
310
310
formatter = ObjectFormatter . new ( 10 )
311
311
expect ( formatter . format ( 'Testing' ) ) . to eq ( '"Testing"' )
312
312
end
313
+
314
+ context 'with ANSI escape codes that fall on the truncate split' do
315
+ it 'removes that escape code so terminals do not get corrupted print a partial escape code' do
316
+ formatter = ObjectFormatter . new ( 38 )
317
+ object = Class . new do
318
+ def inspect
319
+ "#<\e [33mClass\e [0m \e [36mname: \e [0m\" foobars\" \e [36mcount: \e [0m42>"
320
+ end
321
+ end . new
322
+ expect ( formatter . format ( object ) ) . to eq ( "#<\e [33mClass\e [0m ...\e [36mcount: \e [0m42>" )
323
+ end
324
+ end
313
325
end
314
326
315
327
context 'with truncation disabled' do
You can’t perform that action at this time.
0 commit comments