Cache Tty.width, since it is used heavily because of ohai.

This commit is contained in:
Markus Reiter 2018-06-14 22:14:46 +02:00
parent 502b393d66
commit 13d9305fad

View File

@ -6,11 +6,13 @@ module Tty
end end
def width def width
@width ||= begin
width = `/bin/stty size 2>/dev/null`.split[1] width = `/bin/stty size 2>/dev/null`.split[1]
width = `/usr/bin/tput cols 2>/dev/null`.split[0] if width.to_i.zero? width = `/usr/bin/tput cols 2>/dev/null`.split[0] if width.to_i.zero?
width ||= 80 width ||= 80
width.to_i width.to_i
end end
end
def truncate(string) def truncate(string)
(w = width).zero? ? string.to_s : string.to_s[0, w - 4] (w = width).zero? ? string.to_s : string.to_s[0, w - 4]