Skip to content

Commit

Permalink
Guess terminal width from 'stty -a'
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 11, 2012
1 parent 3af507a commit a99c0ed
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/groonga/tester.rb
Expand Up @@ -1520,11 +1520,28 @@ def create_temporary_file(key, content)
end

def guess_term_width
Integer(ENV["COLUMNS"] || ENV["TERM_WIDTH"] || 79)
Integer(guess_term_width_from_env || guess_term_width_from_stty || 79)
rescue ArgumentError
0
end

def guess_term_width_from_env
ENV["COLUMNS"] || ENV["TERM_WIDTH"]
end

def guess_term_width_from_stty
case `stty -a`
when /(\d+) columns/
$1
when /columns (\d+)/
$1
else
nil
end
rescue SystemCallError
nil
end

def string_width(string)
string.gsub(/\e\[[0-9;]+m/, "").size
end
Expand Down

0 comments on commit a99c0ed

Please sign in to comment.