Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/stdlib/string.rb,
lib/wavefront-cli/output/hcl/stdlib/string.rb

Overview

Extensions to stdlib String

Instance Method Summary collapse

Instance Method Details

#braced(indent = 0) ⇒ Object



5
6
7
8
# File 'lib/wavefront-cli/output/hcl/stdlib/string.rb', line 5

def braced(indent = 0)
  pad = ' ' * indent
  "\n#{pad}{#{self}\n#{pad}}"
end

#cmd_fold(twidth = TW, indent = 10) ⇒ Object

Fold long command lines. We can’t break on a space after an option or it confuses docopt.

Parameters:

  • twidth (Integer) (defaults to: TW)

    terminal width

  • indent (Integer) (defaults to: 10)

    size of hanging indent, in chars



10
11
12
13
# File 'lib/wavefront-cli/stdlib/string.rb', line 10

def cmd_fold(twidth = TW, indent = 10)
  gsub(/(-\w) /, '\\1^').scan_line(twidth - 12).join("\n" + ' ' * indent)
                        .restored
end

#fold(twidth = 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.

rubocop:disable Metrics/AbcSize

Parameters:

  • twidth (Integer) (defaults to: TW)

    terminal width

  • indent (Integer) (defaults to: 10)

    size of hanging indent, in chars

  • prefix (String) (defaults to: '')

    prepended to every line

Returns:

  • (String)

    the folded line



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wavefront-cli/stdlib/string.rb', line 36

def fold(twidth = TW, indent = 10, prefix = '')
  chunks = gsub(/default: /, 'default:^').scan_line(twidth - 8)
  first_line = format("%s%s\n", prefix, chunks.shift)

  return first_line.restored if chunks.empty?

  rest = chunks.join(' ').scan_line(twidth - indent - 5).map do |l|
    prefix + ' ' * indent + l
  end

  (first_line + rest.join("\n") + "\n").restored
end

#opt_fold(twidth = TW, indent = 10) ⇒ String

Wrapper around #fold()

Parameters:

  • twidth (Integer) (defaults to: TW)

    width of terminal, in chars

  • indent (Integer) (defaults to: 10)

    hanging indent of following lines

Returns:

  • (String)

    folded and indented string



21
22
23
# File 'lib/wavefront-cli/stdlib/string.rb', line 21

def opt_fold(twidth = TW, indent = 10)
  fold(twidth, indent, '  ')
end

#restoredObject

We use a carat as a temporary whitespace character to avoid undesirable line breaking. This puts it back



53
54
55
# File 'lib/wavefront-cli/stdlib/string.rb', line 53

def restored
  tr('^', ' ')
end

#scan_line(width) ⇒ Array

Returns original string chunked into an array width elements whose length < width.

Parameters:

  • width (Integer)

    length of longest string (width of terminal less some margin)

Returns:

  • (Array)

    original string chunked into an array width elements whose length < width



70
71
72
# File 'lib/wavefront-cli/stdlib/string.rb', line 70

def scan_line(width)
  scan(/\S.{0,#{width}}\S(?=\s|$)|\S+/)
end

#to_secondsObject



74
75
76
77
78
79
80
81
82
# File 'lib/wavefront-cli/stdlib/string.rb', line 74

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_snakeString

Make a camelCase string be snake_case

Returns:



96
97
98
99
100
101
# File 'lib/wavefront-cli/stdlib/string.rb', line 96

def to_snake
  gsub(/(.)([A-Z])/) do
    Regexp.last_match[1] + '_' +
      Regexp.last_match[2].downcase
  end
end

#unit_factor(unit) ⇒ Integer

How many seconds in the given unit

Parameters:

  • unit (Symbol)

Returns:

  • (Integer)


88
89
90
91
# File 'lib/wavefront-cli/stdlib/string.rb', line 88

def unit_factor(unit)
  factors = { s: 1, m: 60, h: 3600, d: 86_400, w: 604_800 }
  factors[unit] || 1
end

#value_fold(indent = 0, twidth = TW) ⇒ Object

Fold long value lines in two-column output. The returned string is appended to a key, so the first line is not indented.



60
61
62
63
# File 'lib/wavefront-cli/stdlib/string.rb', line 60

def value_fold(indent = 0, twidth = TW)
  max_line_length = twidth - indent - 4
  scan_line(max_line_length).join("\n" + ' ' * indent)
end