Class: Doing::TemplateString
- Includes:
- Color
- Defined in:
- lib/doing/template_string.rb
Overview
Template string formatting
Constant Summary
Constants included from Color
Color::ATTRIBUTES, Color::ATTRIBUTE_NAMES, Color::COLORED_REGEXP
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Instance Method Summary collapse
-
#apply_colors(color_array) ⇒ Object
Apply a color array to a string.
-
#colored ⇒ String
Return string with %colors replaced with escape codes.
-
#colors? ⇒ Boolean
Test if string contains any valid %colors.
- #fill(placeholder, value, wrap_width: 0, color: '', tags_color: '', reset: '') ⇒ Object
-
#initialize(string, placeholders: {}, force_color: false, wrap_width: 0, color: '', tags_color: '', reset: '') ⇒ TemplateString
constructor
A new instance of TemplateString.
-
#parse_colors ⇒ Hash
Parse a template string for %colors and return a hash of colors and string locations.
- #parsed_colors ⇒ Object
-
#raw ⇒ String
Remove all valid %colors from string.
- #reparse ⇒ Object
Methods included from Color
attributes, coloring?, #support?, #uncolor
Methods inherited from String
#add_at, #add_tags, #add_tags!, #cap_first, #chronify, #chronify_qty, #clean_unlinked_urls, #compress, #compress!, #dedup_tags, #dedup_tags!, #highlight_tags, #highlight_tags!, #ignore?, #is_rx?, #last_color, #link_urls, #link_urls!, #normalize_bool, #normalize_bool!, #normalize_case, #normalize_case!, #normalize_matching, #normalize_matching!, #normalize_order, #normalize_order!, #normalize_trigger, #normalize_trigger!, #pluralize, #remove_self_links, #replace_qualified_urls, #set_type, #simple_wrap, #tag, #tag!, #to_rx, #to_tags, #truncate, #truncate!, #truncmiddle, #truncmiddle!, #truthy?, #uncolor, #uncolor!, #validate_color, #wildcard_to_rx, #wrap
Constructor Details
#initialize(string, placeholders: {}, force_color: false, wrap_width: 0, color: '', tags_color: '', reset: '') ⇒ TemplateString
Returns a new instance of TemplateString.
33 34 35 36 37 38 39 40 |
# File 'lib/doing/template_string.rb', line 33 def initialize(string, placeholders: {}, force_color: false, wrap_width: 0, color: '', tags_color: '', reset: '') Color.coloring = true if force_color @colors = nil @original = string super(Color.reset + string) placeholders.each { |k, v| fill(k, v, wrap_width: wrap_width, color: color, tags_color: ) } end |
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
30 31 32 |
# File 'lib/doing/template_string.rb', line 30 def original @original end |
Instance Method Details
#apply_colors(color_array) ⇒ Object
Apply a color array to a string
109 110 111 112 113 114 115 116 |
# File 'lib/doing/template_string.rb', line 109 def apply_colors(color_array) str = dup color_array.reverse.each do |color| c = color[:color].empty? ? Color.send(color[:name]) : color[:color] str.insert(color[:index], c) end str end |
#colored ⇒ String
Return string with %colors replaced with escape codes
63 64 65 66 |
# File 'lib/doing/template_string.rb', line 63 def colored reparse parsed_colors[:string].apply_colors(parsed_colors[:colors]) end |
#colors? ⇒ Boolean
Test if string contains any valid %colors
47 48 49 50 51 52 |
# File 'lib/doing/template_string.rb', line 47 def colors? scan(/%([a-z]+)/).each do return true if Regexp.last_match(1).validate_color end false end |
#fill(placeholder, value, wrap_width: 0, color: '', tags_color: '', reset: '') ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 187 188 189 190 191 192 193 194 195 |
# File 'lib/doing/template_string.rb', line 118 def fill(placeholder, value, wrap_width: 0, color: '', tags_color: '', reset: '') reparse rx = /(?mi)(?<!\\)%(?<width>-?\d+)?(?:\^(?<mchar>.))?(?:(?<ichar>[ _t]|[^a-z0-9])(?<icount>\d+))?(?<prefix>.[ _t]?)?#{placeholder.sub(/^%/, '')}(?<after>.*?)$/ ph = raw.match(rx) return unless ph placeholder_offset = ph.begin(0) last_colors = parsed_colors[:colors].select { |v| v[:index] <= placeholder_offset + 4 } last_color = last_colors.map { |v| v[:color] }.pop(3).join('') sub!(rx) do m = Regexp.last_match after = m['after'] if value.nil? || value.empty? after else pad = m['width'].to_i mark = m['mchar'] || '' if placeholder == 'shortdate' && m['width'].nil? pad = 13 end indent = nil if m['ichar'] char = m['ichar'] =~ /t/ ? "\t" : ' ' indent = char * m['icount'].to_i end indent ||= placeholder =~ /^title/ ? '' : "\t" prefix = m['prefix'] if placeholder =~ /^title/ color = last_color + color if wrap_width.positive? || pad.positive? width = pad.positive? ? pad : wrap_width out = value.gsub(/%/, '\%').strip.wrap(width, pad: pad, indent: indent, offset: placeholder_offset, prefix: prefix, color: color, after: after, reset: reset, pad_first: false) out.(, last_color: color) if && !.empty? out else out = format("%s%s%#{pad}s%s", prefix, color, value.gsub(/%/, '\%').sub(/\s*$/, ''), after) out.(, last_color: color) if && !.empty? out end elsif placeholder =~ /^note/ if wrap_width.positive? || pad.positive? width = pad.positive? ? pad : wrap_width outstring = value.map do |l| if l.empty? ' ' else line = l.gsub(/%/, '\%').strip.wrap(width, pad: pad, indent: indent, offset: 0, prefix: prefix, color: last_color, after: after, reset: reset, pad_first: true) line.(, last_color: last_color) unless .nil? || .empty? "#{line} " end end.join("\n") "\n#{last_color}#{mark}#{outstring} " else out = format("\n%s%s%s%#{pad}s%s", indent, prefix, last_color, value.join("\n#{indent}#{prefix}").gsub(/%/, '\%').sub(/\s*$/, ''), after) out.(, last_color: last_color) if && !.empty? out end else format("%s%#{pad}s%s", prefix, value.gsub(/%/, '\%').sub(/\s*$/, ''), after) end end end @parsed_colors = parse_colors end |
#parse_colors ⇒ Hash
Parse a template string for %colors and return a hash of colors and string locations
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/doing/template_string.rb', line 86 def parse_colors working = dup color_array = [] scan(/(?<!\\)(%([a-z]+))/).each do |color| valid_color = color[1].validate_color next unless valid_color idx = working.match(/(?<!\\)%#{valid_color}/).begin(0) color_array.push({ name: valid_color, color: Color.send(valid_color), index: idx }) working.sub!(/(?<!\\)%#{valid_color}/, '') end { string: working, colors: color_array } end |
#parsed_colors ⇒ Object
77 78 79 |
# File 'lib/doing/template_string.rb', line 77 def parsed_colors @parsed_colors ||= parse_colors end |
#raw ⇒ String
Remove all valid %colors from string
73 74 75 |
# File 'lib/doing/template_string.rb', line 73 def raw parsed_colors[:string].uncolor end |
#reparse ⇒ Object
54 55 56 |
# File 'lib/doing/template_string.rb', line 54 def reparse @parsed_colors = nil end |