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



80
81
82
83
84
85
86
87
88
# File 'lib/ruby_rich/ansi_code.rb', line 80

def self.background(color, bright=false)
  return "" unless color_enabled?

  if bright
    "\e[#{ANSI_CODES[:bright_background][color]}m"
  else
    "\e[#{ANSI_CODES[:background][color]}m"
  end
end


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

def self.blink
  return "" unless color_enabled?

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

.boldObject



90
91
92
93
94
# File 'lib/ruby_rich/ansi_code.rb', line 90

def self.bold
  return "" unless color_enabled?

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

.color(color, bright = false) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/ruby_rich/ansi_code.rb', line 70

def self.color(color, bright=false)
  return "" unless color_enabled?

  if bright
    "\e[#{ANSI_CODES[:bright_color][color]}m"
  else
    "\e[#{ANSI_CODES[:color][color]}m"
  end
end

.color_enabled=(enabled) ⇒ Object



237
238
239
# File 'lib/ruby_rich/ansi_code.rb', line 237

def self.color_enabled=(enabled)
  @color_enabled = enabled
end

.color_enabled?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/ruby_rich/ansi_code.rb', line 233

def self.color_enabled?
  ENV["NO_COLOR"].nil? && ENV["TERM"] != "dumb" && @color_enabled != false
end

.faintObject



96
97
98
99
100
# File 'lib/ruby_rich/ansi_code.rb', line 96

def self.faint
  return "" unless color_enabled?

  "\e[#{ANSI_CODES[:faint]}m"
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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruby_rich/ansi_code.rb', line 179

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
  )
  return "" unless color_enabled?

  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



143
144
145
146
147
# File 'lib/ruby_rich/ansi_code.rb', line 143

def self.fraktur
  return "" unless color_enabled?

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

.inverseObject



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

def self.inverse
  return "" unless color_enabled?

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

.invisibleObject



149
150
151
152
153
# File 'lib/ruby_rich/ansi_code.rb', line 149

def self.invisible
  return "" unless color_enabled?

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

.italicObject



102
103
104
105
106
# File 'lib/ruby_rich/ansi_code.rb', line 102

def self.italic
  return "" unless color_enabled?

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


167
168
169
170
171
# File 'lib/ruby_rich/ansi_code.rb', line 167

def self.no_blink
  return "" unless color_enabled?

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

.no_inverseObject



173
174
175
176
177
# File 'lib/ruby_rich/ansi_code.rb', line 173

def self.no_inverse
  return "" unless color_enabled?

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

.overlineObject



161
162
163
164
165
# File 'lib/ruby_rich/ansi_code.rb', line 161

def self.overline
  return "" unless color_enabled?

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


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

def self.rapid_blink
  return "" unless color_enabled?

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

.resetObject



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

def self.reset
  return "" unless color_enabled?

  ANSI_CODES[:reset]
end

.strikethroughObject



155
156
157
158
159
# File 'lib/ruby_rich/ansi_code.rb', line 155

def self.strikethrough
  return "" unless color_enabled?

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

.underline(style = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_rich/ansi_code.rb', line 108

def self.underline(style=nil)
  return "" unless color_enabled?

  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