diff options
| author | Score_Under | 2016-05-03 15:56:47 +0100 |
|---|---|---|
| committer | Mike McQuaid | 2016-05-03 15:56:47 +0100 |
| commit | b5007c48e0aa053d51724460c3f18ee6909ce43c (patch) | |
| tree | 79a51032c06865c75b1abe2bf2a423d2d5940df8 /Library | |
| parent | bf23ba1d1e3141ce99f6546053d7ce2249031179 (diff) | |
| download | brew-b5007c48e0aa053d51724460c3f18ee6909ce43c.tar.bz2 | |
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.
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/test/test_utils.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/utils.rb | 3 |
2 files changed, 9 insertions, 4 deletions
diff --git a/Library/Homebrew/test/test_utils.rb b/Library/Homebrew/test/test_utils.rb index 08e95e190..b0caa6a2d 100644 --- a/Library/Homebrew/test/test_utils.rb +++ b/Library/Homebrew/test/test_utils.rb @@ -9,9 +9,13 @@ class TtyTests < Homebrew::TestCase end def test_truncate - Tty.stubs(:width).returns 10 - assert_equal "foobar", Tty.truncate("foobar something very long") - assert_equal "trunca", Tty.truncate("truncate") + Tty.stubs(:width).returns 15 + assert_equal "foobar some", Tty.truncate("foobar something very long") + 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 def test_no_tty_formatting diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index c2b0dd231..84bfb59b0 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -65,7 +65,8 @@ class Tty end def truncate(str) - str.to_s[0, width - 4] + w = width + w > 10 ? str.to_s[0, w - 4] : str end private |
