class Clio::String
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] }
Attributes
marks[R]
text[R]
Public Class Methods
new(text=nil, marks=nil)
click to toggle source
# File lib/clio/string.rb, line 26 def initialize(text=nil, marks=nil) @text = text || '' @marks = marks || Hash.new{ |h,k| h[k]=[] } end
Public Instance Methods
+(other)
click to toggle source
# 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
ansi(code)
click to toggle source
# 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
Also aliased as: color
ansi!(code)
click to toggle source
# File lib/clio/string.rb, line 167 def ansi!(code) marks[0] << ansicolor marks[size] << :clear end
Also aliased as: color!
black()
click to toggle source
# File lib/clio/string.rb, line 176 def black ; color(:black) ; end
black!()
click to toggle source
# File lib/clio/string.rb, line 184 def black! ; color!(:black) ; end
blue()
click to toggle source
# File lib/clio/string.rb, line 175 def blue ; color(:blue) ; end
blue!()
click to toggle source
# File lib/clio/string.rb, line 183 def blue! ; color!(:blue) ; end
cyan()
click to toggle source
# File lib/clio/string.rb, line 179 def cyan ; color(:cyan) ; end
cyan!()
click to toggle source
# File lib/clio/string.rb, line 187 def cyan! ; color!(:cyan) ; end
downcase()
click to toggle source
# File lib/clio/string.rb, line 49 def downcase ; self.class.new(text.upcase, marks) ; end
downcase!()
click to toggle source
# File lib/clio/string.rb, line 50 def downcase! ; text.upcase! ; end
green()
click to toggle source
# File lib/clio/string.rb, line 174 def green ; color(:green) ; end
green!()
click to toggle source
# File lib/clio/string.rb, line 182 def green! ; color!(:green) ; end
gsub(pattern_replacement)
click to toggle source
# File lib/clio/string.rb, line 153 def gsub(pattern_replacement) dup.gsub(pattern, replacement) end
gsub!(pattern,replacement)
click to toggle source
# 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
lr(other, options={})
click to toggle source
# File lib/clio/string.rb, line 71 def lr(other, options={}) Split.new(self, other, options) end
magenta()
click to toggle source
# File lib/clio/string.rb, line 177 def magenta ; color(:magenta) ; end
magenta!()
click to toggle source
# File lib/clio/string.rb, line 185 def magenta! ; color!(:magenta) ; end
red()
click to toggle source
# File lib/clio/string.rb, line 173 def red ; color(:red) ; end
red!()
click to toggle source
# File lib/clio/string.rb, line 181 def red! ; color!(:red) ; end
size()
click to toggle source
# File lib/clio/string.rb, line 42 def size ; text.size ; end
slice(*args)
click to toggle source
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
Also aliased as: []
sub(pattern,replacement)
click to toggle source
# File lib/clio/string.rb, line 130 def sub(pattern,replacement) dup.sub!(pattern, replacement) end
sub!(pattern,replacement)
click to toggle source
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
to_s()
click to toggle source
# 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
upcase()
click to toggle source
# File lib/clio/string.rb, line 45 def upcase ; self.class.new(text.upcase, marks) ; end
upcase!()
click to toggle source
# File lib/clio/string.rb, line 46 def upcase! ; text.upcase! ; end
yellow()
click to toggle source
# File lib/clio/string.rb, line 178 def yellow ; color(:yellow) ; end
yellow!()
click to toggle source
# File lib/clio/string.rb, line 186 def yellow! ; color!(:yellow) ; end
|(other)
click to toggle source
# File lib/clio/string.rb, line 67 def |(other) Split.new(self, other) end
Private Instance Methods
shift_marks(index, delta, marks=nil)
click to toggle source
# File lib/clio/string.rb, line 192 def shift_marks(index, delta, marks=nil) new_marks = {} (marks || @marks).each do |i, v| case i <=> index when -1 new_marks[i] = v when 0, 1 new_marks[i+delta] = v end end new_marks end
shift_marks!(index, delta)
click to toggle source
# File lib/clio/string.rb, line 206 def shift_marks!(index, delta) @marks.replace(shift_marks(index, delta)) end