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.



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

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:



97
98
99
# File 'lib/command_kit/terminal.rb', line 97

def terminal
  IO.console if terminal?
end

#terminal?Boolean Also known as: tty?

Determines if program is running in a terminal.

Returns:

  • (Boolean)

    Specifies whether stdout is connected to a terminal.



72
73
74
# File 'lib/command_kit/terminal.rb', line 72

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.



113
114
115
116
117
118
119
# File 'lib/command_kit/terminal.rb', line 113

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.



153
154
155
156
157
158
159
# File 'lib/command_kit/terminal.rb', line 153

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.



133
134
135
136
137
138
139
# File 'lib/command_kit/terminal.rb', line 133

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