Class: String

Inherits:
Object
  • Object
show all
Includes:
Doing::Color
Defined in:
lib/doing/string.rb,
lib/doing/string_chronify.rb,
lib/doing/template_string.rb

Overview

Chronify methods for strings

Direct Known Subclasses

Doing::TemplateString

Constant Summary

Constants included from Doing::Color

Doing::Color::ATTRIBUTES, Doing::Color::ATTRIBUTE_NAMES, Doing::Color::COLORED_REGEXP

Instance Method Summary collapse

Methods included from Doing::Color

attributes, coloring?, #support?

Instance Method Details

#add_atString

Add @ prefix to string if needed, maintains +/- prefix

Returns:

#add_tags(tags, remove: false) ⇒ String

Returns the tagged string.

Parameters:

  • tags (String or Array)

    List of tags to add. @ symbol optional

  • remove (Boolean) (defaults to: false)

    remove tags instead of adding

Returns:

  • (String)

    the tagged string

#add_tags!(tags, remove: false) ⇒ Object

See Also:

#cap_firstObject

Capitalize on the first character on string

Returns:

  • Capitalized string

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

Parameters:

  • options

    Additional options

Options Hash (**options):

  • :future (Boolean)

    assume future date (default: false)

  • :guess (Symbol)

    :begin or :end to assume beginning or end of arbitrary time range

Returns:

  • (DateTime)

    result

Raises:

  • (InvalidTimeExpression)

#chronify_qtyInteger

Converts simple strings into seconds that can be added to a Time object

Input string can be HH:MM or XX[dhm][XXhm][XXm]

Returns:

  • (Integer)

    seconds

#clean_unlinked_urlsObject

Clean up unlinked

#compressObject

Compress multiple spaces to single space

#compress!Object

#dedup_tagsObject

Remove duplicate tags, leaving only first occurrence

Returns:

  • Deduplicated string

#dedup_tags!Object

See Also:

#expand_date_tags(additional_tags = nil) ⇒ Object

Convert (chronify) natural language dates within configured date tags (tags whose value is expected to be a date). Modifies string in place.

Parameters:

  • additional_tags (Array) (defaults to: nil)

    An array of additional tags to consider date_tags

#highlight_tags(color = 'yellow', last_color: nil) ⇒ String

Colorize @tags with ANSI escapes

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)

Returns:

  • (String)

    string with @tags highlighted

#highlight_tags!(color = 'yellow', last_color: nil) ⇒ Object

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)

#ignore?Boolean

Test if line should be ignored

Returns:

  • (Boolean)

    line is empty or comment

#is_rx?Boolean

Determines if receiver is surrounded by slashes or starts with single quote

Returns:

  • (Boolean)

    True if regex, False otherwise.

#last_colorString

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.

Returns:

  • (String)

    All escape codes in string

Turn raw urls into HTML links

:html (default)

Parameters:

  • opt (Hash)

    Additional Options

Options Hash (**opt):

  • :format (Symbol)

    can be :markdown or

See Also:

#normalize_age(default = :newest) ⇒ Symbol

Convert an age string to a qualified type

Returns:

  • (Symbol)

    :oldest or :newest

#normalize_age!(default = :newest) ⇒ Object

See Also:

#normalize_bool(default = :and) ⇒ Object

Convert a boolean string to a symbol

Returns:

  • Symbol :and, :or, or :not

#normalize_bool!(default = :and) ⇒ Object

See Also:

#normalize_case(default = :smart) ⇒ Object

Convert a case sensitivity string to a symbol

Returns:

  • Symbol :smart, :sensitive, :ignore

#normalize_case!Object

See Also:

#normalize_matching(default = :pattern) ⇒ Object

Convert a matching configuration string to a symbol

Parameters:

  • default (Symbol) (defaults to: :pattern)

    the default matching type to return if the string doesn't match a known symbol

Returns:

  • Symbol :fuzzy, :pattern, :exact

#normalize_matching!(default = :pattern) ⇒ Object

#normalize_order(default = 'asc') ⇒ Object

#normalize_order!(default = 'asc') ⇒ String

Convert a sort order string to a qualified type

