Class: AnsiTerm::String
- Inherits:
-
Object
- Object
- AnsiTerm::String
- Defined in:
- lib/ansiterm/string.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#str ⇒ Object
readonly
Returns the value of attribute str.
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 Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
14 15 16 |
# File 'lib/ansiterm/string.rb', line 14 def attrs @attrs end |
#str ⇒ Object (readonly)
Returns the value of attribute str.
14 15 16 |
# File 'lib/ansiterm/string.rb', line 14 def str @str end |
Instance Method Details
#<<(str) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ansiterm/string.rb', line 129 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
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ansiterm/string.rb', line 110 def[] i str = @str[i] if str a = self.class.new a.set(str,@attrs[i]) a else nil end end |
#[]=(range, str) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ansiterm/string.rb', line 93 def[]= range, str if str.is_a?(self.class) #@str = @str.dup #@attrs = @attrs.dup @str[range] = str.str @attrs[range] = str.attrs else s = @str.dup a = @attrs.dup parse(str) s[range] = @str @str = s a[range] = @attrs @attrs = a end end |
#attr_at(index) ⇒ Object
125 126 127 |
# File 'lib/ansiterm/string.rb', line 125 def attr_at(index) @attrs[index] end |
#char_at(index) ⇒ Object
121 122 123 |
# File 'lib/ansiterm/string.rb', line 121 def char_at(index) @str[index] end |
#encoding ⇒ Object
38 39 40 |
# File 'lib/ansiterm/string.rb', line 38 def encoding @str.encoding end |
#index(str, off = 0) ⇒ Object
46 47 48 |
# File 'lib/ansiterm/string.rb', line 46 def index str, off = 0 @str.index(str,off) end |
#length ⇒ Object
42 43 44 |
# File 'lib/ansiterm/string.rb', line 42 def length @str.length end |
#merge_attr_below(range, attr) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ansiterm/string.rb', line 77 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
54 55 56 |
# File 'lib/ansiterm/string.rb', line 54 def raw return @str, Array(@attrs) end |
#set(str, attrs) ⇒ Object
50 51 52 |
# File 'lib/ansiterm/string.rb', line 50 def set(str,attrs) @str, @attrs = str,Array(attrs) end |
#set_attr(range, attr) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ansiterm/string.rb', line 58 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
16 17 18 |
# File 'lib/ansiterm/string.rb', line 16 def to_plain_str @str.dup end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/ansiterm/string.rb', line 34 def to_s to_str end |
#to_str ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ansiterm/string.rb', line 20 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 |