Module: MonoclePrint::TerminalEscapes

Included in:
OutputDevice
Defined in:
lib/monocle-print/terminal-escapes.rb

Constant Summary collapse

ANSI_COLORS =
{
  :blue => 4,   :white => 7,  :magenta => 5,
  :yellow => 3, :green => 2,  :black => 0,
  :red => 1,    :cyan => 6
}
ANSI_COLOR_NAMES =
ANSI_COLORS.keys.freeze
ANSI_MODIFIERS =
{
  :bold      => 1,
  :underline => 4,
  :blink     => 5,
  :reverse   => 7,
  :conceal   => 8
}
ANSI_MODIFIER_NAMES =
ANSI_MODIFIERS.keys.freeze

Class Method Summary collapse

Class Method Details

.ansi_color(type, color, bold = nil) ⇒ Object

def term_info

@term_info ||= TermInfo.new

end



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/monocle-print/terminal-escapes.rb', line 32

def ansi_color( type, color, bold = nil )
  offset =
    case type
    when ?f then 30  # foreground
    when ?b then 40  # background
    end
  code = offset + ANSI_COLORS.fetch( color.to_sym ) do
    raise( ArgumentError, "unknown color name `%s'" % color )
  end
  "\e[#{ '1;' if bold }#{ code }m"
end

.ansi_modifier(name) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/monocle-print/terminal-escapes.rb', line 62

def ansi_modifier( name )
  code =
    ANSI_MODIFIERS.fetch( name.to_sym ) do
      fail ArgumentError, "unknown modifier name `%s'" % name
    end
  "\e[#{ code }m"
end


82
83
84
# File 'lib/monocle-print/terminal-escapes.rb', line 82

def blink
  "\e[5m"
end

.boldObject



74
75
76
# File 'lib/monocle-print/terminal-escapes.rb', line 74

def bold
  "\e[1m"
end

.clear_attrObject



70
71
72
# File 'lib/monocle-print/terminal-escapes.rb', line 70

def clear_attr
  "\e[0m"
end

.clear_downObject



155
156
157
# File 'lib/monocle-print/terminal-escapes.rb', line 155

def clear_down
  "\e[0J"
end

.clear_leftObject



147
148
149
# File 'lib/monocle-print/terminal-escapes.rb', line 147

def clear_left
  "\e[1K"
end

.clear_lineObject



151
152
153
# File 'lib/monocle-print/terminal-escapes.rb', line 151

def clear_line
  "\e[2K"
end

.clear_rightObject



143
144
145
# File 'lib/monocle-print/terminal-escapes.rb', line 143

def clear_right
  "\e[0K"
end

.clear_screenObject



163
164
165
# File 'lib/monocle-print/terminal-escapes.rb', line 163

def clear_screen
  "\e[2J"
end

.clear_upObject



159
160
161
# File 'lib/monocle-print/terminal-escapes.rb', line 159

def clear_up
  "\e[1J"
end

.concealObject



90
91
92
# File 'lib/monocle-print/terminal-escapes.rb', line 90

def conceal
  "\e[8m"
end

.cursor_backward(columns = 1) ⇒ Object



110
111
112
# File 'lib/monocle-print/terminal-escapes.rb', line 110

def cursor_backward( columns = 1 )
  "\e[%iD" % columns
end

.cursor_down(lines = 1) ⇒ Object



102
103
104
# File 'lib/monocle-print/terminal-escapes.rb', line 102

def cursor_down( lines = 1 )
  "\e[%iB" % lines
end

.cursor_forward(columns = 1) ⇒ Object



106
107
108
# File 'lib/monocle-print/terminal-escapes.rb', line 106

def cursor_forward( columns = 1 )
  "\e[%iC" % columns
end

.cursor_up(lines = 1) ⇒ Object



98
99
100
# File 'lib/monocle-print/terminal-escapes.rb', line 98

def cursor_up( lines = 1 )
  "\e[%iA" % lines
end

.dobule_height_bottomObject



131
132
133
# File 'lib/monocle-print/terminal-escapes.rb', line 131

def dobule_height_bottom
  "\e#4"
end

.double_height_topObject

VT100 escapes



127
128
129
# File 'lib/monocle-print/terminal-escapes.rb', line 127

def double_height_top
  "\e#3"
end

.double_widthObject



139
140
141
# File 'lib/monocle-print/terminal-escapes.rb', line 139

def double_width
  "\e#6"
end

.konsole_color(type, r, g, b, bold = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/monocle-print/terminal-escapes.rb', line 53

def konsole_color( type, r, g, b, bold = nil)
  prefix =
    case type
    when ?f then 38  # foreground
    when ?b then 48  # background
    end
  "\e[#{ prefix };2;#{ r };#{ g };#{ b }m"
end

.restore_cursorObject



118
119
120
# File 'lib/monocle-print/terminal-escapes.rb', line 118

def restore_cursor
  "\e[u"
end

.reverseObject



86
87
88
# File 'lib/monocle-print/terminal-escapes.rb', line 86

def reverse
  "\e[7m"
end

.save_cursorObject



114
115
116
# File 'lib/monocle-print/terminal-escapes.rb', line 114

def save_cursor
  "\e[s"
end

.set_cursor(line = 0, column = 0) ⇒ Object



94
95
96
# File 'lib/monocle-print/terminal-escapes.rb', line 94

def set_cursor( line = 0, column = 0 )
  "\e[%i;%iH" % [ line, column ]
end

.single_widthObject



135
136
137
# File 'lib/monocle-print/terminal-escapes.rb', line 135

def single_width
  "\e#5"
end

.underlineObject



78
79
80
# File 'lib/monocle-print/terminal-escapes.rb', line 78

def underline
  "\e[4m"
end

.xterm_color(type, color_index, bold = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/monocle-print/terminal-escapes.rb', line 44

def xterm_color( type, color_index, bold = nil )
  prefix =
    case type
    when ?f then 38  # foreground
    when ?b then 48  # background
    end
  "\e[#{ prefix };5;#{ color_index }m"
end