Class: Clio::String
- Inherits:
-
Object
- Object
- Clio::String
- Defined in:
- lib/clio/string.rb
Overview
Clio Strings stores a regular string (@text) and a Hash mapping character index to ansicodes (@marks). For example is we has the string:
"Big Apple"
And applied the color red to it, the marks hash would be:
{ 0=>[:red] , 9=>[:clear] }
Instance Attribute Summary collapse
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #color(ansicolor) ⇒ Object
- #color!(ansicolor) ⇒ Object
- #downcase ⇒ Object
- #downcase! ⇒ Object
- #gsub ⇒ Object
-
#initialize(text = nil, marks = nil) ⇒ String
constructor
A new instance of String.
- #lr(other, options = {}) ⇒ Object
- #size ⇒ Object
-
#slice(*args) ⇒ Object
(also: #[])
slice.
-
#sub(pattern, replacement) ⇒ Object
TODO: block support and 1, 2 support.
- #to_s ⇒ Object
- #upcase ⇒ Object
- #upcase! ⇒ Object
- #|(other) ⇒ Object
Constructor Details
#initialize(text = nil, marks = nil) ⇒ String
Returns a new instance of String.
26 27 28 29 |
# File 'lib/clio/string.rb', line 26 def initialize(text=nil, marks=nil) @text = text || '' @marks = marks || Hash.new{ |h,k| h[k]=[] } end |
Instance Attribute Details
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
24 25 26 |
# File 'lib/clio/string.rb', line 24 def marks @marks end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
23 24 25 |
# File 'lib/clio/string.rb', line 23 def text @text end |
Instance Method Details
#+(other) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clio/string.rb', line 67 def +(other) case other when String ntext = text + other.text nmarks = marks.dup other.marks.each{ |i, c| m[i] << c } else ntext = text + other.to_s nmarks = marks.dup end self.class.new(ntext, nmarks) end |
#color(ansicolor) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/clio/string.rb', line 51 def color(ansicolor) m = marks.dup m[0] << ansicolor m[size] << :clear self.class.new(text, m) end |
#color!(ansicolor) ⇒ Object
45 46 47 48 |
# File 'lib/clio/string.rb', line 45 def color!(ansicolor) marks[0] << ansicolor marks[size] << :clear end |
#downcase ⇒ Object
63 |
# File 'lib/clio/string.rb', line 63 def downcase ; self.class.new(text.upcase, marks) ; end |
#downcase! ⇒ Object
64 |
# File 'lib/clio/string.rb', line 64 def downcase! ; text.upcase! ; end |
#gsub ⇒ Object
133 134 |
# File 'lib/clio/string.rb', line 133 def gsub end |
#lr(other, options = {}) ⇒ Object
84 85 86 |
# File 'lib/clio/string.rb', line 84 def lr(other, ={}) Split.new(self, other, ) end |
#size ⇒ Object
42 |
# File 'lib/clio/string.rb', line 42 def size ; text.size ; end |
#slice(*args) ⇒ Object Also known as: []
slice
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/clio/string.rb', line 89 def slice(*args) if args.size == 2 index, len = *args endex = index+len new_text = text[index, len] new_marks = {} marks.each do |i, v| new_marks[i] = v if i >= index && i < endex end self.class.new(new_text, new_marks) elsif args.size == 1 rng = args.first case rng when Range index, endex = rng.begin, rng.end new_text = text[rng] new_marks = {} marks.each do |i, v| new_marks[i] = v if i >= index && i < endex end self.class.new(new_text, new_marks) else self.class.new(text[rng,1], {rng=>marks[rng]}) end else raise ArgumentError end end |
#sub(pattern, replacement) ⇒ Object
TODO: block support and 1, 2 support.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/clio/string.rb', line 121 def sub(pattern,replacement) if md = pattern.match(@text) delta = replacement.size - md.size marks2 = shift_marks(md.end, delta) text2 = text.sub(pattern,replacement) self.class.new(text2, marks2) else self.class.new(text, marks) end end |
#to_s ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/clio/string.rb', line 31 def to_s s = text.dup m = marks.sort{ |a,b| b[0] <=> a[0] } m.each do |index, codes| codes.each do |code| s.insert(index, ANSICode.send(code)) end end s end |
#upcase ⇒ Object
59 |
# File 'lib/clio/string.rb', line 59 def upcase ; self.class.new(text.upcase, marks) ; end |
#upcase! ⇒ Object
60 |
# File 'lib/clio/string.rb', line 60 def upcase! ; text.upcase! ; end |
#|(other) ⇒ Object
80 81 82 |
# File 'lib/clio/string.rb', line 80 def |(other) Split.new(self, other) end |