Class: RubyRich::Syntax::AnsiFormatter

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

Overview

自定义 ANSI 格式化器

Instance Method Summary collapse

Instance Method Details

#format(tokens) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby_rich/syntax.rb', line 154

def format(tokens)
  output = ''
  tokens.each do |token, value|
    color_code = THEME_COLORS[token.qualname] || 
                THEME_COLORS[token.token_chain.map(&:qualname).find { |t| THEME_COLORS[t] }] ||
                ''
    
    if color_code.empty?
      output << value
    else
      output << "#{color_code}#{value}\e[0m"
    end
  end
  output
end