Class: Tui::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/tui.rb

Class Method Summary collapse

Class Method Details

.size(io = $stderr) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/tui.rb', line 300

def size(io = $stderr)
  env_rows = ENV['TRY_HEIGHT'].to_i
  env_cols = ENV['TRY_WIDTH'].to_i
  rows = env_rows.positive? ? env_rows : nil
  cols = env_cols.positive? ? env_cols : nil

  streams = [io, $stdout, $stdin].compact.uniq

  streams.each do |stream|
    next unless (!rows || !cols)
    next unless stream.respond_to?(:winsize)

    begin
      s_rows, s_cols = stream.winsize
      rows ||= s_rows
      cols ||= s_cols
    rescue IOError, Errno::ENOTTY, Errno::EOPNOTSUPP, Errno::ENODEV
      next
    end
  end

  if (!rows || !cols)
    begin
      console = IO.console
      if console
        c_rows, c_cols = console.winsize
        rows ||= c_rows
        cols ||= c_cols
      end
    rescue IOError, Errno::ENOTTY, Errno::EOPNOTSUPP, Errno::ENODEV
    end
  end

  rows ||= 24
  cols ||= 80
  [rows, cols]
end