Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/irb/command/pushws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
# #<Object...>, [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}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified a bit, to remove the zero by doing

obj_inspection = "#{obj_inspection[...threshold - 1]}...#{closing}"

end

obj_inspection
Expand Down
23 changes: 23 additions & 0 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,29 @@ def test_workspaces_returns_the_stack_of_workspaces
assert_empty err
assert_match(/\[#<TestIRB::Workspac...>, #<TestIRB::Workspac...>\]\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 = [
'#<TestIRB::Workspac...>',
{ 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
Expand Down