Class: Canis::AnsiParser

Inherits:
Object show all
Defined in:
lib/canis/core/util/ansiparser.rb

Instance Method Summary collapse

Instance Method Details

#parse_format(s) ⇒ nil

NOTE: Experimental and minimal parses the formatted string and yields either an array of color, bgcolor and attrib or the text. This will be called by convert_to_chunk.

Currently, assumes colors and attributes are correct. No error checking or fancy stuff.

s="#[fg=green]hello there#[fg=yellow, bg=black, dim]"

Returns:

  • (nil)

    knows nothign about output format.

Since:

  • 1.4.1 2011-11-3 experimental, can change



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/canis/core/util/ansiparser.rb', line 28

def parse_format s  # yields attribs or text
  ## set default colors
  color   = :white
  bgcolor = :black
  attrib  = FFI::NCurses::A_NORMAL
  text    = ""

  ## split #[...]
  #a       = s.split /(#\[[^\]]*\])/
  a       = s.split /(\x1b\[\d*(?:;\d+)*?[a-zA-Z])/
    a.each { |e| 
    ## process color or attrib portion
    #[ "", "\e[1m", "", "\e[34m", "", "\e[47m", "Showing all items...", "\e[0m", "", "\e[0m", "\n"]
    if e[0,1] == "\x1b" && e[-1,1] == "m"

      #e.each { |f|  x=/^.\[(.*).$/.match(f) 
      $log.debug "XXX: ANSI e #{e} "
      x=/^.\[(.*).$/.match(e) 
        color, bgcolor, attrib = nil, nil, nil
        $log.debug "XXX: ANSI #{x} ..... #{x[1]}  "
        args = x[1].split ';'
        ## first split on commas to separate fg, bg and attr
        # http://ascii-table.com/ansi-escape-sequences.php
        args.each { |att|  
          $log.debug "XXX: ANSI att: #{att}   "
          case att.to_i
          when 0
            color, bgcolor, attrib = nil, nil, nil
            yield :reset # actually this resets all so we need an endall or clearall reset

          when 1
            attrib = 'bold'
          when 2
            attrib = 'dim'
          when 4
            attrib = 'underline'
          when 5
            attrib = 'blink'
          when 7
            attrib = 'reverse'
          when 8
            attrib = 'hidden' # XXX
          when 30 
            color = 'black'
          when 31  
            color = 'red'
          when 32  
            color = 'green'
          when 33  
            color = 'yellow'
          when 34  
            color = 'blue'
          when 35  
            color = 'magenta'
          when 36  
            color = 'cyan'
          when 37  
            color = 'white'

            #Background colors
          when 40  
            bgcolor = 'black'
          when 41  
            bgcolor = 'red'
          when 42  
            bgcolor = 'green'
          when 43  
            bgcolor = 'yellow'
          when 44  
            bgcolor = 'blue'
          when 45  
            bgcolor = 'magenta'
          when 46  
            bgcolor = 'cyan'
          when 47  
            bgcolor = 'white'
          else
            $log.warn "XXX: WARN ANSI not used #{att} "
          end
        } # args.ea
        #} # e.each
        $log.debug "XXX:  ANSI YIELDING #{color} , #{bgcolor} , #{attrib} "
        yield [color,bgcolor,attrib] if block_given?
    else
      text = e
      yield text if block_given?
    end
  } # a.each
end