Class: Console::Terminal::XTerm

Inherits:
Text
  • Object
show all
Defined in:
lib/console/terminal/xterm.rb

Overview

XTerm style terminal output.

Constant Summary collapse

COLORS =

XTerm color codes.

{
  black: 0,
  red: 1,
  green: 2,
  yellow: 3,
  blue: 4,
  magenta: 5,
  cyan: 6,
  white: 7,
  default: 9,
}.freeze
ATTRIBUTES =

XTerm attribute codes.

{
  normal: 0,
  bold: 1,
  bright: 1,
  faint: 2,
  italic: 3,
  underline: 4,
  blink: 5,
  reverse: 7,
  hidden: 8,
}.freeze

Instance Attribute Summary

Attributes inherited from Text

#stream

Instance Method Summary collapse

Methods inherited from Text

#The stream to write to.=, #[], #[]=, #initialize, #print, #print_line, #puts, #write

Constructor Details

This class inherits a constructor from Console::Terminal::Text

Instance Method Details

#colors?Boolean

Whether the terminal supports colors.

Returns:

  • (Boolean)


42
43
44
# File 'lib/console/terminal/xterm.rb', line 42

def colors?
  true
end

#resetObject

Reset the style.



86
87
88
# File 'lib/console/terminal/xterm.rb', line 86

def reset
  "\e[0m"
end

#sizeObject

The size of the terminal.



47
48
49
50
51
52
# File 'lib/console/terminal/xterm.rb', line 47

def size
  @stream.winsize
rescue Errno::ENOTTY
  # Fake it...
  [24, 80]
end

#style(foreground, background = nil, *attributes) ⇒ Object

Apply the given style to the output.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/console/terminal/xterm.rb', line 65

def style(foreground, background = nil, *attributes)
  tokens = []
  
  if foreground
    tokens << 30 + COLORS.fetch(foreground)
  end
  
  if background
    tokens << 40 + COLORS.fetch(background)
  end
  
  attributes.each do |attribute|
    tokens << ATTRIBUTES.fetch(attribute){attribute.to_i}
  end
  
  return "\e[#{tokens.join(';')}m"
end

#widthObject

The width of the terminal.



55
56
57
# File 'lib/console/terminal/xterm.rb', line 55

def width
  size.last
end