Class: Clio::String

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#marksObject (readonly)

Returns the value of attribute marks.



24
25
26
# File 'lib/clio/string.rb', line 24

def marks
  @marks
end

#textObject (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



53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object Also known as: color



158
159
160
161
162
163
# 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

#ansi!(code) ⇒ Object Also known as: color!



167
168
169
170
# File 'lib/clio/string.rb', line 167

def ansi!(code)
  marks[0] << ansicolor
  marks[size] << :clear
end

#blackObject



176
# File 'lib/clio/string.rb', line 176

def black    ; color(:black)    ; end

#black!Object



184
# File 'lib/clio/string.rb', line 184

def black!   ; color!(:black)   ; end

#blueObject



175
# File 'lib/clio/string.rb', line 175

def blue     ; color(:blue)     ; end

#blue!Object



183
# File 'lib/clio/string.rb', line 183

def blue!    ; color!(:blue)    ; end

#cyanObject



179
# File 'lib/clio/string.rb', line 179

def cyan     ; color(:cyan)     ; end

#cyan!Object



187
# File 'lib/clio/string.rb', line 187

def cyan!    ; color!(:cyan)    ; end

#downcaseObject



49
# File 'lib/clio/string.rb', line 49

def downcase  ; self.class.new(text.upcase, marks) ; end

#downcase!Object



50
# File 'lib/clio/string.rb', line 50

def downcase! ; text.upcase! ; end

#greenObject



174
# File 'lib/clio/string.rb', line 174

def green    ; color(:green)    ; end

#green!Object



182
# File 'lib/clio/string.rb', line 182

def green!   ; color!(:green)   ; end

#gsub(pattern_replacement) ⇒ Object



153
154
155
# File 'lib/clio/string.rb', line 153

def gsub(pattern_replacement)
  dup.gsub(pattern, replacement)
end

#gsub!(pattern, replacement) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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 = {}) ⇒ Object



71
72
73
# File 'lib/clio/string.rb', line 71

def lr(other, options={})
  Split.new(self, other, options)
end

#magentaObject



177
# File 'lib/clio/string.rb', line 177

def magenta  ; color(:magenta)  ; end

#magenta!Object



185
# File 'lib/clio/string.rb', line 185

def magenta! ; color!(:magenta) ; end

#redObject



173
# File 'lib/clio/string.rb', line 173

def red      ; color(:red)      ; end

#red!Object



181
# File 'lib/clio/string.rb', line 181

def red!     ; color!(:red)     ; end

#sizeObject



42
# File 'lib/clio/string.rb', line 42

def size ; text.size ; end

#slice(*args) ⇒ Object Also known as: []

slice



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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

#sub(pattern, replacement) ⇒ Object



130
131
132
# File 'lib/clio/string.rb', line 130

def sub(pattern,replacement)
  dup.sub!(pattern, replacement)
end

#sub!(pattern, replacement) ⇒ Object

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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_sObject



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.reverse_each do |code|
      s.insert(index, ANSICode.__send__(code))
    end
  end
  s
end

#upcaseObject



45
# File 'lib/clio/string.rb', line 45

def upcase  ; self.class.new(text.upcase, marks) ; end

#upcase!Object



46
# File 'lib/clio/string.rb', line 46

def upcase! ; text.upcase! ; end

#yellowObject



178
# File 'lib/clio/string.rb', line 178

def yellow   ; color(:yellow)   ; end

#yellow!Object



186
# File 'lib/clio/string.rb', line 186

def yellow!  ; color!(:yellow)  ; end

#|(other) ⇒ Object



67
68
69
# File 'lib/clio/string.rb', line 67

def |(other)
  Split.new(self, other)
end