Module: Howzit::StringUtils
- Included in:
- String
- Defined in:
- lib/howzit/stringutils.rb
Overview
String Extensions
Instance Method Summary collapse
- #available? ⇒ Boolean
- #extract_metadata ⇒ Object
- #get_metadata ⇒ Object
- #normalize_metadata(meta) ⇒ Object
- #render_template(vars) ⇒ Object
- #render_template!(vars) ⇒ Object
- #split_line(width, indent = '') ⇒ Object
-
#trunc(len) ⇒ Object
Truncate string to nearest word.
- #trunc!(len) ⇒ Object
-
#uncolor ⇒ Object
Just strip out color codes when requested.
-
#wrap(width) ⇒ Object
Adapted from https://github.com/pazdera/word_wrap/, copyright (c) 2014, 2015 Radek Pazdera Distributed under the MIT License.
- #wrap!(width) ⇒ Object
Instance Method Details
#available? ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/howzit/stringutils.rb', line 80 def available? if File.exist?(File.(self)) File.executable?(File.(self)) else system "which #{self}", out: File::NULL end end |
#extract_metadata ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/howzit/stringutils.rb', line 101 def if File.exist?(self) leader = IO.read(self).split(/^#/)[0].strip leader.length > 0 ? leader. : {} else {} end end |
#get_metadata ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/howzit/stringutils.rb', line 110 def data = {} scan(/(?mi)^(\S[\s\S]+?): ([\s\S]*?)(?=\n\S[\s\S]*?:|\Z)/).each do |m| data[m[0].strip.downcase] = m[1] end (data) end |
#normalize_metadata(meta) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/howzit/stringutils.rb', line 118 def () data = {} .each do |k, v| case k when /^templ\w+$/ data['template'] = v when /^req\w+$/ data['required'] = v else data[k] = v end end data end |
#render_template(vars) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/howzit/stringutils.rb', line 88 def render_template(vars) content = dup vars.each do |k, v| content.gsub!(/\[%#{k}(:.*?)?\]/, v) end content.gsub(/\[%(.*?):(.*?)\]/, '\2') end |
#render_template!(vars) ⇒ Object
97 98 99 |
# File 'lib/howzit/stringutils.rb', line 97 def render_template!(vars) replace render_template(vars) end |
#split_line(width, indent = '') ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/howzit/stringutils.rb', line 63 def split_line(width, indent = '') line = dup at = line.index(/\s/) last_at = at while !at.nil? && at < width last_at = at at = line.index(/\s/, last_at + 1) end if last_at.nil? [indent + line[0, width], line[width, line.length]] else [indent + line[0, last_at], line[last_at + 1, line.length]] end end |
#trunc(len) ⇒ Object
Truncate string to nearest word
51 52 53 54 55 56 57 |
# File 'lib/howzit/stringutils.rb', line 51 def trunc(len) split(/ /).each_with_object([]) do |x, ob| break ob unless ob.join(' ').length + ' '.length + x.length <= len ob.push(x) end.join(' ').strip end |
#trunc!(len) ⇒ Object
59 60 61 |
# File 'lib/howzit/stringutils.rb', line 59 def trunc!(len) replace trunc(len) end |
#uncolor ⇒ Object
Just strip out color codes when requested
7 8 9 |
# File 'lib/howzit/stringutils.rb', line 7 def uncolor gsub(/\e\[[\d;]+m/, '').gsub(/\e\]1337;SetMark/,'') end |
#wrap(width) ⇒ Object
Adapted from https://github.com/pazdera/word_wrap/, copyright (c) 2014, 2015 Radek Pazdera Distributed under the MIT License
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/howzit/stringutils.rb', line 14 def wrap(width) width ||= 80 output = [] indent = '' text = gsub(/\t/, ' ') text.lines do |line| line.chomp! "\n" if line.length > width indent = if line.uncolor =~ /^(\s*(?:[+\-*]|\d+\.) )/ ' ' * Regexp.last_match[1].length else '' end new_lines = line.split_line(width) while new_lines.length > 1 && new_lines[1].length + indent.length > width output.push new_lines[0] new_lines = new_lines[1].split_line(width, indent) end output += [new_lines[0], indent + new_lines[1]] else output.push line end end output.map!(&:rstrip) output.join("\n") end |
#wrap!(width) ⇒ Object
45 46 47 |
# File 'lib/howzit/stringutils.rb', line 45 def wrap!(width) replace(wrap(width)) end |