Tty: Avoid truncating if not sensible

This causes truncate to simply return the original string if the
terminal is not very wide, or if the terminal is unsupported.
This commit is contained in:
Score_Under 2016-05-03 15:56:47 +01:00 committed by Mike McQuaid
parent bf23ba1d1e
commit b5007c48e0
2 changed files with 9 additions and 4 deletions

View File

@ -9,9 +9,13 @@ class TtyTests < Homebrew::TestCase
end end
def test_truncate def test_truncate
Tty.stubs(:width).returns 10 Tty.stubs(:width).returns 15
assert_equal "foobar", Tty.truncate("foobar something very long") assert_equal "foobar some", Tty.truncate("foobar something very long")
assert_equal "trunca", Tty.truncate("truncate") assert_equal "truncate", Tty.truncate("truncate")
# When the terminal is unsupported, we report 0 width
Tty.stubs(:width).returns 0
assert_equal "foobar something very long", Tty.truncate("foobar something very long")
end end
def test_no_tty_formatting def test_no_tty_formatting

View File

@ -65,7 +65,8 @@ class Tty
end end
def truncate(str) def truncate(str)
str.to_s[0, width - 4] w = width
w > 10 ? str.to_s[0, w - 4] : str
end end
private private