Module: CommandKit::Terminal
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
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ Object
Initializes any terminal settings.
-
#terminal ⇒ IO?
Returns the terminal object, if stdout is connected to a terminal.
-
#terminal? ⇒ Boolean
(also: #tty?)
Determines if program is running in a terminal.
-
#terminal_height ⇒ Integer
Returns the terminal's height in number of lines.
-
#terminal_size ⇒ (Integer, Integer)
The terminal height (lines) and width (columns).
-
#terminal_width ⇒ Integer
Returns the terminal's width in number of lines.
Methods included from Stdio
#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Instance Method Details
#initialize(**kwargs) ⇒ Object
If the $LINES
env variable is set, and is non-zero, it will be
returned by #terminal_height.
If the $COLUMNS
env variable is set, and is non-zero, it will be
returned by #terminal_width.
Initializes any terminal settings.
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 |
#terminal ⇒ IO?
Returns the terminal object, if stdout is connected to a terminal.
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.
72 73 74 |
# File 'lib/command_kit/terminal.rb', line 72 def terminal? IO.respond_to?(:console) && stdout.tty? end |
#terminal_height ⇒ Integer
Returns 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).
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_width ⇒ Integer
Returns the terminal's width in number of lines.
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 |