Class: ANSIString
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#without_ansi ⇒ Object
readonly
Returns the value of attribute without_ansi.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<<(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #[](range) ⇒ Object
- #[]=(range, replacement_str) ⇒ Object
- #dup ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(str) ⇒ ANSIString
constructor
A new instance of ANSIString.
- #insert(position, string) ⇒ Object
- #inspect ⇒ Object
- #length ⇒ Object
- #lines ⇒ Object
- #replace(str) ⇒ Object
- #reverse ⇒ Object
-
#rindex(*args) ⇒ Object
See String#rindex for arguments.
- #scan(pattern) ⇒ Object
- #slice(index, length = nil) ⇒ Object
- #split(*args) ⇒ Object
- #strip ⇒ Object
- #sub(pattern, replacement) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(str) ⇒ ANSIString
Returns a new instance of ANSIString.
12 13 14 |
# File 'lib/ansi_string.rb', line 12 def initialize(str) process_string raw_string_for(str) end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
7 8 9 |
# File 'lib/ansi_string.rb', line 7 def raw @raw end |
#without_ansi ⇒ Object (readonly)
Returns the value of attribute without_ansi.
7 8 9 |
# File 'lib/ansi_string.rb', line 7 def without_ansi @without_ansi end |
Instance Method Details
#+(other) ⇒ Object
16 17 18 |
# File 'lib/ansi_string.rb', line 16 def +(other) self.class.new @raw + raw_string_for(other) end |
#<<(other) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ansi_string.rb', line 20 def <<(other) range = length..length str = replace_in_string(range, other) process_string raw_string_for(str) self end |
#<=>(other) ⇒ Object
219 220 221 |
# File 'lib/ansi_string.rb', line 219 def <=>(other) (other.class == self.class && @raw <=> other.raw) end |
#==(other) ⇒ Object
215 216 217 |
# File 'lib/ansi_string.rb', line 215 def ==(other) (other.class == self.class && other.raw == @raw) || (other.kind_of?(String) && other == @raw) end |
#[](range) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ansi_string.rb', line 39 def [](range) # convert numeric position to a range range = (range..range) if range.is_a?(Integer) range_begin = range.begin range_end = range.end if range.exclude_end? if range_begin == 0 && range_end == 0 return "" else range_end -= 1 end end range_begin = @without_ansi.length - range.begin.abs if range.begin < 0 range_end = @without_ansi.length - range.end.abs if range.end < 0 str = build_string_with_ansi_for(range_begin..range_end) ANSIString.new str if str end |
#[]=(range, replacement_str) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ansi_string.rb', line 61 def []=(range, replacement_str) # convert numeric position to a range range = (range..range) if range.is_a?(Integer) range_begin = range.begin range_end = range.exclude_end? ? range.end - 1 : range.end range_begin = @without_ansi.length - range.begin.abs if range.begin < 0 range_end = @without_ansi.length - range.end.abs if range.end < 0 updated_string = replace_in_string(range_begin..range_end, replacement_str) process_string raw_string_for(updated_string) self end |
#dup ⇒ Object
184 185 186 |
# File 'lib/ansi_string.rb', line 184 def dup ANSIString.new(@raw.dup) end |
#empty? ⇒ Boolean
35 36 37 |
# File 'lib/ansi_string.rb', line 35 def empty? length == 0 end |
#insert(position, string) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ansi_string.rb', line 27 def insert(position, string) if position < 0 position = @without_ansi.length + position + 1 end self[position...position] = string self end |
#inspect ⇒ Object
211 212 213 |
# File 'lib/ansi_string.rb', line 211 def inspect to_s.inspect end |
#length ⇒ Object
140 141 142 |
# File 'lib/ansi_string.rb', line 140 def length @without_ansi.length end |
#lines ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ansi_string.rb', line 144 def lines result = [] current_string = "" @ansi_sequence_locations.map do |location| if location[:text] == "\n" result << ANSIString.new(current_string + "\n") current_string = "" next end location[:text].scan(/.*(?:\n|$)/).each_with_index do |line, i| break if line == "" if i == 0 current_string << [ location[:start_ansi_sequence], line, location[:end_ansi_sequence] ].join else result << ANSIString.new(current_string) current_string = "" current_string << [ location[:start_ansi_sequence], line, location[:end_ansi_sequence] ].join end end if location[:text].end_with?("\n") result << ANSIString.new(current_string) current_string = "" next end end result << ANSIString.new(current_string) if current_string.length > 0 result end |
#replace(str) ⇒ Object
81 82 83 84 |
# File 'lib/ansi_string.rb', line 81 def replace(str) process_string raw_string_for(str) self end |
#reverse ⇒ Object
86 87 88 89 90 91 |
# File 'lib/ansi_string.rb', line 86 def reverse str = @ansi_sequence_locations.reverse.map do |location| [location[:start_ansi_sequence], location[:text].reverse, location[:end_ansi_sequence]].join end.join ANSIString.new str end |
#rindex(*args) ⇒ Object
See String#rindex for arguments
77 78 79 |
# File 'lib/ansi_string.rb', line 77 def rindex(*args) @without_ansi.rindex(*args) end |
#scan(pattern) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ansi_string.rb', line 93 def scan(pattern) results = [] without_ansi.enum_for(:scan, pattern).each do md = Regexp.last_match if md.captures.any? results << md.captures.map.with_index do |_, i| # captures use 1-based indexing self[md.begin(i+1)..md.end(i+1)-1] end else results << self[md.begin(0)..md.end(0)-1] end end results end |
#slice(index, length = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ansi_string.rb', line 109 def slice(index, length=nil) return ANSIString.new("") if length == 0 range = nil index = index.without_ansi if index.is_a?(ANSIString) index = Regexp.new Regexp.escape(index) if index.is_a?(String) if index.is_a?(Integer) length ||= 1 range = (index..index+length-1) elsif index.is_a?(Range) range = index elsif index.is_a?(Regexp) md = @without_ansi.match(index) capture_group_index = length || 0 if md capture_group = md.offset(capture_group_index) range = (capture_group.first..capture_group.last-1) end else raise(ArgumentError, "Must pass in at least an index or a range.") end self[range] if range end |
#split(*args) ⇒ Object
132 133 134 |
# File 'lib/ansi_string.rb', line 132 def split(*args) raw.split(*args).map { |s| ANSIString.new(s) } end |
#strip ⇒ Object
136 137 138 |
# File 'lib/ansi_string.rb', line 136 def strip ANSIString.new raw.strip end |
#sub(pattern, replacement) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ansi_string.rb', line 188 def sub(pattern, replacement) str = "" count = 0 max_count = 1 index = 0 @without_ansi.enum_for(:scan, pattern).each do md = Regexp.last_match str << build_string_with_ansi_for(index...(index + md.begin(0))) index = md.end(0) break if (count += 1) == max_count end if index != @without_ansi.length str << build_string_with_ansi_for(index..@without_ansi.length) end nstr = str.gsub /(\033\[[0-9;]*m)(.+?)\033\[0m\1/, '\1\2' ANSIString.new(nstr) end |
#to_s ⇒ Object Also known as: to_str
206 207 208 |
# File 'lib/ansi_string.rb', line 206 def to_s @raw.dup end |