Class: Chunks::ColorParser

Inherits:
Object show all
Defined in:
lib/rbcurse/core/include/chunk.rb

Instance Method Summary collapse

Constructor Details

#initialize(cp) ⇒ ColorParser

Returns a new instance of ColorParser.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbcurse/core/include/chunk.rb', line 76

def initialize cp
  color_parser cp
  @color_pair = $datacolor
  @attrib     = FFI::NCurses::A_NORMAL
  @color_array = [:white]
  @bgcolor_array = [:black]
  @attrib_array = [@attrib]
  @color_pair_array = [@color_pair]
  @color = :white
  @bgcolor = :black
end

Instance Method Details

#color_parser(f) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbcurse/core/include/chunk.rb', line 170

def color_parser f
  $log.debug "XXX:  color_parser setting in CP to #{f} "
  if f == :tmux
    @color_parser = get_default_color_parser()
  elsif f == :ansi
    require 'rbcurse/core/util/ansiparser'
    @color_parser = AnsiParser.new
  else
    @color_parser = f
  end
end

#convert_to_chunk(s, colorp = $datacolor, att = FFI::NCurses::A_NORMAL) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbcurse/core/include/chunk.rb', line 95

def convert_to_chunk s, colorp=$datacolor, att=FFI::NCurses::A_NORMAL
  #require 'rbcurse/core/include/chunk'

  @color_parser ||= get_default_color_parser()
  ## defaults
  color_pair = @color_pair
  attrib = @attrib
  #res = []
  res = ChunkLine.new
  color = @color
  bgcolor = @bgcolor
  # stack the values, so when user issues "/end" we can pop earlier ones

  @color_parser.parse_format(s) do |p|
    case p
    when Array
      ## got color / attrib info, this starts a new span

      #color, bgcolor, attrib = *p
      lc, lb, la = *p
      if la
        @attrib = get_attrib la
      end
      if lc || lb
        @color = lc ? lc : @color_array.last
        @bgcolor = lb ? lb : @bgcolor_array.last
        @color_array << @color
        @bgcolor_array << @bgcolor
        @color_pair = get_color($datacolor, @color, @bgcolor)
      end
      @color_pair_array << @color_pair
      @attrib_array << @attrib
      #$log.debug "XXX: CHUNK start #{color_pair} , #{attrib} :: c:#{lc} b:#{lb} "
      #$log.debug "XXX: CHUNK start arr #{@color_pair_array} :: #{@attrib_array} "

    when :endcolor

      # end the current (last) span
      @color_pair_array.pop
      @color_pair = @color_pair_array.last
      @attrib_array.pop
      @attrib = @attrib_array.last
      #$log.debug "XXX: CHUNK end #{color_pair} , #{attrib} "
      #$log.debug "XXX: CHUNK end arr #{@color_pair_array} :: #{@attrib_array} "
    when :reset   # ansi has this
      # end all previous colors
      @color_pair = $datacolor # @color_pair_array.first
      @color_pair_array = [@color_pair]
      @attrib = FFI::NCurses::A_NORMAL #@attrib_array.first
      @attrib_array = [@attrib]
      @bgcolor_array = [@bgcolor_array.first]
      @color_array = [@color_array.first]

    when String

      ## create the chunk
      #$log.debug "XXX:  CHUNK     using on #{p}  : #{@color_pair} , #{@attrib} " # 2011-12-10 12:38:51

      #chunk =  [color_pair, p, attrib] 
      chunk = Chunk.new @color_pair, p, @attrib
      if block_given?
        yield chunk
      else
        res << chunk
      end
    end
  end # parse
  return res unless block_given?
end

#get_default_color_parserObject



164
165
166
167
# File 'lib/rbcurse/core/include/chunk.rb', line 164

def get_default_color_parser
  require 'rbcurse/core/util/colorparser'
  @color_parser || DefaultColorParser.new
end