Class: AnsiArt::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi_art/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(ansi) ⇒ Document

Returns a new instance of Document.



3
4
5
# File 'lib/ansi_art/document.rb', line 3

def initialize ansi
  @ansi = ansi
end

Instance Method Details

#convert(conv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ansi_art/document.rb', line 15

def convert conv
  buffer = Buffer.new
  ctrl_seq = ""
  high_byte = false  # current status

  @ansi.bytes do |b|
    if(ctrl_seq.empty? && b != 0x1B) #if is not ctrl sequence
      case b
        when 10 #newline
          conv.put buffer.to_s!
          conv.newLine
        when 13 #ignore \r
        else
          buffer.push b
          high_byte = (!high_byte && b > 128)
        end
      else # is control sequence
        ctrl_seq += (c = [b].pack('C*'))
        if(c.match(/[0-9;\[\x1B]/).nil?)
          if(c == "m") # terminal color config
            ## ANSI Convert##
            # Remove half byte from string before put to converter
            half_char = buffer.slice!(-1) if high_byte
            # puts string with old color settings
            conv.put buffer.to_s!  unless buffer.empty?

            if high_byte
              buffer.push half_char
              # ask converter to store left side color
              conv.wait_half_char!
            end

            # Strip esc chars and "[" and tail char
            ctrl_seq.gsub! /[\x1B\[]/ , ''
            ctrl_seq.slice! -1

            #split with ";" spliter
            confs = ctrl_seq.split(';')
            if(confs.empty?) #*[m = clear setting
              conv.reset_color
            else
              ctrl_seq.split(';').each do |conf|
                case conf = conf.to_i
                  when 0 then conv.reset_color
                  when 1 then conv.set_color_for :bri, true
                  when 30..37 then conv.set_color_for :fg, conf % 10
                  when 40..47 then conv.set_color_for :bg, conf % 10
                end
              end
            end
            conv.commit_color
          end
          ctrl_seq = ""
        end
      end
  end
  conv.put buffer.to_s
  return conv.output
end

#to_htmlObject



7
8
9
# File 'lib/ansi_art/document.rb', line 7

def to_html
  return convert HtmlConverter.new
end

#to_png(w = 800, o = {}) ⇒ Object



11
12
13
# File 'lib/ansi_art/document.rb', line 11

def to_png w=800, o={}
  return convert PngConverter.new(w,o)
end