Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/wavefront-cli/string.rb,
lib/wavefront-cli/output/hcl/dashboard.rb
Overview
Extensions to the String class to help with formatting.
Instance Method Summary collapse
- #braced(indent = 0) ⇒ Object
-
#cmd_fold(tw = TW, indent = 10) ⇒ Object
Fold long command lines.
-
#fold(tw = TW, indent = 10, prefix = '') ⇒ String
Fold long lines with a hanging indent.
-
#opt_fold(tw = TW, indent = 10) ⇒ String
Wrapper around #fold().
-
#restored ⇒ Object
We use a carat as a temporary whitespace character to avoid undesirable line breaking.
-
#scan_line(width) ⇒ Array
Original string chunked into an array width elements whose length < width.
- #to_seconds ⇒ Object
-
#to_snake ⇒ String
Make a camelCase string be snake_case.
-
#unit_factor(unit) ⇒ Integer
How many seconds in the given unit.
Instance Method Details
#braced(indent = 0) ⇒ Object
90 91 92 93 |
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 90 def braced(indent = 0) pad = ' ' * indent "\n#{pad}{#{self}\n#{pad}}" end |
#cmd_fold(tw = TW, indent = 10) ⇒ Object
Fold long command lines. We can’t break on a space after an option or it confuses docopt.
10 11 12 13 |
# File 'lib/wavefront-cli/string.rb', line 10 def cmd_fold(tw = TW, indent = 10) gsub(/(-\w) /, '\\1^').scan_line(tw - 12).join("\n" + ' ' * indent) .restored end |
#fold(tw = TW, indent = 10, prefix = '') ⇒ String
Fold long lines with a hanging indent. Originally a special case for option folding, now addded the prefix parameter to make it more general. Don’t line-break default values, because it also breaks docopt.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wavefront-cli/string.rb', line 35 def fold(tw = TW, indent = 10, prefix = '') chunks = gsub(/default: /, 'default:^').scan_line(tw - 8) first_line = format("%s%s\n", prefix, chunks.shift) return first_line.restored if chunks.empty? rest = chunks.join(' ').scan_line(tw - indent - 5).map do |l| prefix + ' ' * indent + l end (first_line + rest.join("\n") + "\n").restored end |
#opt_fold(tw = TW, indent = 10) ⇒ String
Wrapper around #fold()
21 22 23 |
# File 'lib/wavefront-cli/string.rb', line 21 def opt_fold(tw = TW, indent = 10) fold(tw, indent, ' ') end |
#restored ⇒ Object
We use a carat as a temporary whitespace character to avoid undesirable line breaking. This puts it back
51 52 53 |
# File 'lib/wavefront-cli/string.rb', line 51 def restored tr('^', ' ') end |
#scan_line(width) ⇒ Array
Returns original string chunked into an array width elements whose length < width.
60 61 62 |
# File 'lib/wavefront-cli/string.rb', line 60 def scan_line(width) scan(/\S.{0,#{width}}\S(?=\s|$)|\S+/) end |
#to_seconds ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/wavefront-cli/string.rb', line 64 def to_seconds begin number, unit = match(/^(\d+)([smhdw])$/).captures rescue NoMethodError raise ArgumentError end number.to_i * unit_factor(unit.to_sym) end |
#to_snake ⇒ String
Make a camelCase string be snake_case
86 87 88 89 |
# File 'lib/wavefront-cli/string.rb', line 86 def to_snake self.gsub(/(.)([A-Z])/) { Regexp.last_match[1] + '_' + Regexp.last_match[2].downcase } end |
#unit_factor(unit) ⇒ Integer
How many seconds in the given unit
78 79 80 81 |
# File 'lib/wavefront-cli/string.rb', line 78 def unit_factor(unit) factors = { s: 1, m: 60, h: 3600, d: 86_400, w: 604_800 } factors[unit] || 1 end |