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
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ansiterm/string.rb', line 120 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
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ansiterm/string.rb', line 101 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 98 99 |
# File 'lib/ansiterm/string.rb', line 91 def[]= range, str s = @str.dup a = @attrs.dup parse(str) s[range] = @str @str = s a[range] = @attrs @attrs = a end |
#attr_at(index) ⇒ Object
116 117 118 |
# File 'lib/ansiterm/string.rb', line 116 def attr_at(index) @attrs[index] end |
#char_at(index) ⇒ Object
112 113 114 |
# File 'lib/ansiterm/string.rb', line 112 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 |