Module: CommandKit::Terminal

Includes:
Env, Stdio
Included in:
Pager
Defined in:
lib/command_kit/terminal.rb

Overview

Provides direct access to the terminal.

Environment Variables

  • LINES - The explicit number of lines or rows the console should have.
  • COLUMNS - The explicit number of columns the console should have.

Constant Summary collapse

DEFAULT_TERMINAL_HEIGHT =

The default terminal height to fallback to.

25
DEFAULT_TERMINAL_WIDTH =

The default terminal width to fallback to.

80

Instance Attribute Summary

Attributes included from Env

#env

Instance Method Summary collapse

Methods included from Stdio

#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout

Instance Method Details

#initialize(**kwargs) ⇒ Object

Note:

If the $LINES env variable is set, and is non-zero, it will be returned by #terminal_height.

Note:

If the $COLUMNS env variable is set, and is non-zero, it will be returned by #terminal_width.

Initializes any terminal settings.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/command_kit/terminal.rb', line 46

def initialize(**kwargs)
  super(**kwargs)

  @terminal_height = if (lines = env['LINES'])
                       lines.to_i
                     else
                       DEFAULT_TERMINAL_HEIGHT
                     end

  @terminal_width  = if (columns = env['COLUMNS'])
                       columns.to_i
                     else
                       DEFAULT_TERMINAL_WIDTH
                     end
end

#terminalIO?

Returns the terminal object, if stdout is connected to a terminal.

Examples:

terminal
# => #<File:/dev/tty>

Returns:

  • (IO, nil)

    The IO objects or nil if stdout is not connected to a terminal.

See Also:



90
91
92
# File 'lib/command_kit/terminal.rb', line 90

def terminal
  IO.console if terminal?
end

#terminal?Boolean

Determines if program is running in a terminal.

Returns:

  • (Boolean)

    Specifies whether stdout is connected to a terminal.



70
71
72
# File 'lib/command_kit/terminal.rb', line 70

def terminal?
  IO.respond_to?(:console) && stdout.tty?
end

#terminal_heightInteger

Returns the terminal's height in number of lines.

Examples:

terminal_height
# => 22

Returns:

  • (Integer)

    The terminal's height in number of lines.



106
107
108
109
110
111
112
# File 'lib/command_kit/terminal.rb', line 106

def terminal_height
  if (terminal = self.terminal)
    terminal.winsize[0]
  else
    @terminal_height
  end
end

#terminal_size(Integer, Integer)

The terminal height (lines) and width (columns).

Examples:

terminal_size
# => [23, 91]

Returns:

  • ((Integer, Integer))

    Returns the height and width of the terminal.



146
147
148
149
150
151
152
# File 'lib/command_kit/terminal.rb', line 146

def terminal_size
  if (terminal = self.terminal)
    terminal.winsize
  else
    [@terminal_height, @terminal_width]
  end
end

#terminal_widthInteger

Returns the terminal's width in number of lines.

Examples:

terminal_width
# => 91

Returns:

  • (Integer)

    The terminal's width in number of columns.



126
127
128
129
130
131
132
# File 'lib/command_kit/terminal.rb', line 126

def terminal_width
  if (terminal = self.terminal)
    terminal.winsize[1]
  else
    @terminal_width
  end
end