Class Clio::String
In: lib/clio/string.rb
Parent: 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] }

Methods

+   []   ansi   ansi!   black   black!   blue   blue!   color   color!   cyan   cyan!   downcase   downcase!   green   green!   gsub   gsub!   lr   magenta   magenta!   new   red   red!   size   slice   sub   sub!   to_s   upcase   upcase!   yellow   yellow!   |  

Attributes

marks  [R] 
text  [R] 

Public Class methods

[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

[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
[](*args)

Alias for slice

[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

[Source]

# File lib/clio/string.rb, line 167
    def ansi!(code)
      marks[0] << ansicolor
      marks[size] << :clear
    end

[Source]

# File lib/clio/string.rb, line 176
    def black    ; color(:black)    ; end

[Source]

# File lib/clio/string.rb, line 184
    def black!   ; color!(:black)   ; end

[Source]

# File lib/clio/string.rb, line 175
    def blue     ; color(:blue)     ; end

[Source]

# File lib/clio/string.rb, line 183
    def blue!    ; color!(:blue)    ; end
color(code)

Alias for ansi

color!(code)

Alias for ansi!

[Source]

# File lib/clio/string.rb, line 179
    def cyan     ; color(:cyan)     ; end

[Source]

# File lib/clio/string.rb, line 187
    def cyan!    ; color!(:cyan)    ; end

[Source]

# File lib/clio/string.rb, line 49
    def downcase  ; self.class.new(text.upcase, marks) ; end

[Source]

# File lib/clio/string.rb, line 50
    def downcase! ; text.upcase! ; end

[Source]

# File lib/clio/string.rb, line 174
    def green    ; color(:green)    ; end

[Source]

# File lib/clio/string.rb, line 182
    def green!   ; color!(:green)   ; end

[Source]

# File lib/clio/string.rb, line 153
    def gsub(pattern_replacement)
      dup.gsub(pattern, replacement)
    end

[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

[Source]

# File lib/clio/string.rb, line 71
    def lr(other, options={})
      Split.new(self, other, options)
    end

[Source]

# File lib/clio/string.rb, line 177
    def magenta  ; color(:magenta)  ; end

[Source]

# File lib/clio/string.rb, line 185
    def magenta! ; color!(:magenta) ; end

[Source]

# File lib/clio/string.rb, line 173
    def red      ; color(:red)      ; end

[Source]

# File lib/clio/string.rb, line 181
    def red!     ; color!(:red)     ; end

[Source]

# File lib/clio/string.rb, line 42
    def size ; text.size ; end

slice

[Source]

# 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

[Source]

# 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.

[Source]

# 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

[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

[Source]

# File lib/clio/string.rb, line 45
    def upcase  ; self.class.new(text.upcase, marks) ; end

[Source]

# File lib/clio/string.rb, line 46
    def upcase! ; text.upcase! ; end

[Source]

# File lib/clio/string.rb, line 178
    def yellow   ; color(:yellow)   ; end

[Source]

# File lib/clio/string.rb, line 186
    def yellow!  ; color!(:yellow)  ; end

[Source]

# File lib/clio/string.rb, line 67
    def |(other)
      Split.new(self, other)
    end

[Validate]