diff --git a/lib/irb/command/pushws.rb b/lib/irb/command/pushws.rb index 0c2dd9e92..51ac07a1c 100644 --- a/lib/irb/command/pushws.rb +++ b/lib/irb/command/pushws.rb @@ -29,7 +29,12 @@ def truncated_inspect(obj) obj_inspection = obj.inspect if obj_inspection.size > threshold - obj_inspection = obj_inspection[0, threshold - 1] + "...>" + closing = obj_inspection[-1] + # Support closing characters for objects like: + # #, [array...], {hash...}, (123/456...), "string...", /regexp.../ + # Other objects will be truncated without a closing character. + closing = nil unless '>]})"/'.include?(closing) + obj_inspection = "#{obj_inspection[0, threshold - 1]}...#{closing}" end obj_inspection diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index fd98a5a1b..9091bbc81 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -535,6 +535,29 @@ def test_workspaces_returns_the_stack_of_workspaces assert_empty err assert_match(/\[#, #\]\n/, out) end + + def test_workspaces_various_truncation + out, err = execute_lines( + 'pushws { key: 123456789123456789 }', + 'pushws "a" * 30', + 'pushws [1] * 30', + 'pushws /aaaaaaaaaaaaaaaaaaaaaaaaa/', + 'pushws 1r/123456789123456789123456789', + 'pushws 123456789123456789123456789', + 'pushws Struct.new(:inspect).new("a" * 30)' + ) + expected_truncations = [ + '#', + { key: 123456789123456789 }.inspect[0, 19] + '...}', + '"aaaaaaaaaaaaaaaaaa..."', + '[1, 1, 1, 1, 1, 1, ...]', + '/aaaaaaaaaaaaaaaaaa.../', + '(1/1234567891234567...)', + '1234567891234567891...', + 'aaaaaaaaaaaaaaaaaaa...' + ] + assert_include(out, "[#{expected_truncations.join(", ")}]") + end end class PopwsTest < WorkspaceCommandTestCase