module Term2IRC

FIXME: This module breaks some termi ANSI pattern, such as: “e[31;0;42mhello, worlde[0m” This must remain string green-backgrounded, but currently it would truncate all the color info.

Constants

ATTRIBUTE_I2T_TABLE
ATTRIBUTE_T2I_TABLE
ATTRIBUTE_TABLE
COLOR_I2T_TABLE
COLOR_T2I_TABLE
COLOR_TABLE
RE_TERMINAL_SEQUENCE
VERSION

Public Instance Methods

meta_to_irc(meta) click to toggle source
# File lib/term2irc.rb, line 60
def meta_to_irc(meta)
  words = ""
  if meta[:reset]
    words << ATTRIBUTE_TABLE[:reset][1]
  end
  if meta[:bold]
    words << ATTRIBUTE_TABLE[:bold][1]
  end
  if meta[:underline]
    words << ATTRIBUTE_TABLE[:underline][1]
  end

  if meta[:foreground] or meta[:background]
    f = meta[:foreground]
    b = meta[:background]
    words << "\x03"
    if f and !b
      words << "#{COLOR_T2I_TABLE[f]}"
    else
      words << "#{f && COLOR_T2I_TABLE[f]},#{COLOR_T2I_TABLE[b]}"
    end
  end
  return words
end
t2i(str) click to toggle source
# File lib/term2irc.rb, line 33
def t2i(str)
  str.gsub(RE_TERMINAL_SEQUENCE) do |part|
    meta_to_irc(term_ansi_to_meta(part))
  end
end
term_ansi_to_meta(part) click to toggle source
# File lib/term2irc.rb, line 39
def term_ansi_to_meta(part)
  meta = {}
  sequences = part.tr("\e[m", '').split(';')
  sequences.each do |sequence|
    # TODO: DRY...
    case sequence
    when "0"
      meta[:reset] = true
    when "1"
      meta[:bold] = true
    when "4"
      meta[:underline] = true
    when /^3[0-7]$/
      meta[:foreground] = sequence[1]
    when /^4[0-7]$/
      meta[:background] = sequence[1]
    end
  end
  return meta
end