Class: TTY::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/terminal.rb

Constant Summary collapse

@@default_width =
80
@@default_height =
24

Instance Method Summary collapse

Instance Method Details

#color?Boolean

Check if terminal supports color

Returns:

  • (Boolean)


144
145
146
# File 'lib/tty/terminal.rb', line 144

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

#default_heightInteger

Return default height of terminal

Examples:

default_height = TTY::Terminal.default_height

Returns:

  • (Integer)


41
42
43
# File 'lib/tty/terminal.rb', line 41

def default_height
  @@default_height
end

#default_height=(height) ⇒ Integer

Set default height of terminal

Examples:

default_height = TTY::Terminal.default_height

Returns:

  • (Integer)


53
54
55
# File 'lib/tty/terminal.rb', line 53

def default_height=(height)
  @@default_height = height
end

#default_widthInteger

Return default width of terminal

Examples:

default_width = TTY::Terminal.default_width

Returns:

  • (Integer)


18
19
20
# File 'lib/tty/terminal.rb', line 18

def default_width
  @@default_width
end

#default_width=(width) ⇒ Integer

Set default width of terminal

Parameters:

  • width (Integer)

Returns:

  • (Integer)


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

def default_width=(width)
  @@default_width = width
end

#dynamic_heightInteger

Calculate dynamic height of the terminal

Returns:

  • (Integer)

    height



99
100
101
# File 'lib/tty/terminal.rb', line 99

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



117
118
119
# File 'lib/tty/terminal.rb', line 117

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



135
136
137
# File 'lib/tty/terminal.rb', line 135

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

#dynamic_widthInteger

Calculate dynamic width of the terminal

Returns:

  • (Integer)

    width



90
91
92
# File 'lib/tty/terminal.rb', line 90

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



108
109
110
# File 'lib/tty/terminal.rb', line 108

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



126
127
128
# File 'lib/tty/terminal.rb', line 126

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

#heightObject

Determine current height



75
76
77
78
79
80
81
82
83
# File 'lib/tty/terminal.rb', line 75

def height
  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

#widthInteger

Determine current width

Returns:

  • (Integer)

    width



62
63
64
65
66
67
68
69
70
# File 'lib/tty/terminal.rb', line 62

def width
  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