Module: CLI::UI::Terminal

Defined in:
lib/cli/ui/terminal.rb

Constant Summary collapse

DEFAULT_WIDTH =
80
DEFAULT_HEIGHT =
24

Class Method Summary collapse

Class Method Details

.heightObject

Returns the width of the terminal, if possible Otherwise, will return DEFAULT_HEIGHT

: -> Integer



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

def height
  winsize[0]
end

.setup_winsize_trapObject

: -> void



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

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

.widthObject

Returns the width of the terminal, if possible Otherwise will return DEFAULT_WIDTH

: -> Integer



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

def width
  winsize[1]
end

.winsizeObject

: -> [Integer, Integer]



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

def 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