Module: Term2IRC

Extended by:
Term2IRC
Included in:
Term2IRC
Defined in:
lib/term2irc.rb,
lib/term2irc/version.rb

Overview

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.

Constant Summary collapse

ATTRIBUTE_TABLE =
{
  :reset     => ["0", "\x0f"],
  :bold      => ["1", "\x02"],
  :underline => ["4", "\x1f"],
}
ATTRIBUTE_T2I_TABLE =
ATTRIBUTE_I2T_TABLE =
COLOR_TABLE =
{
  :black   => ["0", "1"],
  :red     => ["1", "4"],
  :green   => ["2", "3"],
  :yellow  => ["3", "8"],
  :blue    => ["4", "12"],
  :magenta => ["5", "13"],
  :cyan    => ["6", "11"],
  :white   => ["7", "0"],
}
COLOR_T2I_TABLE =
COLOR_I2T_TABLE =
RE_TERMINAL_SEQUENCE =
/(\e\[(?:\d+(?:;\d+)*)m)/
VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#meta_to_irc(meta) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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