Module: Howzit::StringUtils

Included in:
String
Defined in:
lib/howzit/stringutils.rb

Overview

String Extensions

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/howzit/stringutils.rb', line 80

def available?
  if File.exist?(File.expand_path(self))
    File.executable?(File.expand_path(self))
  else
    system "which #{self}", out: File::NULL
  end
end

#extract_metadataObject



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_metadataObject



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 (meta)
  data = {}
  meta.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

Parameters:

  • len (number)

    max length of string



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

#uncolorObject

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