class ColorFilter

The ColorFilter class implements ANSI color (SGR) support.

A Filter can keep state and partial data

Public Instance Methods

filter_out(str) click to toggle source

The #filter_out method filters output data

str

The string to be processed

return

The filtered data

# File lib/network/protocol/colorfilter.rb, line 28
def filter_out(str)
  return "" if str.nil? || str.empty?
  if @pstack.color_on
    str.gsub!(%r\[COLOR\s+(\w+)\s+ON\s+(\w+)\]/i) do |m|
      if ColorTable[$1] && ColorTable[$2]
        ColorTable[$1][2]+ColorTable[$2][3]
      else
        ''
      end
    end
    str.gsub!(%r\[COLOR\s+(\w+)\]/i) do |m|
      if ColorTable[$1]
        ColorTable[$1][2]
      else
        ''
      end
    end
    str.gsub!(%r\[\/COLOR\]/i) do |m|
      ANSICODE['reset']
    end
    str.gsub!(%r\[[BI]\]/i) do |m|
      ANSICODE['bold']
    end
    str.gsub!(%r\[U\]/i) do |m|
      ANSICODE['underline']
    end
    str.gsub!(%r\[\/[BUI]\]/i) do |m|
      ANSICODE['reset']
    end
  else
    str.gsub!(%r\[COLOR\s+(\w+)\s+ON\s+(\w+)\]/i,'')
    str.gsub!(%r\[COLOR\s+(\w+)\]|\[\/COLOR\]/i, '')
    str.gsub!(%r\[SIZE .*?\]|\[\/SIZE\]/i, '')
    str.gsub!(%r\[FONT .*?\]|\[\/FONT\]/i, '')
    str.gsub!(%r\[[BUI]\]|\[\/[BUI]\]/i, '')
  end
  str
end