Class: TTY::Terminal
- Inherits:
-
Object
- Object
- TTY::Terminal
- Defined in:
- lib/tty/terminal.rb,
lib/tty/terminal/echo.rb,
lib/tty/terminal/home.rb,
lib/tty/terminal/color.rb,
lib/tty/terminal/pager.rb,
lib/tty/terminal/pager/basic.rb,
lib/tty/terminal/pager/system.rb
Defined Under Namespace
Classes: BasicPager, Color, Echo, Home, Pager, SystemPager
Instance Attribute Summary collapse
-
#color ⇒ TTY::Terminal::Color
readonly
Return access to color terminal.
-
#default_height ⇒ Integer
readonly
Return default height of terminal.
-
#default_width ⇒ Integer
readonly
Return default width of terminal.
-
#pager ⇒ TTY::Terminal::Pager
readonly
Output pager.
Instance Method Summary collapse
-
#color? ⇒ Boolean
Check if terminal supports color.
-
#dynamic_height ⇒ Integer
Calculate dynamic height of the terminal.
-
#dynamic_height_stty ⇒ Integer
Detect terminal height with stty utility.
-
#dynamic_height_tput ⇒ Integer
Detect terminal height with tput utility.
-
#dynamic_width ⇒ Integer
Calculate dynamic width of the terminal.
-
#dynamic_width_stty ⇒ Integer
Detect terminal width with stty utility.
-
#dynamic_width_tput ⇒ Integer
Detect terminal width with tput utility.
-
#echo(is_on = true, &block) ⇒ Object
Echo given block.
-
#echo_off ⇒ Object
Switch echo off.
-
#echo_on ⇒ Object
Switch echo on.
-
#height ⇒ Integer
Determine current height.
-
#home ⇒ String
Find user home directory.
-
#initialize(options = {}) ⇒ Terminal
constructor
Initialize a Terminal.
-
#page(text) ⇒ Object
Run text through a dynamically chosen pager.
-
#width ⇒ Integer
Determine current width.
Constructor Details
#initialize(options = {}) ⇒ Terminal
Initialize a Terminal
42 43 44 45 46 47 48 49 |
# File 'lib/tty/terminal.rb', line 42 def initialize( = {}) @color = TTY::Terminal::Color.new(self.color?) @echo = TTY::Terminal::Echo.new @pager = TTY::Terminal::Pager @home = Home.new @default_width = .fetch(:default_width) { 80 } @default_height = .fetch(:default_height) { 24 } end |
Instance Attribute Details
#color ⇒ TTY::Terminal::Color (readonly)
Return access to color terminal
30 31 32 |
# File 'lib/tty/terminal.rb', line 30 def color @color end |
#default_height ⇒ Integer (readonly)
Return default height of terminal
23 24 25 |
# File 'lib/tty/terminal.rb', line 23 def default_height @default_height end |
#default_width ⇒ Integer (readonly)
Return default width of terminal
13 14 15 |
# File 'lib/tty/terminal.rb', line 13 def default_width @default_width end |
#pager ⇒ TTY::Terminal::Pager (readonly)
Output pager
37 38 39 |
# File 'lib/tty/terminal.rb', line 37 def pager @pager end |
Instance Method Details
#color? ⇒ Boolean
Check if terminal supports color
142 143 144 |
# File 'lib/tty/terminal.rb', line 142 def color? %x(tput colors 2>/dev/null).to_i > 2 end |
#dynamic_height ⇒ Integer
Calculate dynamic height of the terminal
97 98 99 |
# File 'lib/tty/terminal.rb', line 97 def dynamic_height @dynamic_height ||= (dynamic_height_stty.nonzero? || dynamic_height_tput) end |
#dynamic_height_stty ⇒ Integer
Detect terminal height with stty utility
115 116 117 |
# File 'lib/tty/terminal.rb', line 115 def dynamic_height_stty %x(tty size 2>/dev/null).split[0].to_i end |
#dynamic_height_tput ⇒ Integer
Detect terminal height with tput utility
133 134 135 |
# File 'lib/tty/terminal.rb', line 133 def dynamic_height_tput %x(tput lines 2>/dev/null).to_i end |
#dynamic_width ⇒ Integer
Calculate dynamic width of the terminal
88 89 90 |
# File 'lib/tty/terminal.rb', line 88 def dynamic_width @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput) end |
#dynamic_width_stty ⇒ Integer
Detect terminal width with stty utility
106 107 108 |
# File 'lib/tty/terminal.rb', line 106 def dynamic_width_stty %x(tty size 2>/dev/null).split[1].to_i end |
#dynamic_width_tput ⇒ Integer
Detect terminal width with tput utility
124 125 126 |
# File 'lib/tty/terminal.rb', line 124 def dynamic_width_tput %x(tput cols 2>/dev/null).to_i end |
#echo(is_on = true, &block) ⇒ Object
Echo given block
165 166 167 |
# File 'lib/tty/terminal.rb', line 165 def echo(is_on = true, &block) @echo.echo(is_on, &block) end |
#echo_off ⇒ Object
Switch echo off
156 157 158 |
# File 'lib/tty/terminal.rb', line 156 def echo_off @echo.off end |
#echo_on ⇒ Object
Switch echo on
149 150 151 |
# File 'lib/tty/terminal.rb', line 149 def echo_on @echo.on end |
#height ⇒ Integer
Determine current height
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tty/terminal.rb', line 72 def height env_tty_lines = ENV['TTY_LINES'] if env_tty_lines =~ /^\d+$/ env_tty_lines.to_i else TTY::System.unix? ? dynamic_height : default_height end rescue default_height end |
#home ⇒ String
Find user home directory
174 175 176 |
# File 'lib/tty/terminal.rb', line 174 def home @home.home end |
#page(text) ⇒ Object
Run text through a dynamically chosen pager
184 185 186 |
# File 'lib/tty/terminal.rb', line 184 def page(text) @pager.page(text) end |