端末サイズ取得例 (dttermの端末サイズ問い合わせを使用)
- #!/usr/bin/ruby
- #encoding: ascii-8bit
- require 'io/console'
- resp = ""
- STDIN.raw do |stdin|
- STDERR.print "\e[18t" # query terminal size
- resp = ""
- while (c = stdin.getc)
- resp << c.chr
- break unless /[\[\x1b\x9c;0-9]/ =~ c
- end
- end
- if /(?:\x9c|\x1b\[)8;(\d+);(\d+)t/ =~ resp # check response of cursor position query
- rows = $1.to_i
- cols = $2.to_i
- puts "COLUMNS=#{cols}"
- puts "ROWS=#{rows}"
- else
- STDERR.puts "Invalid response"
- end