Class: Yatte::Syntax::Theme

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

Direct Known Subclasses

Yatte::Syntax::Themes::Default

Constant Summary collapse

RESET =
"\x1b[0m"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



21
22
23
# File 'lib/yatte/syntax/theme.rb', line 21

def default
  @default ||= find(:default)&.new
end

.find(name) ⇒ Object



17
18
19
# File 'lib/yatte/syntax/theme.rb', line 17

def find(name)
  registry[name]
end

.inherited(subclass) ⇒ Object



25
26
27
28
# File 'lib/yatte/syntax/theme.rb', line 25

def inherited(subclass)
  super
  subclass.instance_variable_set(:@styles, styles.dup)
end

.register(name, klass) ⇒ Object



13
14
15
# File 'lib/yatte/syntax/theme.rb', line 13

def register(name, klass)
  registry[name] = klass
end

.registryObject



9
10
11
# File 'lib/yatte/syntax/theme.rb', line 9

def registry
  @registry ||= {}
end

.style(token_type, ansi_code) ⇒ Object



39
40
41
# File 'lib/yatte/syntax/theme.rb', line 39

def style(token_type, ansi_code)
  styles[token_type] = ansi_code
end

.stylesObject



43
44
45
# File 'lib/yatte/syntax/theme.rb', line 43

def styles
  @styles ||= {}
end

.tag(name = nil) ⇒ Object

DSL methods for subclasses



32
33
34
35
36
37
# File 'lib/yatte/syntax/theme.rb', line 32

def tag(name = nil)
  return @tag if name.nil?

  @tag = name.to_sym
  Theme.register(@tag, self)
end

Instance Method Details

#color_for(token_type) ⇒ Object



48
49
50
# File 'lib/yatte/syntax/theme.rb', line 48

def color_for(token_type)
  self.class.styles[token_type] || self.class.styles[:plain] || RESET
end

#colorize(tokens) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/yatte/syntax/theme.rb', line 52

def colorize(tokens)
  buf = +""
  tokens.each do |text, type|
    buf << color_for(type) << text << RESET
  end
  buf
end