lib/reterm/util.rb
# Converts specified ansi string to array
# of character data & corresponding control
# logic
Converts specified ansi string to array
of character data & corresponding control
logic
Converts specified ansi string to array of character data & corresponding control logic
def parse_ansi(str)
    require 'strscan'
    r = []
    f = []
    t = []
    a = nil
    s = StringScanner.new(str)
    while(!s.eos?)
      if s.scan(/(\e|\[)\[0m/)
        t << f.pop
        t.compact!
        if f.empty?
          r << [a, t]
          t = []
          a = nil
        end
      elsif s.scan(/\e\[(3[0-7]|90|1)m/)
        f << ANSI_COLORS[s[1].to_i]
      elsif s.scan(/\e\[(([0-9]+;?)+)m/)
        sgr = s[1].split(";").collect { |s| s.to_i }
        f << if (30..37).include?(sgr[0])
               ANSI_COLORS[sgr[1]]
             elsif sgr[0] == 38
               if sgr[1] == 5
                if sgr[2] < 8
                  ANSI_COLORS[sgr[2]]
                elsif sgr[2] < 16
                  ANSI_COLORS[sgr[2]]
                elsif sgr[2] < 232
                  re = (sgr[2] >> 5) * 32
                  gr = ((sgr[2] & 28) >> 2) * 32
                  bl = (sgr[2] & 3) * 64
                  [re, gr, bl]
                else 
                end
               else 
               end
                other sgr commands
             end
      else
        a  = "" if a.nil?
        a += s.scan(/./m)
      end
    end
    r << [a, (t + f).compact] unless f.empty?
    r
  end
def parse_ansi ( str ) require 'strscan' r = [ ] f = [ ] t = [ ] a = nil s = StringScanner . new ( str ) while ( ! s . eos? ) if s . scan ( / ( \e | \[ ) \[ 0m / ) t << f . pop t . compact! if f . empty? r << [ a , t ] t = [ ] a = nil end elsif s . scan ( / \e \[ (3[0-7]|90|1)m / ) f << ANSI_COLORS [ s [ 1 ] . to_i ] elsif s . scan ( / \e \[ (([0-9]+;?)+)m / ) sgr = s [ 1 ] . split ( ";" ) . collect { | s | s . to_i } f << if ( 30 .. 37 ) . include? ( sgr [ 0 ] ) ANSI_COLORS [ sgr [ 1 ] ] elsif sgr [ 0 ] == 38 if sgr [ 1 ] == 5 if sgr [ 2 ] < 8 ANSI_COLORS [ sgr [ 2 ] ] elsif sgr [ 2 ] < 16 ANSI_COLORS [ sgr [ 2 ] ] elsif sgr [ 2 ] < 232 re = ( sgr [ 2 ] >> 5 ) * 32 gr = ( ( sgr [ 2 ] & 28 ) >> 2 ) * 32 bl = ( sgr [ 2 ] & 3 ) * 64 [ re , gr , bl ] else end else end end else a = "" if a . nil? a += s . scan ( / . /m ) end end r << [ a , ( t + f ) . compact ] unless f . empty? r end
Converts specified ansi string to array
of character data & corresponding control
logic
Converts specified ansi string to array of character data & corresponding control logic
# end of formatting # basic formatter # FIXME need to register formatting for 'a' # up to this point (and reset 'a') (and below) # sgr # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters # TODO verify: # https://stackoverflow.com/questions/12338015/converting-8-bit-color-into-rgb-value # if srg[2] < 256 # TODO # if sgr[1] == 2 # TODO # TODO other sgr commands # handle remaining / lingering data
str
<not_specific>