Class: String
- Inherits:
-
Object
- Object
- String
- Includes:
- Doing::Color
- Defined in:
- lib/doing/string.rb,
lib/doing/string_chronify.rb
Overview
Chronify methods for strings
Constant Summary
Constants included from Doing::Color
Doing::Color::ATTRIBUTES, Doing::Color::ATTRIBUTE_NAMES, Doing::Color::COLORED_REGEXP
Instance Method Summary collapse
- #add_tags(tags, remove: false) ⇒ Object
- #add_tags!(tags, remove: false) ⇒ Object
-
#cap_first ⇒ Object
Capitalize on the first character on string.
-
#chronify(**options) ⇒ DateTime
Converts input string into a Time object when input takes on the following formats: - interval format e.g.
-
#chronify_qty ⇒ Integer
Converts simple strings into seconds that can be added to a Time object.
-
#clean_unlinked_urls ⇒ Object
Clean up unlinked
. -
#compress ⇒ Object
Compress multiple spaces to single space.
- #compress! ⇒ Object
- #dedup_tags ⇒ Object
-
#dedup_tags! ⇒ Object
Remove duplicate tags, leaving only first occurrence.
-
#highlight_tags(color = 'yellow') ⇒ String
Colorize @tags with ANSI escapes.
- #highlight_tags!(color = 'yellow') ⇒ Object
-
#ignore? ⇒ Boolean
Test if line should be ignored.
-
#is_rx? ⇒ Boolean
Determines if receiver is surrounded by slashes or starts with single quote.
-
#last_color ⇒ String
Returns the last escape sequence from a string.
- #link_urls(**opt) ⇒ Object
-
#link_urls!(**opt) ⇒ Object
Turn raw urls into HTML links.
- #normalize_bool(default = :and) ⇒ Object
-
#normalize_bool!(default = :and) ⇒ Object
Convert a boolean string to a symbol.
- #normalize_case(default = :smart) ⇒ Object
-
#normalize_case! ⇒ Object
Convert a case sensitivity string to a symbol.
- #normalize_order(default = 'asc') ⇒ Object
-
#normalize_order!(default = 'asc') ⇒ String
Convert a sort order string to a qualified type.
- #normalize_trigger ⇒ Object
- #normalize_trigger! ⇒ Object
-
#remove_self_links ⇒ Object
Remove
formatting. -
#replace_qualified_urls(**options) ⇒ Object
Replace qualified urls.
- #set_type(kind = nil) ⇒ Object
- #simple_wrap(width) ⇒ Object
-
#tag(tag, value: nil, remove: false, rename_to: nil, regex: false, single: false, force: false) ⇒ String
Add, rename, or remove a tag.
-
#tag!(tag, **options) ⇒ Object
Add, rename, or remove a tag in place.
-
#to_rx(distance: 3, case_type: :smart) ⇒ Regexp
Convert string to fuzzy regex.
- #to_tags ⇒ Object
-
#truncate(len, ellipsis: '...') ⇒ Object
Truncate to nearest word.
- #truncate!(len, ellipsis: '...') ⇒ Object
-
#truncmiddle(len, ellipsis: '...') ⇒ Object
Truncate string in the middle.
- #truncmiddle!(len, ellipsis: '...') ⇒ Object
-
#truthy? ⇒ Boolean
Test string for truthiness (0, "f", "false", "n", "no" all return false, case insensitive, otherwise true).
-
#uncolor ⇒ Object
Remove color escape codes.
- #uncolor! ⇒ Object
-
#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '') ⇒ Object
Wrap string at word breaks, respecting tags.
Methods included from Doing::Color
attributes, coloring?, #support?
Instance Method Details
#add_tags(tags, remove: false) ⇒ Object
323 324 325 326 327 328 |
# File 'lib/doing/string.rb', line 323 def (, remove: false) title = self.dup = . .each { |tag| title.tag!(tag, remove: remove) } title end |
#add_tags!(tags, remove: false) ⇒ Object
319 320 321 |
# File 'lib/doing/string.rb', line 319 def (, remove: false) replace (, remove: remove) end |
#cap_first ⇒ Object
Capitalize on the first character on string
237 238 239 240 241 |
# File 'lib/doing/string.rb', line 237 def cap_first sub(/^\w/) do |m| m.upcase end end |
#chronify(**options) ⇒ DateTime
Converts input string into a Time object when input takes on the following formats: - interval format e.g. '1d2h30m', '45m' etc. - a semantic phrase e.g. 'yesterday 5:30pm' - a strftime e.g. '2016-03-15 15:32:04 PDT'
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/doing/string_chronify.rb', line 27 def chronify(**) now = Time.now raise InvalidTimeExpression, "Invalid time expression #{inspect}" if to_s.strip == '' secs_ago = if match(/^(\d+)$/) # plain number, assume minutes Regexp.last_match(1).to_i * 60 elsif (m = match(/^(?:(?<day>\d+)d)?(?:(?<hour>\d+)h)?(?:(?<min>\d+)m)?$/i)) # day/hour/minute format e.g. 1d2h30m [[m['day'], 24 * 3600], [m['hour'], 3600], [m['min'], 60]].map { |qty, secs| qty ? (qty.to_i * secs) : 0 }.reduce(0, :+) end if secs_ago now - secs_ago else Chronic.parse(self, { guess: .fetch(:guess, :begin), context: .fetch(:future, false) ? :future : :past, ambiguous_time_range: 8 }) end end |
#chronify_qty ⇒ Integer
Converts simple strings into seconds that can be added to a Time object
Input string can be HH:MM or XX[[XXhm][XXm]] (1d2h30m, 45m, 1.5d, 1h20m, etc.)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/doing/string_chronify.rb', line 57 def chronify_qty minutes = 0 case self.strip when /^(\d+):(\d\d)$/ minutes += Regexp.last_match(1).to_i * 60 minutes += Regexp.last_match(2).to_i when /^(\d+(?:\.\d+)?)([hmd])?$/ amt = Regexp.last_match(1) type = Regexp.last_match(2).nil? ? 'm' : Regexp.last_match(2) minutes = case type.downcase when 'm' amt.to_i when 'h' (amt.to_f * 60).round when 'd' (amt.to_f * 60 * 24).round else minutes end end minutes * 60 end |
#clean_unlinked_urls ⇒ Object
Clean up unlinked
507 508 509 510 511 512 513 514 515 516 |
# File 'lib/doing/string.rb', line 507 def clean_unlinked_urls gsub(/<(\w+:.*?)>/) do |match| m = Regexp.last_match if m[1] =~ /<a href/ match else %(<a href="#{m[1]}" title="Link to #{m[1]}">[link]</a>) end end end |
#compress ⇒ Object
Compress multiple spaces to single space
66 67 68 |
# File 'lib/doing/string.rb', line 66 def compress gsub(/ +/, ' ').strip end |
#compress! ⇒ Object
70 71 72 |
# File 'lib/doing/string.rb', line 70 def compress! replace compress end |
#dedup_tags ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/doing/string.rb', line 416 def title = dup = title.scan(/(?<=\A| )(@(\S+?)(\([^)]+\))?)(?= |\Z)/).uniq .each do |tag| found = false title.gsub!(/( |^)#{tag[1]}(\([^)]+\))?(?= |$)/) do |m| if found '' else found = true m end end end title end |
#dedup_tags! ⇒ Object
Remove duplicate tags, leaving only first occurrence
412 413 414 |
# File 'lib/doing/string.rb', line 412 def replace end |
#highlight_tags(color = 'yellow') ⇒ String
Colorize @tags with ANSI escapes
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/doing/string.rb', line 86 def (color = 'yellow') escapes = scan(/(\e\[[\d;]+m)[^\e]+@/) color = color.split(' ') unless color.is_a?(Array) tag_color = '' color.each { |c| tag_color += Doing::Color.send(c) } last_color = if !escapes.empty? escapes[-1][0] else Doing::Color.default end gsub(/(\s|m)(@[^ ("']+)/, "\\1#{tag_color}\\2#{Doing::Color.reset}#{last_color}") end |
#highlight_tags!(color = 'yellow') ⇒ Object
75 76 77 |
# File 'lib/doing/string.rb', line 75 def (color = 'yellow') replace (color) end |
#ignore? ⇒ Boolean
Test if line should be ignored
104 105 106 107 |
# File 'lib/doing/string.rb', line 104 def ignore? line = self line =~ /^#/ || line =~ /^\s*$/ end |
#is_rx? ⇒ Boolean
Determines if receiver is surrounded by slashes or starts with single quote
14 15 16 |
# File 'lib/doing/string.rb', line 14 def is_rx? self =~ %r{(^/.*?/$|^')} end |
#last_color ⇒ String
Returns the last escape sequence from a string.
Actually returns all escape codes, with the assumption that the result of inserting them will generate the same color as was set at the end of the string. Because you can send modifiers like dark and bold separate from color codes, only using the last code may not render the same style.
444 445 446 |
# File 'lib/doing/string.rb', line 444 def last_color scan(/\e\[[\d;]+m/).join('') end |
#link_urls(**opt) ⇒ Object
458 459 460 461 462 463 464 465 466 467 |
# File 'lib/doing/string.rb', line 458 def link_urls(**opt) fmt = opt.fetch(:format, :html) return self unless fmt str = dup str = str.remove_self_links if fmt == :markdown str.replace_qualified_urls(format: fmt).clean_unlinked_urls end |
#link_urls!(**opt) ⇒ Object
Turn raw urls into HTML links
453 454 455 456 |
# File 'lib/doing/string.rb', line 453 def link_urls!(**opt) fmt = opt.fetch(:format, :html) replace link_urls(format: fmt) end |
#normalize_bool(default = :and) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/doing/string.rb', line 294 def normalize_bool(default = :and) case self when /(and|all)/i :and when /(any|or)/i :or when /(not|none)/i :not else default.is_a?(Symbol) ? default : default.normalize_bool end end |
#normalize_bool!(default = :and) ⇒ Object
Convert a boolean string to a symbol
290 291 292 |
# File 'lib/doing/string.rb', line 290 def normalize_bool!(default = :and) replace normalize_bool(default) end |
#normalize_case(default = :smart) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/doing/string.rb', line 272 def normalize_case(default = :smart) case self when /^c/i :sensitive when /^i/i :ignore when /^s/i :smart else default.is_a?(Symbol) ? default : default.normalize_case end end |
#normalize_case! ⇒ Object
Convert a case sensitivity string to a symbol
268 269 270 |
# File 'lib/doing/string.rb', line 268 def normalize_case! replace normalize_case end |
#normalize_order(default = 'asc') ⇒ Object
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/doing/string.rb', line 252 def normalize_order(default = 'asc') case self when /^a/i 'asc' when /^d/i 'desc' else default end end |
#normalize_order!(default = 'asc') ⇒ String
Convert a sort order string to a qualified type
248 249 250 |
# File 'lib/doing/string.rb', line 248 def normalize_order!(default = 'asc') replace normalize_order(default) end |
#normalize_trigger ⇒ Object
311 312 313 |
# File 'lib/doing/string.rb', line 311 def normalize_trigger gsub(/\((?!\?:)/, '(?:').downcase end |
#normalize_trigger! ⇒ Object
307 308 309 |
# File 'lib/doing/string.rb', line 307 def normalize_trigger! replace normalize_trigger end |
#remove_self_links ⇒ Object
Remove
470 471 472 473 474 475 476 477 478 479 |
# File 'lib/doing/string.rb', line 470 def remove_self_links gsub(/<(.*?)>/) do |match| m = Regexp.last_match if m[1] =~ /^https?:/ m[1] else match end end end |
#replace_qualified_urls(**options) ⇒ Object
Replace qualified urls
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/doing/string.rb', line 482 def replace_qualified_urls(**) fmt = .fetch(:format, :html) gsub(%r{(?mi)(?x: (?<!["'\[(\\]) (?<protocol>(?:http|https)://) (?<domain>[\w\-]+(?:\.[\w\-]+)+) (?<path>[\w\-.,@?^=%&;:/~+#]*[\w\-@^=%&;/~+#])? )}) do |_match| m = Regexp.last_match url = "#{m['domain']}#{m['path']}" proto = m['protocol'].nil? ? 'http://' : m['protocol'] case fmt when :terminal TTY::Link.link_to("#{proto}#{url}", "#{proto}#{url}") when :html %(<a href="#{proto}#{url}" title="Link to #{m['domain']}">[#{url}]</a>) when :markdown "[#{url}](#{proto}#{url})" else m[0] end end end |
#set_type(kind = nil) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/doing/string.rb', line 518 def set_type(kind = nil) if kind case kind.to_s when /^a/i gsub(/^\[ *| *\]$/, '').split(/ *, */) when /^i/i to_i when /^f/i to_f when /^sy/i sub(/^:/, '').to_sym when /^b/i self =~ /^(true|yes)$/ ? true : false else to_s end else case self when / *, */ gsub(/^\[ *| *\]$/, '').split(/ *, */) when /^[0-9]+$/ to_i when /^[0-9]+\.[0-9]+$/ to_f when /^:\w+/ sub(/^:/, '').to_sym when /^(true|yes)$/i true when /^(false|no)$/i false else to_s end end end |
#simple_wrap(width) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/doing/string.rb', line 165 def simple_wrap(width) str = gsub(/@\S+\(.*?\)/) { |tag| tag.gsub(/\s/, '%%%%') } words = str.split(/ /).map { |word| word.gsub(/%%%%/, ' ') } out = [] line = [] words.each do |word| if word.uncolor.length >= width chars = word.uncolor.split('') out << chars.slice!(0, width - 1).join('') while chars.count >= width line << chars.join('') next elsif line.join(' ').uncolor.length + word.uncolor.length + 1 > width out.push(line.join(' ')) line.clear end line << word.uncolor end out.push(line.join(' ')) out.join("\n") end |
#tag(tag, value: nil, remove: false, rename_to: nil, regex: false, single: false, force: false) ⇒ String
Add, rename, or remove a tag
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/doing/string.rb', line 352 def tag(tag, value: nil, remove: false, rename_to: nil, regex: false, single: false, force: false) log_level = single ? :info : :debug title = dup title.chomp! tag = tag.sub(/^@?/, '') case_sensitive = tag !~ /[A-Z]/ rx_tag = if regex tag.gsub(/\./, '\S') else tag.gsub(/\?/, '.').gsub(/\*/, '\S*?') end if remove || rename_to rx = Regexp.new("(?<=^| )@#{rx_tag}(?<parens>\\((?<value>[^)]*)\\))?(?= |$)", case_sensitive) m = title.match(rx) if m.nil? && rename_to && force title.tag!(rename_to, value: value, single: single) elsif m title.gsub!(rx) do rename_to ? "@#{rename_to}#{value.nil? ? m['parens'] : "(#{value})"}" : '' end title. title.chomp! if rename_to f = "@#{tag}".cyan t = "@#{rename_to}".cyan Doing.logger.write(log_level, 'Tag:', %(renamed #{f} to #{t} in "#{title}")) else f = "@#{tag}".cyan Doing.logger.write(log_level, 'Tag:', %(removed #{f} from "#{title}")) end else Doing.logger.debug('Skipped:', "not tagged #{"@#{tag}".cyan}") end elsif title =~ /@#{tag}(?=[ (]|$)/ Doing.logger.debug('Skipped:', "already tagged #{"@#{tag}".cyan}") return title else add = tag add += "(#{value})" unless value.nil? title.chomp! title += " @#{add}" title. title.chomp! Doing.logger.write(log_level, 'Tag:', %(added #{('@' + tag).cyan} to "#{title}")) end title.gsub(/ +/, ' ') end |
#tag!(tag, **options) ⇒ Object
Add, rename, or remove a tag in place
335 336 337 |
# File 'lib/doing/string.rb', line 335 def tag!(tag, **) replace tag(tag, **) end |
#to_rx(distance: 3, case_type: :smart) ⇒ Regexp
Convert string to fuzzy regex. Characters in words can be separated by up to distance characters in haystack, spaces indicate unlimited distance.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/doing/string.rb', line 31 def to_rx(distance: 3, case_type: :smart) case_sensitive = case case_type when :smart self =~ /[A-Z]/ ? true : false when :sensitive true else false end pattern = case dup.strip when %r{^/.*?/$} sub(%r{/(.*?)/}, '\1') when /^'/ sub(/^'(.*?)'?$/, '\1') else split(/ +/).map { |w| w.split('').join(".{0,#{distance}}") }.join('.*?') end Regexp.new(pattern, !case_sensitive) end |
#to_tags ⇒ Object
315 316 317 |
# File 'lib/doing/string.rb', line 315 def gsub(/ *, */, ' ').gsub(/ +/, ' ').split(/ /).sort.uniq.map { |t| t.strip.sub(/^@/, '') } end |
#truncate(len, ellipsis: '...') ⇒ Object
Truncate to nearest word
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/doing/string.rb', line 114 def truncate(len, ellipsis: '...') return self if length <= len total = 0 res = [] split(/ /).each do |word| break if total + 1 + word.length > len total += 1 + word.length res.push(word) end res.join(' ') + ellipsis end |
#truncate!(len, ellipsis: '...') ⇒ Object
129 130 131 |
# File 'lib/doing/string.rb', line 129 def truncate!(len, ellipsis: '...') replace truncate(len, ellipsis: ellipsis) end |
#truncmiddle(len, ellipsis: '...') ⇒ Object
Truncate string in the middle
139 140 141 142 143 144 145 146 |
# File 'lib/doing/string.rb', line 139 def truncmiddle(len, ellipsis: '...') return self if length <= len len -= (ellipsis.length / 2).to_i total = length half = total / 2 cut = (total - len) / 2 sub(/(.{#{half - cut}}).*?(.{#{half - cut}})$/, "\\1#{ellipsis}\\2") end |
#truncmiddle!(len, ellipsis: '...') ⇒ Object
148 149 150 |
# File 'lib/doing/string.rb', line 148 def truncmiddle!(len, ellipsis: '...') replace truncmiddle(len, ellipsis: ellipsis) end |
#truthy? ⇒ Boolean
Test string for truthiness (0, "f", "false", "n", "no" all return false, case insensitive, otherwise true)
57 58 59 60 61 62 63 |
# File 'lib/doing/string.rb', line 57 def truthy? if self =~ /^(0|f(alse)?|n(o)?)$/i false else true end end |
#uncolor ⇒ Object
Remove color escape codes
157 158 159 |
# File 'lib/doing/string.rb', line 157 def uncolor gsub(/\e\[[\d;]+m/,'') end |
#uncolor! ⇒ Object
161 162 163 |
# File 'lib/doing/string.rb', line 161 def uncolor! replace uncolor end |
#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '') ⇒ Object
Wrap string at word breaks, respecting tags
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/doing/string.rb', line 195 def wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '') last_color = color.empty? ? '' : after.last_color note_rx = /(?i-m)(%(?:[io]d|(?:\^[\s\S])?(?:(?:[ _t]|[^a-z0-9])?\d+)?(?:[\s\S][ _t]?)?)?note)/ # Don't break inside of tag values str = gsub(/@\S+\(.*?\)/) { |tag| tag.gsub(/\s/, '%%%%') } words = str.split(/ /).map { |word| word.gsub(/%%%%/, ' ') } out = [] line = [] words.each do |word| if word.uncolor.length >= len chars = word.uncolor.split('') out << chars.slice!(0, len - 1).join('') while chars.count >= len line << chars.join('') next elsif line.join(' ').uncolor.length + word.uncolor.length + 1 > len out.push(line.join(' ')) line.clear end line << word.uncolor end out.push(line.join(' ')) note = '' after = after.dup if after.frozen? after.sub!(note_rx) do note = Regexp.last_match(0) '' end out[0] = format("%-#{pad}s%s%s", out[0], last_color, after) left_pad = ' ' * offset left_pad += indent out.map { |l| "#{left_pad}#{color}#{l}#{last_color}" }.join("\n").strip + last_color + " #{note}".chomp end |