Returns:

  • (String)

    'asc' or 'desc'

#normalize_triggerString

Adds ?: to any parentheticals in a regular expression to avoid match groups

Returns:

  • (String)

    modified regular expression

#normalize_trigger!Object

See Also:

Remove formatting

#replace_qualified_urls(**options) ⇒ Object

Replace qualified urls

#set_type(kind = nil) ⇒ Object

Convert a string value to an appropriate type. If kind is not specified, '[one, two]' becomes an Array, '1' becomes Integer, '1.5' becomes Float, 'true' or 'yes' becomes TrueClass, 'false' or 'no' becomes FalseClass.

Parameters:

  • kind (String) (defaults to: nil)

    specify string, array, integer, float, symbol, or boolean (falls back to string if value is not recognized)

Returns:

  • Converted object type

#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

Parameters:

  • tag

    The tag

  • value (String) (defaults to: nil)

    Value for tag (@tag(value))

  • remove (Boolean) (defaults to: false)

    Remove the tag instead of adding

  • rename_to (String) (defaults to: nil)

    Replace tag with this tag

  • regex (Boolean) (defaults to: false)

    Tag is regular expression

  • single (Boolean) (defaults to: false)

    Operating on a single item (for logging)

  • force (Boolean) (defaults to: false)

    With rename_to, add tag if it doesn't exist

Returns:

  • (String)

    The string with modified tags

#tag!(tag, **options) ⇒ Object

Add, rename, or remove a tag in place

See Also:

#time_string(format: :dhm) ⇒ Object

Convert DD:HH:MM to a natural language string

Parameters:

  • format (Symbol) (defaults to: :dhm)

    The format to output (:dhm, :hm, :m, :clock, :natural)

#to_p(number) ⇒ Object

Pluralize a string based on quantity

Parameters:

  • number (Integer)

    the quantity of the object the string represents

#to_rx(distance: nil, case_type: nil) ⇒ Regexp

Convert string to fuzzy regex. Characters in words can be separated by up to distance characters in haystack, spaces indicate unlimited distance.

Examples:

"this word".to_rx(2) => /t.{0,3}h.{0,3}i.{0,3}s.{0,3}.*?w.{0,3}o.{0,3}r.{0,3}d/

Parameters:

  • distance (Integer) (defaults to: nil)

    Allowed distance between characters

  • case_type (defaults to: nil)

    The case type

Returns:

  • (Regexp)

    Regex pattern

#to_secondsInteger

Convert DD:HH:MM to seconds

Returns:

  • (Integer)

    rounded number of seconds

Raises:

  • (Errors::DoingRuntimeError)

#to_tagsArray

Convert a list of tags to an array. Tags can be with or without @ symbols, separated by any character, and can include parenthetical values (with spaces)

Returns:

  • (Array)

    array of tags including @ symbols

#truncate(len, ellipsis: '...') ⇒ Object

Truncate to nearest word

Parameters:

  • len

    The length

#truncate!(len, ellipsis: '...') ⇒ Object

#truncmiddle(len, ellipsis: '...') ⇒ Object

Truncate string in the middle

Parameters:

  • len

    The length

  • ellipsis (defaults to: '...')

    The ellipsis

#truncmiddle!(len, ellipsis: '...') ⇒ Object

#truthy?Boolean

Test string for truthiness (0, "f", "false", "n", "no" all return false, case insensitive, otherwise true)

Returns:

  • (Boolean)

    String is truthy

#uncolorObject

Remove color escape codes

Returns:

  • clean string

#uncolor!Object

#validate_colorString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract the longest valid color from a string.

Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.

Returns:

  • (String)

    a valid color name

#wildcard_to_rxString

Convert ? and * wildcards to regular expressions. Uses \S (non-whitespace) instead of . (any character)

Returns:

  • (String)

    Regular expression string

#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '', pad_first: false) ⇒ Object

Wrap string at word breaks, respecting tags

Parameters:

  • len (Integer)

    The length

  • offset (Integer) (defaults to: 0)

    (Optional) The width to pad each subsequent line

  • prefix (String) (defaults to: '')

    (Optional) A prefix to add to each line