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
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.
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 |
#terminal ⇒ IO?
Returns the terminal object, if stdout is connected to a terminal.
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.
70 71 72 |
# File 'lib/command_kit/terminal.rb', line 70 def terminal? IO.respond_to?(:console) && stdout.tty? end |
#terminal_height ⇒ Integer
Returns 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).
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_width ⇒ Integer
Returns the terminal's width in number of lines.
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 |