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 ⇒ Object readonly
-
#default_height ⇒ Integer
Return default height of terminal.
-
#default_width ⇒ Integer
Return default width of terminal.
-
#pager ⇒ 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 ⇒ Object
Determine current height.
-
#home ⇒ String
Find user home directory.
-
#initialize ⇒ Terminal
constructor
A new instance of Terminal.
-
#page(text) ⇒ Object
Run text through a dynamically chosen pager.
-
#width ⇒ Integer
Determine current width.
Constructor Details
Instance Attribute Details
#color ⇒ Object (readonly)
27 28 29 |
# File 'lib/tty/terminal.rb', line 27 def color @color end |
#default_height ⇒ Integer
Return default height of terminal
24 25 26 |
# File 'lib/tty/terminal.rb', line 24 def default_height @default_height end |
#default_width ⇒ Integer
Return default width of terminal
14 15 16 |
# File 'lib/tty/terminal.rb', line 14 def default_width @default_width end |
#pager ⇒ Pager (readonly)
Output pager
34 35 36 |
# File 'lib/tty/terminal.rb', line 34 def pager @pager end |
Instance Method Details
#color? ⇒ Boolean
Check if terminal supports color
156 157 158 |
# File 'lib/tty/terminal.rb', line 156 def color? %x{tput colors 2>/dev/null}.to_i > 2 end |
#dynamic_height ⇒ Integer
Calculate dynamic height of the terminal
111 112 113 |
# File 'lib/tty/terminal.rb', line 111 def dynamic_height @dynamic_height ||= (dynamic_height_stty.nonzero? || dynamic_height_tput) end |
#dynamic_height_stty ⇒ Integer
Detect terminal height with stty utility
129 130 131 |
# File 'lib/tty/terminal.rb', line 129 def dynamic_height_stty %x{stty size 2>/dev/null}.split[0].to_i end |
#dynamic_height_tput ⇒ Integer
Detect terminal height with tput utility
147 148 149 |
# File 'lib/tty/terminal.rb', line 147 def dynamic_height_tput %x{tput lines 2>/dev/null}.to_i end |
#dynamic_width ⇒ Integer
Calculate dynamic width of the terminal
102 103 104 |
# File 'lib/tty/terminal.rb', line 102 def dynamic_width @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput) end |
#dynamic_width_stty ⇒ Integer
Detect terminal width with stty utility
120 121 122 |
# File 'lib/tty/terminal.rb', line 120 def dynamic_width_stty %x{stty size 2>/dev/null}.split[1].to_i end |
#dynamic_width_tput ⇒ Integer
Detect terminal width with tput utility
138 139 140 |
# File 'lib/tty/terminal.rb', line 138 def dynamic_width_tput %x{tput cols 2>/dev/null}.to_i end |
#echo(is_on = true, &block) ⇒ Object
Echo given block
179 180 181 |
# File 'lib/tty/terminal.rb', line 179 def echo(is_on=true, &block) @echo.echo(is_on, &block) end |
#echo_off ⇒ Object
Switch echo off
170 171 172 |
# File 'lib/tty/terminal.rb', line 170 def echo_off @echo.off end |
#echo_on ⇒ Object
Switch echo on
163 164 165 |
# File 'lib/tty/terminal.rb', line 163 def echo_on @echo.on end |
#height ⇒ Object
Determine current height
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/tty/terminal.rb', line 86 def height env_tty_lines = ENV['TTY_LINES'] if env_tty_lines =~ /^\d+$/ result = env_tty_lines.to_i else result = TTY::System.unix? ? dynamic_height : self.default_height end rescue self.default_height end |
#home ⇒ String
Find user home directory
188 189 190 191 |
# File 'lib/tty/terminal.rb', line 188 def home @home ||= Home.new @home.home end |
#page(text) ⇒ Object
Run text through a dynamically chosen pager
199 200 201 |
# File 'lib/tty/terminal.rb', line 199 def page(text) @pager.page(text) end |
#width ⇒ Integer
Determine current width
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tty/terminal.rb', line 72 def width env_tty_columns = ENV['TTY_COLUMNS'] if env_tty_columns =~ /^\d+$/ result = env_tty_columns.to_i else result = TTY::System.unix? ? dynamic_width : default_width end rescue default_width end |