Object
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] }
# File lib/clio/string.rb, line 53 def +(other) case other when String ntext = text + other.text nmarks = marks.dup omarks = shift_marks(0, text.size, other.marks) omarks.each{ |i, c| nmarks[i].concat(c) } else ntext = text + other.to_s nmarks = marks.dup end self.class.new(ntext, nmarks) end
# File lib/clio/string.rb, line 158 def ansi(code) m = marks.dup m[0] << code m[size] << :clear self.class.new(text, m) end
# File lib/clio/string.rb, line 167 def ansi!(code) marks[0] << ansicolor marks[size] << :clear end
# File lib/clio/string.rb, line 184 def black! ; color!(:black) ; end
# File lib/clio/string.rb, line 49 def downcase ; self.class.new(text.upcase, marks) ; end
# File lib/clio/string.rb, line 50 def downcase! ; text.upcase! ; end
# File lib/clio/string.rb, line 182 def green! ; color!(:green) ; end
# File lib/clio/string.rb, line 153 def gsub(pattern_replacement) dup.gsub(pattern, replacement) end
# File lib/clio/string.rb, line 135 def gsub!(pattern,replacement) mark_changes = [] text = @text.gsub(pattern) do |s| index = $~.begin(0) delta = (replacement.size - s.size) mark_changes << [index, delta] replacement end marks = @marks mark_changes.each do |index, delta| marks = shift_marks(index, delta, marks) end @text = text @marks = marks self end
# File lib/clio/string.rb, line 71 def lr(other, options={}) Split.new(self, other, options) end
# File lib/clio/string.rb, line 177 def magenta ; color(:magenta) ; end
# File lib/clio/string.rb, line 185 def magenta! ; color!(:magenta) ; end
slice
# File lib/clio/string.rb, line 76 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
# File lib/clio/string.rb, line 130 def sub(pattern,replacement) dup.sub!(pattern, replacement) end
This is more limited than the normal String method. It does not yet support a block, and replacement won't substitue for 1, 2, etc.
TODO: block support.
# File lib/clio/string.rb, line 112 def sub!(pattern,replacement) mark_changes = [] text = @text.sub(pattern) do |s| index = $~.begin(0) delta = (replacement.size - s.size) mark_changes << [index, delta] replacement end marks = @marks mark_changes.each do |index, delta| marks = shift_marks(index, delta, marks) end @text = text @marks = marks self end
# 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.reverse_each do |code| s.insert(index, ANSICode.__send__(code)) end end s end
# File lib/clio/string.rb, line 45 def upcase ; self.class.new(text.upcase, marks) ; end
# File lib/clio/string.rb, line 46 def upcase! ; text.upcase! ; end
# File lib/clio/string.rb, line 178 def yellow ; color(:yellow) ; end
Generated with the Darkfish Rdoc Generator 2.