Class: AnsiTerm::String

Inherits:
Object
  • Object
show all
Defined in:
lib/ansiterm/string.rb

Instance Method Summary collapse

Constructor Details

#initialize(str = "") ⇒ String

Returns a new instance of String.



6
7
8
# File 'lib/ansiterm/string.rb', line 6

def initialize(str="")
  parse(str)
end

Instance Method Details

#<<(str) ⇒ Object



82
83
84
# File 'lib/ansiterm/string.rb', line 82

def << str
  parse(self.to_str + "\e[0m" + str.to_str)
end

#[](i) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ansiterm/string.rb', line 67

def[] i
  str = @str[i]
  if str
    a = self.class.new
    a.set(str,@attrs[i])
    a
  else
    nil
  end
end

#[]=(range, str) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/ansiterm/string.rb', line 59

def[]= range, str
  s = @str
  a = @attrs
  parse(str)
  @str   = s[0..(range.min-1)].to_s + @str   + s[(range.max)..-1].to_s
  @attrs = a[0..(range.min-1)].to_a + @attrs + a[(range.max)..-1].to_a
end

#attr_at(index) ⇒ Object



78
79
80
# File 'lib/ansiterm/string.rb', line 78

def attr_at(index)
  @attrs[index]
end

#encodingObject



32
33
34
# File 'lib/ansiterm/string.rb', line 32

def encoding
  @str.encoding
end

#index(str, off = 0) ⇒ Object



40
41
42
# File 'lib/ansiterm/string.rb', line 40

def index str, off = 0
  @str.index(str,off)
end

#lengthObject



36
37
38
# File 'lib/ansiterm/string.rb', line 36

def length
  @str.length
end

#set(str, attrs) ⇒ Object



44
45
46
# File 'lib/ansiterm/string.rb', line 44

def set(str,attrs)
  @str, @attrs = str,Array(attrs)
end

#set_attr(range, attr) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ansiterm/string.rb', line 48

def set_attr(range, attr)
  min = range.first - 1
  fattr = @attrs[min]
  attr = fattr.merge(attr) if fattr
  r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually"
  last = nil
  @attrs[range] = @attrs[range].map do |a| 
    a == last ? a : last = attr.merge(a)
  end
end

#to_plain_strObject



10
11
12
# File 'lib/ansiterm/string.rb', line 10

def to_plain_str
  @str.dup
end

#to_sObject



28
29
30
# File 'lib/ansiterm/string.rb', line 28

def to_s
  to_str
end

#to_strObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ansiterm/string.rb', line 14

def to_str
  out = ""
  a = Attr.new
  @str.length.times.each do |i|
    if a != @attrs[i]
      old = a
      a = @attrs[i]||Attr.new
      out << old.transition_to(a)
    end
    out << @str[i]
  end
  out
end