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
- #char_at(index) ⇒ Object
- #encoding ⇒ Object
- #index(str, off = 0) ⇒ Object
-
#initialize(str = nil) ⇒ String
constructor
A new instance of String.
- #length ⇒ Object
- #merge_attr_below(range, attr) ⇒ Object
- #raw ⇒ Object
- #set(str, attrs) ⇒ Object
- #set_attr(range, attr) ⇒ Object
- #to_plain_str ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(str = nil) ⇒ String
Returns a new instance of String.
5 6 7 8 9 10 11 12 |
# File 'lib/ansiterm/string.rb', line 5 def initialize(str=nil) if str parse(str) else @str = "" @attrs = [] end end |
Instance Method Details
#<<(str) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ansiterm/string.rb', line 118 def << str return self if str.nil? if !str.kind_of?(self.class) parse(self.to_str + "\e[0m" + str.to_str) return self end s,a = str.raw @str.concat(s) @attrs.concat(a) self end |
#[](i) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ansiterm/string.rb', line 99 def[] i str = @str[i] if str a = self.class.new a.set(str,@attrs[i]) a else nil end end |
#[]=(range, str) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/ansiterm/string.rb', line 91 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
114 115 116 |
# File 'lib/ansiterm/string.rb', line 114 def attr_at(index) @attrs[index] end |
#char_at(index) ⇒ Object
110 111 112 |
# File 'lib/ansiterm/string.rb', line 110 def char_at(index) @str[index] end |
#encoding ⇒ Object
36 37 38 |
# File 'lib/ansiterm/string.rb', line 36 def encoding @str.encoding end |
#index(str, off = 0) ⇒ Object
44 45 46 |
# File 'lib/ansiterm/string.rb', line 44 def index str, off = 0 @str.index(str,off) end |
#length ⇒ Object
40 41 42 |
# File 'lib/ansiterm/string.rb', line 40 def length @str.length end |
#merge_attr_below(range, attr) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ansiterm/string.rb', line 75 def merge_attr_below(range, attr) min = [0,range.first - 1].max fattr = @attrs[min] r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually" last = nil @attrs[range] = @attrs[range].map do |a| if a.bgcol == 49 n = attr.merge(a, ignore: :bgcol) else n = attr.merge(a) end last == n ? last : n end #fattr #@attrs[min+r].merge(fattr) end |
#raw ⇒ Object
52 53 54 |
# File 'lib/ansiterm/string.rb', line 52 def raw return @str, Array(@attrs) end |
#set(str, attrs) ⇒ Object
48 49 50 |
# File 'lib/ansiterm/string.rb', line 48 def set(str,attrs) @str, @attrs = str,Array(attrs) end |
#set_attr(range, attr) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ansiterm/string.rb', line 56 def set_attr(range, attr) min = [range.first - 1,0].max fattr = @attrs[min] #attr = fattr.merge(attr) r = Array(@attrs[range]).count # Inefficient, but saves dealing with negative offsets etc. "manually" last = nil @attrs[range] = @attrs[range]&.map do |a| n = a ? a.merge(attr) : attr last == n ? last : n end || [] rm = range.last+1 if a = @attrs[rm] @attrs[rm] = Attr.new.merge(fattr).merge(a) if !a.bgcol @attrs[rm] = @attrs[rm].merge({bgcol: 49}) end end end |
#to_plain_str ⇒ Object
14 15 16 |
# File 'lib/ansiterm/string.rb', line 14 def to_plain_str @str.dup end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/ansiterm/string.rb', line 32 def to_s to_str end |
#to_str ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ansiterm/string.rb', line 18 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 |