Module: Rubycom::Helpers
- Defined in:
- lib/rubycom/helpers.rb
Class Method Summary collapse
-
.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.
-
.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.
-
.format_tags(tag_list, desc_width = 90) ⇒ Array
Arranges each given tag hash such that all tags will line up nicely in vertical columns.
-
.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.
-
.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.
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
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
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
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.(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
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.
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 |