Module: IRB::Color

Defined in:
lib/irb/color.rb

Defined Under Namespace

Classes: ColorizeVisitor

Constant Summary collapse

CLEAR =
0
BOLD =
1
UNDERLINE =
4
REVERSE =
7
BLACK =
30
RED =
31
GREEN =
32
YELLOW =
33
BLUE =
34
MAGENTA =
35
CYAN =
36
WHITE =
37

Class Method Summary collapse

Class Method Details

.clear(colorable: colorable?) ) ⇒ Object



152
153
154
# File 'lib/irb/color.rb', line 152

def clear(colorable: colorable?)
  colorable ? CLEAR_SEQ : ''
end

.colorable?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/irb/color.rb', line 119

def colorable?
  supported = $stdout.tty? && (/mswin|mingw/.match?(RUBY_PLATFORM) || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))

  # because ruby/debug also uses irb's color module selectively,
  # irb won't be activated in that case.
  if IRB.respond_to?(:conf)
    supported && !!IRB.conf.fetch(:USE_COLORIZE, true)
  else
    supported
  end
end

.colorize(text, seq, colorable: colorable?) ) ⇒ Object



156
157
158
159
160
# File 'lib/irb/color.rb', line 156

def colorize(text, seq, colorable: colorable?)
  return text unless colorable
  seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
  "#{seq}#{text}#{CLEAR_SEQ}"
end

.colorize_code(code, complete: true, ignore_error: false, colorable: colorable?, , local_variables: []) ⇒ Object

If ‘complete` is false (code is incomplete), this does not warn compile_error. This option is needed to avoid warning a user when the compile_error is happening because the input is not wrong but just incomplete.



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
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
# File 'lib/irb/color.rb', line 165

def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?, local_variables: [])
  return code unless colorable

  result = Prism.parse_lex(code, scopes: [local_variables])

  # IRB::ColorPrinter skips colorizing syntax invalid fragments
  return Reline::Unicode.escape_for_print(code) if ignore_error && !result.success?

  prism_node, prism_tokens = result.value
  errors = result.errors

  unless complete
    errors = filter_incomplete_code_errors(errors, prism_tokens)
  end

  visitor = ColorizeVisitor.new
  prism_node.accept(visitor)

  error_tokens = errors.map { |e| [e.location.start_line, e.location.start_column, 0, e.location.end_line, e.location.end_column, :error, e.location.slice] }
  error_tokens.reject! { |t| t.last.match?(/\A\s*\z/) }
  tokens = prism_tokens.map { |t,| [t.location.start_line, t.location.start_column, 2, t.location.end_line, t.location.end_column, t.type, t.value] }
  tokens.pop if tokens.last&.[](5) == :EOF

  colored = +''
  line_index = 0
  col = 0
  lines = code.lines
  flush = -> next_line_index, next_col {
    return if next_line_index == line_index && next_col == col
    (line_index...[next_line_index, lines.size].min).each do |ln|
      colored << Reline::Unicode.escape_for_print(lines[line_index].byteslice(col..))
      line_index = ln + 1
      col = 0
    end
    unless col == next_col
      colored << Reline::Unicode.escape_for_print(lines[next_line_index].byteslice(col..next_col - 1))
    end
  }

  (visitor.tokens + tokens + error_tokens).sort.each do |start_line, start_column, _priority, end_line, end_column, type, value|
    next if start_line - 1 < line_index || (start_line - 1 == line_index && start_column < col)

    flush.call(start_line - 1, start_column)
    if type == :__END__
      color = TOKEN_SEQS[type]
      end_line = start_line
      value = '__END__'
      end_column = start_column + 7
    else
      color = TOKEN_SEQS[type]
    end
    if color
      value.split(/(\n)/).each do |s|
        colored << (s == "\n" ? s : "#{color}#{Reline::Unicode.escape_for_print(s)}#{CLEAR_SEQ}")
      end
    else
      colored << value
    end
    line_index = end_line - 1
    col = end_column
  end
  flush.call lines.size, 0
  colored
end

.inspect_colorable?(obj, seen: {}.compare_by_identity) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/irb/color.rb', line 131

def inspect_colorable?(obj, seen: {}.compare_by_identity)
  case obj
  when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
    true
  when Hash
    without_circular_ref(obj, seen: seen) do
      obj.all? { |k, v| inspect_colorable?(k, seen: seen) && inspect_colorable?(v, seen: seen) }
    end
  when Array
    without_circular_ref(obj, seen: seen) do
      obj.all? { |o| inspect_colorable?(o, seen: seen) }
    end
  when Range
    inspect_colorable?(obj.begin, seen: seen) && inspect_colorable?(obj.end, seen: seen)
  when Module
    !obj.name.nil?
  else
    false
  end
end