Class: TTY::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/terminal.rb,
lib/tty/terminal/echo.rb,
lib/tty/terminal/home.rb,
lib/tty/terminal/color.rb

Defined Under Namespace

Classes: Color, Echo, Home

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTerminal

Returns a new instance of Terminal.



29
30
31
32
33
34
# File 'lib/tty/terminal.rb', line 29

def initialize
  @color = TTY::Terminal::Color.new(self.color?)
  @echo  = TTY::Terminal::Echo.new
  @default_width  = 80
  @default_height = 24
end

Instance Attribute Details

#colorObject (readonly)



27
28
29
# File 'lib/tty/terminal.rb', line 27

def color
  @color
end

#default_heightInteger

Return default height of terminal

Examples:

default_height = TTY::Terminal.default_height

Returns:

  • (Integer)


24
25
26
# File 'lib/tty/terminal.rb', line 24

def default_height
  @default_height
end

#default_widthInteger

Return default width of terminal

Examples:

default_width = TTY::Terminal.default_width

Returns:

  • (Integer)


14
15
16
# File 'lib/tty/terminal.rb', line 14

def default_width
  @default_width
end

Instance Method Details

#color?Boolean

Check if terminal supports color

Returns:

  • (Boolean)


148
149
150
# File 'lib/tty/terminal.rb', line 148

def color?
  %x{tput colors 2>/dev/null}.to_i > 2
end

#dynamic_heightInteger

Calculate dynamic height of the terminal

Returns:

  • (Integer)

    height



103
104
105
# File 'lib/tty/terminal.rb', line 103

def dynamic_height
  @dynamic_height ||= (dynamic_height_stty.nonzero? || dynamic_height_tput)
end

#dynamic_height_sttyInteger

Detect terminal height with stty utility

Returns:

  • (Integer)

    height



121
122
123
# File 'lib/tty/terminal.rb', line 121

def dynamic_height_stty
  %x{stty size 2>/dev/null}.split[0].to_i
end

#dynamic_height_tputInteger

Detect terminal height with tput utility

Returns:

  • (Integer)

    height



139
140
141
# File 'lib/tty/terminal.rb', line 139

def dynamic_height_tput
  %x{tput lines 2>/dev/null}.to_i
end

#dynamic_widthInteger

Calculate dynamic width of the terminal

Returns:

  • (Integer)

    width



94
95
96
# File 'lib/tty/terminal.rb', line 94

def dynamic_width
  @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
end

#dynamic_width_sttyInteger

Detect terminal width with stty utility

Returns:

  • (Integer)

    width



112
113
114
# File 'lib/tty/terminal.rb', line 112

def dynamic_width_stty
  %x{stty size 2>/dev/null}.split[1].to_i
end

#dynamic_width_tputInteger

Detect terminal width with tput utility

Returns:

  • (Integer)

    width



130
131
132
# File 'lib/tty/terminal.rb', line 130

def dynamic_width_tput
  %x{tput cols 2>/dev/null}.to_i
end

#echo(is_on = true, &block) ⇒ Object



161
162
163
# File 'lib/tty/terminal.rb', line 161

def echo(is_on=true, &block)
  @echo.echo(is_on, &block)
end

#echo_offObject



156
157
158
# File 'lib/tty/terminal.rb', line 156

def echo_off
  @echo.off
end

#echo_onObject



152
153
154
# File 'lib/tty/terminal.rb', line 152

def echo_on
  @echo.on
end

#heightObject

Determine current height



78
79
80
81
82
83
84
85
86
87
# File 'lib/tty/terminal.rb', line 78

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

#homeString

Find user home directory

Returns:

  • (String)


170
171
172
# File 'lib/tty/terminal.rb', line 170

def home
  @home ||= Home.new.home
end

#widthInteger

Determine current width

Returns:

  • (Integer)

    width



64
65
66
67
68
69
70
71
72
73
# File 'lib/tty/terminal.rb', line 64

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