Class: String

Inherits:
Object show all
Includes:
Doing::ChronifyString, Doing::Color, Doing::Completion::StringUtils, Doing::StringHighlight, Doing::StringNormalize, Doing::StringQuery, Doing::StringTags, Doing::StringTransform, Doing::StringTruncate, Doing::StringURL
Defined in:
lib/doing/good.rb,
lib/doing/colors.rb,
lib/doing/normalize.rb,
lib/doing/string/string.rb,
lib/doing/chronify/chronify.rb,
lib/doing/completion/fig_completion.rb,
lib/doing/completion/zsh_completion.rb,
lib/doing/completion/completion_string.rb

Overview

Template coloring

Direct Known Subclasses

Doing::TemplateString

Constant Summary

Constants included from Doing::Color

Doing::Color::ATTRIBUTES, Doing::Color::ATTRIBUTE_NAMES, Doing::Color::ESCAPE_REGEX

Instance Method Summary collapse

Methods included from Doing::Completion::StringUtils

#ltrunc, #ltrunc!, #short_desc

Methods included from Doing::ChronifyString

#chronify, #chronify_qty, #expand_date_tags, #is_range?, #split_date_range, #time_string, #to_seconds

Methods included from Doing::StringURL

#clean_unlinked_urls, #link_urls, #link_urls!, #remove_self_links, #replace_qualified_urls

Methods included from Doing::StringTruncate

#trunc, #trunc!, #truncend, #truncend!, #truncmiddle, #truncmiddle!

Methods included from Doing::StringTransform

#cap_first, #compress, #compress!, #set_type, #simple_wrap, #titlecase, #to_p, #wrap

Methods included from Doing::StringTags

#add_at, #add_tags, #add_tags!, #dedup_tags, #dedup_tags!, #remove_at, #split_tags, #tag, #tag!, #to_tags

Methods included from Doing::StringQuery

#ignore?, #ignore_case, #rx?, #to_bool, #to_phrase_query, #to_query, #to_rx, #truthy?, #wildcard_to_rx

Methods included from Doing::StringHighlight

#highlight_search, #highlight_search!, #highlight_tags, #highlight_tags!, #last_color, #uncolor!

Methods included from Doing::Color

attributes, coloring?, #rgb, #support?, template, uncolor

Methods included from Doing::StringNormalize

#normalize_age, #normalize_age!, #normalize_bool, #normalize_bool!, #normalize_case, #normalize_case!, #normalize_change_type, #normalize_list_style, #normalize_matching, #normalize_matching!, #normalize_order, #normalize_order!, #normalize_tag_sort, #normalize_tag_sort!, #normalize_trigger, #normalize_trigger!

Instance Method Details

#good?Boolean

Tests if object is nil or empty



46
47
48
# File 'lib/doing/good.rb', line 46

def good?
  !strip.empty?
end

#last_color_codeObject

Get the calculated ANSI color at the end of the string



137
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
# File 'lib/doing/colors.rb', line 137

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

#normalize_colorString

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



128
129
130
# File 'lib/doing/colors.rb', line 128

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

#sanitizeObject



6
7
8
# File 'lib/doing/completion/fig_completion.rb', line 6

def sanitize
  gsub(/"/, '\"')
end

#uncolorObject

Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.



220
221
222
# File 'lib/doing/colors.rb', line 220

def uncolor
  gsub(ESCAPE_REGEX, '')
end

#utf8String

Force UTF-8 encoding if available



33
34
35
36
37
38
39
# File 'lib/doing/string/string.rb', line 33

def utf8
  if String.method_defined? :force_encoding
    dup.force_encoding('utf-8')
  else
    self
  end
end

#valid_id?Boolean

Test if string is a valid 32-character MD5 id



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

def valid_id?
  strip =~ /^[a-z0-9]{32}$/ ? true : false
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.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/doing/colors.rb', line 108

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

  valid_color
end