Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/na/colors.rb,
lib/na/string.rb

Overview

String helpers

Instance Method Summary collapse

Instance Method Details

#actionObject



66
67
68
# File 'lib/na/string.rb', line 66

def action
  sub(/^[ \t]*- /, '')
end

#action?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/na/string.rb', line 49

def action?
  self =~ /^[ \t]*- /
end

#blank?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/na/string.rb', line 53

def blank?
  strip =~ /^$/
end

#cap_firstString

Capitalize first character, leaving other capitalization in place

Returns:

  • (String)

    capitalized string



187
188
189
190
191
192
# File 'lib/na/string.rb', line 187

def cap_first
  sub(/^([a-z])(.*)$/) do
    m = Regexp.last_match
    m[1].upcase << m[2]
  end
end

#cap_first!Object



177
178
179
# File 'lib/na/string.rb', line 177

def cap_first!
  replace cap_first
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'

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:

  • (StandardError)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/na/string.rb', line 268

def chronify(**options)
  now = Time.now
  raise StandardError, "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
    res = now - secs_ago
    notify(%(date/time string "#{self}" interpreted as #{res} (#{secs_ago} seconds ago)), debug: true)
  else
    date_string = dup
    date_string = 'today' if date_string.match(REGEX_DAY) && now.strftime('%a') =~ /^#{Regexp.last_match(1)}/i
    date_string = "#{options[:context].to_s} #{date_string}" if date_string =~ REGEX_TIME && options[:context]

    res = Chronic.parse(date_string, {
                          guess: options.fetch(:guess, :begin),
                          context: options.fetch(:future, false) ? :future : :past,
                          ambiguous_time_range: 8
                        })

    NA.notify(%(date/time string "#{self}" interpreted as #{res}), debug: true)
  end

  res
end

#dir_matches(any: [], all: [], none: [], require_last: true, distance: 1) ⇒ Object



157
158
159
160
161
162
# File 'lib/na/string.rb', line 157

def dir_matches(any: [], all: [], none: [], require_last: true, distance: 1)
  any_rx = any.map { |q| q.dir_to_rx(distance: distance, require_last: require_last) }
  all_rx = all.map { |q| q.dir_to_rx(distance: distance, require_last: require_last) }
  none_rx = none.map { |q| q.dir_to_rx(distance: distance, require_last: false) }
  matches_any(any_rx) && matches_all(all_rx) && matches_none(none_rx)
end

#dir_to_rx(distance: 1, require_last: true) ⇒ Object

Note:

Splits at / or :, adds variable distance between characters, joins segments with slashes and requires that last segment match last segment of target path

Convert a directory path to a regular expression

Parameters:

  • distance (defaults to: 1)

    The distance allowed between characters

  • require_last (defaults to: true)

    Require match to be last element in path



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

def dir_to_rx(distance: 1, require_last: true)
  "#{split(%r{[/:]}).map { |comp| comp.split('').join(".{0,#{distance}}").gsub(/\*/, '[^ ]*?') }.join('.*?/.*?')}#{require_last ? '[^/]*?$' : ''}"
end

#done?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/na/string.rb', line 70

def done?
  self =~ /@done/
end

#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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/na/string.rb', line 212

def expand_date_tags(additional_tags = nil)
  iso_rx = /\d{4}-\d\d-\d\d \d\d:\d\d/

  watch_tags = [
    'due',
    'start(?:ed)?',
    'beg[ia]n',
    'done',
    'finished',
    'completed?',
    'waiting',
    'defer(?:red)?'
  ]

  if additional_tags
    date_tags = additional_tags
    date_tags = date_tags.split(/ *, */) if date_tags.is_a?(String)
    date_tags.map! do |tag|
      tag.sub(/^@/, '').gsub(/\((?!\?:)(.*?)\)/, '(?:\1)').strip
    end
    watch_tags.concat(date_tags).uniq!
  end

  done_rx = /(?<=^| )@(?<tag>#{watch_tags.join('|')})\((?<date>.*?)\)/i

  gsub!(done_rx) do
    m = Regexp.last_match
    t = m['tag']
    d = m['date']
    future = t =~ /^(done|complete)/ ? false : true
    parsed_date = d =~ iso_rx ? Time.parse(d) : d.chronify(guess: :begin, future: future)
    parsed_date.nil? ? m[0] : "@#{t}(#{parsed_date.strftime('%F %R')})"
  end
end

#good?Boolean

Tests if object is nil or empty

Returns:

  • (Boolean)

    true if object is defined and has content



15
16
17
# File 'lib/na/string.rb', line 15

def good?
  !strip.empty?
end

#highlight_search(regexes, color: '{y}', last_color: '{xg}') ⇒ Object

Highlight search results

Parameters:

  • regexes (Array)

    The regexes for the search

  • color (String) (defaults to: '{y}')

    The highlight color template

  • last_color (String) (defaults to: '{xg}')

    Color to restore after highlight



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/na/string.rb', line 109

def highlight_search(regexes, color: '{y}', last_color: '{xg}')
  string = dup
  color = NA::Color.template(color)
  regexes.each do |rx|
    next if rx.nil?

    rx = Regexp.new(rx.wildcard_to_rx, Regexp::IGNORECASE) if rx.is_a?(String)

    string.gsub!(rx) do
      m = Regexp.last_match
      last = m.pre_match.last_color
      "#{color}#{m[0]}#{NA::Color.template(last)}"
    end
  end
  string
end

#highlight_tags(color: '{m}', value: '{y}', parens: '{m}', last_color: '{xg}') ⇒ String

Colorize @tags with ANSI escapes

Parameters:

  • color (String) (defaults to: '{m}')

    color (see #Color)

  • value (String) (defaults to: '{y}')

    The value color template

  • parens (String) (defaults to: '{m}')

    The parens color template

  • last_color (String) (defaults to: '{xg}')

    Color to restore after tag highlight

Returns:

  • (String)

    string with @tags highlighted



91
92
93
94
95
96
97
# File 'lib/na/string.rb', line 91

def highlight_tags(color: '{m}', value: '{y}', parens: '{m}', last_color: '{xg}')
  tag_color = NA::Color.template(color)
  paren_color = NA::Color.template(parens)
  value_color = NA::Color.template(value)
  gsub(/(\s|m)(@[^ ("']+)(?:(\()(.*?)(\)))?/,
       "\\1#{tag_color}\\2#{paren_color}\\3#{value_color}\\4#{paren_color}\\5#{last_color}")
end

#ignore?Boolean

Test if line should be ignored

Returns:

  • (Boolean)

    line is empty or comment



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

def ignore?
  line = self
  line =~ /^#/ || line.strip.empty?
end

#indent_levelNumber

Determine indentation level of line

Returns:

  • (Number)

    number of indents detected



42
43
44
45
46
47
# File 'lib/na/string.rb', line 42

def indent_level
  prefix = match(/(^[ \t]+)/)
  return 0 if prefix.nil?

  prefix[1].gsub(/  /, "\t").scan(/\t/).count
end

#last_colorString

Note:

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 the last escape sequence from a string.

Returns:

  • (String)

    All escape codes in string



138
139
140
# File 'lib/na/string.rb', line 138

def last_color
  scan(/\e\[[\d;]+m/).join('').gsub(/\e\[0m/, '')
end

#last_color_codeObject

Get the calculated ANSI color at the end of the string

Returns:

  • ANSI escape sequence to match color



138
139
140
141
142
143
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
183
184
185
186
187
# File 'lib/na/colors.rb', line 138

def last_color_code
  m = scan(ESCAPE_REGEX)

  em = ['0']
  fg = nil
  bg = nil
  rgbf = nil
  rgbb = nil

  m.each do |c|
    case c
    when '0'
      em = ['0']
      fg, bg, rgbf, rgbb = nil
    when /^[34]8/
      case c
      when /^3/
        fg = nil
        rgbf = c
      when /^4/
        bg = nil
        rgbb = c
      end
    else
      c.split(/;/).each do |i|
        x = i.to_i
        if x <= 9
          em << x
        elsif x >= 30 && x <= 39
          rgbf = nil
          fg = x
        elsif x >= 40 && x <= 49
          rgbb = nil
          bg = x
        elsif x >= 90 && x <= 97
          rgbf = nil
          fg = x
        elsif x >= 100 && x <= 107
          rgbb = nil
          bg = x
        end
      end
    end
  end

  escape = "\e[#{em.join(';')}m"
  escape += "\e[#{rgbb}m" if rgbb
  escape += "\e[#{rgbf}m" if rgbf
  escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
end

#matches(any: [], all: [], none: []) ⇒ Object



164
165
166
# File 'lib/na/string.rb', line 164

def matches(any: [], all: [], none: [])
  matches_any(any) && matches_all(all) && matches_none(none)
end

#na?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/na/string.rb', line 74

def na?
  self =~ /@#{NA.na_tag}\b/
end

#normalize_colorString

Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg

Returns:

  • (String)

    Normalized color name



129
130
131
# File 'lib/na/colors.rb', line 129

def normalize_color
  gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg')
end

#projectObject



61
62
63
64
# File 'lib/na/string.rb', line 61

def project
  m = match(/^([ \t]*)([^\-][^@:]*?): *(@\S+ *)*$/)
  m ? m[2] : nil
end

#project?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/na/string.rb', line 57

def project?
  !action? && self =~ /:( +@\S+(\([^)]*\))?)*$/
end

#read_fileObject



29
30
31
32
33
34
35
# File 'lib/na/string.rb', line 29

def read_file
  file = File.expand_path(self)
  raise "Missing file #{file}" unless File.exist?(file)

  # IO.read(file).force_encoding('ASCII-8BIT').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
  IO.read(file).force_encoding('utf-8')
end

#shorten_pathString

Replace home directory with tilde

Returns:



199
200
201
# File 'lib/na/string.rb', line 199

def shorten_path
  sub(/^#{ENV['HOME']}/, '~')
end

#validate_colorString

Extract the longest valid %color name 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



111
112
113
114
115
116
117
118
119
120
# File 'lib/na/colors.rb', line 111

def validate_color
  valid_color = nil
  compiled = ''
  normalize_color.split('').each do |char|
    compiled += char
    valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i
  end

  valid_color
end

#wildcard_to_rxString

Convert wildcard characters to regular expressions

Returns:



173
174
175
# File 'lib/na/string.rb', line 173

def wildcard_to_rx
  gsub(/\./, '\\.').gsub(/\?/, '.').gsub(/\*/, '[^ ]*?')
end