端末サイズ取得例 (dttermの端末サイズ問い合わせを使用)

Format
Ruby
Post date
2017-08-08 21:14
Publication Period
Unlimited
  1. #!/usr/bin/ruby
  2. #encoding: ascii-8bit
  3. require 'io/console'
  4. resp = ""
  5. STDIN.raw do |stdin|
  6. STDERR.print "\e[18t" # query terminal size
  7. resp = ""
  8. while (c = stdin.getc)
  9. resp << c.chr
  10. break unless /[\[\x1b\x9c;0-9]/ =~ c
  11. end
  12. end
  13. if /(?:\x9c|\x1b\[)8;(\d+);(\d+)t/ =~ resp # check response of cursor position query
  14. rows = $1.to_i
  15. cols = $2.to_i
  16. puts "COLUMNS=#{cols}"
  17. puts "ROWS=#{rows}"
  18. else
  19. STDERR.puts "Invalid response"
  20. end
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text