Class: AnsiTerm::String
- Inherits:
-
Object
- Object
- AnsiTerm::String
- Defined in:
- lib/ansiterm/string.rb
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #[](i) ⇒ Object
- #[]=(range, str) ⇒ Object
- #attr_at(index) ⇒ Object
- #encoding ⇒ Object
- #index(str, off = 0) ⇒ Object
-
#initialize(str = "") ⇒ String
constructor
A new instance of String.
- #length ⇒ Object
- #set(str, attrs) ⇒ Object
- #set_attr(range, attr) ⇒ Object
- #to_plain_str ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
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 |
#encoding ⇒ Object
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 |
#length ⇒ Object
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_str ⇒ Object
10 11 12 |
# File 'lib/ansiterm/string.rb', line 10 def to_plain_str @str.dup end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/ansiterm/string.rb', line 28 def to_s to_str end |