Module: CLI::UI::Terminal

Extended by:
T::Sig
Defined in:
lib/cli/ui/terminal.rb

Constant Summary collapse

DEFAULT_WIDTH =
80
DEFAULT_HEIGHT =
24

Class Method Summary collapse

Methods included from T::Sig

sig

Class Method Details

.heightObject


25
26
27
# File 'lib/cli/ui/terminal.rb', line 25

def self.height
  winsize[0]
end

.setup_winsize_trapObject


46
47
48
49
50
# File 'lib/cli/ui/terminal.rb', line 46

def self.setup_winsize_trap
  @winsize_trap ||= Signal.trap('WINCH') do
    @winsize = nil
  end
end

.widthObject


17
18
19
# File 'lib/cli/ui/terminal.rb', line 17

def self.width
  winsize[1]
end

.winsizeObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cli/ui/terminal.rb', line 30

def self.winsize
  @winsize ||= begin
    winsize = IO.console.winsize
    setup_winsize_trap

    if winsize.any?(&:zero?)
      [DEFAULT_HEIGHT, DEFAULT_WIDTH]
    else
      winsize
    end
  rescue
    [DEFAULT_HEIGHT, DEFAULT_WIDTH]
  end
end