From b5007c48e0aa053d51724460c3f18ee6909ce43c Mon Sep 17 00:00:00 2001 From: Score_Under Date: Tue, 3 May 2016 15:56:47 +0100 Subject: [PATCH] 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. --- Library/Homebrew/test/test_utils.rb | 10 +++++++--- 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 08e95e190c..b0caa6a2d4 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 c2b0dd231a..84bfb59b00 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