Module: Rubycom::Helpers

Defined in:
lib/rubycom/helpers.rb

Class Method Summary collapse

Class Method Details

.format_command_list(command_doc, desc_width = 90, indent = '') ⇒ Array

Arranges each command_name => command_description in command_doc with a separator such that all command names and descriptions will line up nicely in vertical columns.

formatted to line up vertically

Parameters:

  • command_doc (Hash)

    a mapping of command names to documentation summaries

  • desc_width (Integer) (defaults to: 90)

    the maximum width to use for the description column

Returns:

  • (Array)

    a list of strings comprised of command_name, separator, and description

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
# File 'lib/rubycom/helpers.rb', line 44

def self.format_command_list(command_doc, desc_width = 90, indent ='')
  return [] if command_doc.nil?
  raise ArgumentError, "command_doc should be a Hash but was a #{command_doc.class}" unless command_doc.class == Hash
  command_doc = {} if command_doc.nil?
  longest_command_name = command_doc.keys.max { |t, n| t.to_s.length <=> n.to_s.length }
  command_doc.map { |command_name, doc|
    self.format_command_summary("#{indent}#{command_name}", doc, self.get_separator(command_name, longest_command_name), desc_width)
  }
end

.format_command_summary(command_name, command_description, separator = ' - ', max_width = 90) ⇒ Object

Arranges the given command_name and command_description with the separator in a standard format

Parameters:

  • command_name (String)

    the command format

  • command_description (String)

    the description for the given command

  • separator (String) (defaults to: ' - ')

    optional separator to use

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
# File 'lib/rubycom/helpers.rb', line 72

def self.format_command_summary(command_name, command_description, separator = '  -  ', max_width = 90)
  command_name = '' if command_name.nil?
  command_description = '' if command_description.nil?
  separator = '' if separator == nil
  raise ArgumentError, "command_name and separator #{command_name}#{separator} size should not be greater than max_width: #{max_width} but was #{separator.size + command_name.size}" if command_name.size+separator.size > max_width
  $stdout.sync = true
  prefix_space = (' ' * "#{command_name}#{separator}".length)
  line_width = max_width - prefix_space.length
  "#{command_name}#{separator}#{self.word_wrap(command_description, line_width, prefix_space)}\n"
end

.format_tags(tag_list, desc_width = 90) ⇒ Array

Arranges each given tag hash such that all tags will line up nicely in vertical columns.

formatted to line up vertically

Parameters:

  • tag_list (Array)

    an Array of Hashes which include keys: :name, :tag_name, :text, :types

  • desc_width (Integer) (defaults to: 90)

    the maximum width to use for the description column

Returns:

  • (Array)

    a list of strings comprised of types, separator, name||tag_name, separator, text

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubycom/helpers.rb', line 10

def self.format_tags(tag_list, desc_width = 90)
  tag_list = [] if tag_list.nil?
  raise ArgumentError, "tag_list should be an Array but was a #{tag_list.class}" unless tag_list.class == Array
  tag_list.each { |h|
    raise ArgumentError, "tag #{h} should be a Hash but was a #{h.class}" unless h.class == Hash
    [:name, :tag_name, :text, :types].each { |t| h.fetch(t) }
    types = h[:types]
    raise ArgumentError, "tag[:types] #{types} should be an Array but was #{types.class}" unless types.class == Array
  }

  longest_name = tag_list.map { |tag| (tag[:name].nil?) ? tag[:tag_name] : tag[:name] }.max
  longest_types = tag_list.map { |tag| "#{tag[:types]}" }.max
  longest_combo_name = tag_list.map { |tag| "#{tag[:tag_name]}#{tag[:name]}" }.max
  {
      others: tag_list.select { |tag| !["param", "return"].include?(tag[:tag_name]) }.map { |tag|
        combo_name = (tag[:tag_name].nil? || tag[:tag_name].empty? || tag[:name].nil? || tag[:name].empty?) ? '' : "#{tag[:tag_name]}: #{tag[:name]}"
        "#{(tag[:types].empty?) ? '' : tag[:types]}#{self.get_separator(tag[:types], longest_types, tag[:types].empty? ? nil : ' ')}#{self.format_command_summary(combo_name, tag[:text], self.get_separator(combo_name, longest_combo_name), desc_width)}"
      },
      params: tag_list.select { |tag| tag[:tag_name] == "param" }.map { |tag|
        "#{tag[:types]}#{self.get_separator(tag[:types], longest_types, ' ')}#{self.format_command_summary(tag[:name], tag[:text], self.get_separator(tag[:name], longest_name), desc_width)}"
      },
      returns: tag_list.select { |tag| tag[:tag_name] == "return" }.map { |tag|
        "#{tag[:types]}#{self.get_separator(tag[:types], longest_types, ' ')}#{self.format_command_summary(tag[:tag_name], tag[:text], self.get_separator(tag[:tag_name], longest_name), desc_width)}"
      }
  }
end

.get_separator(name, longest_name = '', sep = ' - ') ⇒ String

Creates a separator with the appropriate spacing to line up a command/description pair in a command list

Parameters:

  • name (String)

    the command name to create a doc separator for

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

    the longest name which will be shown above or below the given name

  • sep (String) (defaults to: ' - ')

    the separator to use

Returns:

  • (String)

    a spaced separator String for use in a command/description list



60
61
62
63
64
65
# File 'lib/rubycom/helpers.rb', line 60

def self.get_separator(name, longest_name='', sep='  -  ')
  name = "#{name}" unless name.class == String
  sep = '' if sep.nil?
  longest_name = name if name.size > longest_name.size
  (' ' * (longest_name.to_s.length - name.to_s.length)) << sep
end

.word_wrap(text, line_width = 80, prefix = '') ⇒ Object

Converts a string longer than line_width to a multiline string where each line is at most line_width long.

Parameters:

  • text (String)

    the text to be wrapped

  • line_width (Integer) (defaults to: 80)

    the maximum length any single line in the string should be, default: 80, minimum: 1

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

    a prefix to add the the front of any new lines created as a result of the wrap, default: ”



88
89
90
91
92
93
94
95
# File 'lib/rubycom/helpers.rb', line 88

def self.word_wrap(text, line_width=80, prefix='')
  text = "#{text}" unless text.class == String
  prefix = "#{prefix}" unless prefix.class == String
  line_width = 1 if line_width < 1
  ([text.gsub("\n", ' ')].map { |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  } * "\n").gsub("\n", "\n#{prefix}")
end