Class: RubyRich::AnsiCode

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/ansi_code.rb

Constant Summary collapse

ANSI_CODES =
{
  reset: "\e[0m",
  bold: "1",
  faint: "2",
  italic: "3",
  underline: "4",
  curly_underline: "4:3",
  dotted_underline: "4:4",
  dashed_underline: "4:5",
  double_underline: "21",
  blink: "5",
  rapid_blink: "6",
  inverse: "7",
  invisible: "8",
  strikethrough: "9",
  fraktur: "20",
  no_blink: "25",
  no_inverse: "27",
  overline: "53",
  color: {
    black: "30",
    red: "31",
    green: "32",
    yellow: "33",
    blue: "34",
    magenta: "35",
    cyan: "36",
    white: "37"
  },
  bright_color:{
    black: "90",
    red: "91",
    green: "92",
    yellow: "93",
    blue: "94",
    magenta: "95",
    cyan: "96",
    white: "97"
  },
  background: {
    black: "40",
    red: "41",
    green: "42",
    yellow: "43",
    blue: "44",
    magenta: "45",
    cyan: "46",
    white: "47"
  },
  bright_background: {
    black: "100",
    red: "101",
    green: "102",
    yellow: "103",
    blue: "104",
    magenta: "105",
    cyan: "106",
    white: "107"
  }
}

Class Method Summary collapse

Class Method Details

.background(color, bright = false) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/ruby_rich/ansi_code.rb', line 76

def self.background(color, bright=false)
  if bright
    "\e[#{ANSI_CODES[:bright_background][color]}m"
  else
    "\e[#{ANSI_CODES[:background][color]}m"
  end
end


107
108
109
# File 'lib/ruby_rich/ansi_code.rb', line 107

def self.blink
  "\e[#{ANSI_CODES[:blink]}m"
end

.boldObject



84
85
86
# File 'lib/ruby_rich/ansi_code.rb', line 84

def self.bold
  "\e[#{ANSI_CODES[:bold]}m"
end

.color(color, bright = false) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/ruby_rich/ansi_code.rb', line 68

def self.color(color, bright=false)
  if bright
    "\e[#{ANSI_CODES[:bright_color][color]}m"
  else
    "\e[#{ANSI_CODES[:color][color]}m"
  end
end

.font(font_color, font_bright: false, background: nil, background_bright: false, bold: false, italic: false, underline: false, underline_style: nil, strikethrough: false, overline: false) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruby_rich/ansi_code.rb', line 143

def self.font(font_color, 
  font_bright: false, 
  background: nil, 
  background_bright: false,
  bold: false, 
  italic: false,
  underline: false,
  underline_style: nil,
  strikethrough: false,
  overline: false
  )
  code = if font_bright
    "\e[#{ANSI_CODES[:bright_color][font_color]}"
  else
    "\e[#{ANSI_CODES[:color][font_color]}"
  end
  if background
    code += ";" + if background_bright
      "#{ANSI_CODES[:bright_background][background]}"
    else
      "#{ANSI_CODES[:background][background]}"
    end
  end
  if bold
    code += ";" + ANSI_CODES[:bold]
  end
  if italic
    code += ";" + ANSI_CODES[:italic]
  end
  if underline
    case underline_style
    when nil
      code += ";" + ANSI_CODES[:underline]
    when :double
      code += ";" + ANSI_CODES[:double_underline]
    when :curly
      code += ";" + ANSI_CODES[:curly_underline]
    when :dotted
      code += ";" + ANSI_CODES[:dotted_underline]
    when :dashed
      code += ";" + ANSI_CODES[:dashed_underline]
    end
  end
  if strikethrough
    code += ";" +  ANSI_CODES[:strikethrough]
  end
  if overline
    code += ";" +  ANSI_CODES[:overline]
  end
  return code+"m"
end

.frakturObject



119
120
121
# File 'lib/ruby_rich/ansi_code.rb', line 119

def self.fraktur
  "\e[#{ANSI_CODES[:fraktur]}m"
end

.inverseObject



115
116
117
# File 'lib/ruby_rich/ansi_code.rb', line 115

def self.inverse
  "\e[#{ANSI_CODES[:inverse]}m"
end

.invisibleObject



123
124
125
# File 'lib/ruby_rich/ansi_code.rb', line 123

def self.invisible
  "\e[#{ANSI_CODES[:invisible]}m"
end

.italicObject



88
89
90
# File 'lib/ruby_rich/ansi_code.rb', line 88

def self.italic
  "\e[#{ANSI_CODES[:italic]}m"
end


135
136
137
# File 'lib/ruby_rich/ansi_code.rb', line 135

def self.no_blink
  "\e[#{ANSI_CODES[:no_blink]}m"
end

.no_inverseObject



139
140
141
# File 'lib/ruby_rich/ansi_code.rb', line 139

def self.no_inverse
  "\e[#{ANSI_CODES[:no_inverse]}m"
end

.overlineObject



131
132
133
# File 'lib/ruby_rich/ansi_code.rb', line 131

def self.overline
  "\e[#{ANSI_CODES[:overline]}m"
end


111
112
113
# File 'lib/ruby_rich/ansi_code.rb', line 111

def self.rapid_blink
  "\e[#{ANSI_CODES[:rapid_blink]}m"
end

.resetObject



64
65
66
# File 'lib/ruby_rich/ansi_code.rb', line 64

def self.reset
  ANSI_CODES[:reset]
end

.strikethroughObject



127
128
129
# File 'lib/ruby_rich/ansi_code.rb', line 127

def self.strikethrough
  "\e[#{ANSI_CODES[:strikethrough]}m"
end

.underline(style = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_rich/ansi_code.rb', line 92

def self.underline(style=nil)
  case style
  when nil
    return "\e[#{ANSI_CODES[:underline]}m"
  when :double
    return "\e[#{ANSI_CODES[:double_underline]}m"
  when :curly
    return "\e[#{ANSI_CODES[:curly_underline]}m"
  when :dotted
    return "\e[#{ANSI_CODES[:dotted_underline]}m"
  when :dashed
    return "\e[#{ANSI_CODES[:dashed_underline]}m"  
  end
end