Class: Hansi::SexpRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/hansi/sexp_renderer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme: :default, mode: Hansi.mode, join: "") ⇒ SexpRenderer

Returns a new instance of SexpRenderer.



9
10
11
12
13
# File 'lib/hansi/sexp_renderer.rb', line 9

def initialize(theme: :default, mode: Hansi.mode, join: "")
  @theme = Theme[theme]
  @mode  = mode
  @join  = join
end

Class Method Details

.render(*args, **options) ⇒ Object



5
6
7
# File 'lib/hansi/sexp_renderer.rb', line 5

def self.render(*args, **options)
  new(**options).render(args)
end

Instance Method Details

#render(input, codes: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hansi/sexp_renderer.rb', line 15

def render(input, codes: nil)
  return "#{codes}#{input}#{Hansi.reset}" unless input.respond_to? :to_ary and sexp = input.to_ary
  return render(sexp.first) if sexp.size < 2

  style, *content = sexp
  codes ||= Hansi.reset
  style &&= @theme[style]
  codes  += style.to_ansi(mode: @mode).to_s if style

  content.map { |e| render(e, codes: codes) }.join(@join)
end