class Toys::Utils::HelpText::HelpStringAssembler
@private
Attributes
result[R]
Public Class Methods
new(tool, executable_name, delegates, subtools, search_term, show_source_path, separate_sources, indent, indent2, wrap_width, styled)
click to toggle source
# File lib/toys/utils/help_text.rb, line 335 def initialize(tool, executable_name, delegates, subtools, search_term, show_source_path, separate_sources, indent, indent2, wrap_width, styled) require "toys/utils/terminal" @tool = tool @executable_name = executable_name @delegates = delegates @subtools = subtools @search_term = search_term @show_source_path = show_source_path @separate_sources = separate_sources @indent = indent @indent2 = indent2 @wrap_width = wrap_width @lines = Utils::Terminal.new(output: ::StringIO.new, styled: styled) assemble end
Private Instance Methods
add_at_least_one_group_to_synopsis(flag_group, synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 466 def add_at_least_one_group_to_synopsis(flag_group, synopsis) return if flag_group.empty? synopsis << "(" flag_group.flags.each do |flag| synopsis << "[#{flag_spec_string(flag, true)}]" end synopsis << ")" end
add_at_most_one_group_to_synopsis(flag_group, synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 451 def add_at_most_one_group_to_synopsis(flag_group, synopsis) return if flag_group.empty? synopsis << "[" first = true flag_group.flags.each do |flag| if first first = false else synopsis << "|" end synopsis << flag_spec_string(flag, true) end synopsis << "]" end
add_description_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 497 def add_description_section desc = @tool.long_desc.dup @delegates.each do |delegate| desc << "" << "Delegated from \"#{delegate.display_name}\"" unless delegate.long_desc.empty? desc << "" desc += delegate.long_desc end end desc = desc[1..-1] if desc.first == "" desc = wrap_indent(desc) return if desc.empty? @lines << "" @lines << bold("DESCRIPTION") desc.each do |line| @lines << indent_str(line) end end
add_exactly_one_group_to_synopsis(flag_group, synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 436 def add_exactly_one_group_to_synopsis(flag_group, synopsis) return if flag_group.empty? synopsis << "(" first = true flag_group.flags.each do |flag| if first first = false else synopsis << "|" end synopsis << flag_spec_string(flag, true) end synopsis << ")" end
add_flag_group_sections()
click to toggle source
# File lib/toys/utils/help_text.rb, line 516 def add_flag_group_sections @tool.flag_groups.each do |group| next if group.empty? @lines << "" desc_str = group.desc.to_s.upcase desc_str = "FLAGS" if desc_str.empty? @lines << bold(desc_str) precede_with_blank = false unless group.long_desc.empty? wrap_indent(group.long_desc).each do |line| @lines << indent_str(line) end precede_with_blank = true end group.flags.each do |flag| add_indented_section(flag_spec_string(flag), flag, precede_with_blank) precede_with_blank = true end end end
add_indented_section(header, info, precede_with_blank)
click to toggle source
# File lib/toys/utils/help_text.rb, line 584 def add_indented_section(header, info, precede_with_blank) @lines << "" if precede_with_blank @lines << indent_str(header) desc = info unless desc.is_a?(::Array) desc = wrap_indent2(info.long_desc) desc = wrap_indent2(info.desc) if desc.empty? end desc.each do |line| @lines << indent2_str(line) end end
add_name_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 367 def add_name_section @lines << bold("NAME") name_str = ([@executable_name] + @tool.full_name).join(" ") add_prefix_with_desc(name_str, @tool.desc) end
add_ordinary_group_to_synopsis(flag_group, synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 424 def add_ordinary_group_to_synopsis(flag_group, synopsis) flag_group.flags.each do |flag| synopsis << "[#{flag_spec_string(flag, true)}]" end end
add_positional_arguments_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 550 def add_positional_arguments_section args_to_display = @tool.positional_args return if args_to_display.empty? @lines << "" @lines << bold("POSITIONAL ARGUMENTS") precede_with_blank = false args_to_display.each do |arg_info| add_indented_section(arg_name(arg_info), arg_info, precede_with_blank) precede_with_blank = true end end
add_prefix_with_desc(prefix, desc)
click to toggle source
# File lib/toys/utils/help_text.rb, line 373 def add_prefix_with_desc(prefix, desc) if desc.empty? @lines << indent_str(prefix) elsif !desc.is_a?(WrappableString) @lines << indent_str("#{prefix} - #{desc}") else desc = wrap_indent_indent2(WrappableString.new(["#{prefix} -"] + desc.fragments)) @lines << indent_str(desc[0]) desc[1..-1].each do |line| @lines << indent2_str(line) end end end
add_required_group_to_synopsis(flag_group, synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 430 def add_required_group_to_synopsis(flag_group, synopsis) flag_group.flags.each do |flag| synopsis << "(#{flag_spec_string(flag, true)})" end end
add_source_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 486 def add_source_section return unless @show_source_path && @tool.source_info&.source_name @lines << "" @lines << bold("SOURCE") @lines << indent_str("Defined in #{@tool.source_info.source_name}") @delegates.each do |delegate| @lines << indent_str("Delegated from \"#{delegate.display_name}\"" \ " defined in #{delegate.source_info.source_name}") end end
add_subtool_list_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 562 def add_subtool_list_section return if @subtools.empty? @lines << "" @lines << bold("TOOLS") if @search_term @lines << indent_str("Showing search results for \"#{@search_term}\"") @lines << "" end first_section = true @subtools.each do |source_name, subtool_list| @lines << "" unless first_section if @separate_sources @lines << indent_str(underline("From #{source_name}")) @lines << "" end subtool_list.each do |local_name, subtool| add_prefix_with_desc(bold(local_name), subtool.desc) end first_section = false end end
add_synopsis_clause(synopsis)
click to toggle source
# File lib/toys/utils/help_text.rb, line 394 def add_synopsis_clause(synopsis) first = true synopsis.each do |line| @lines << (first ? indent_str(line) : indent2_str(line)) first = false end end
add_synopsis_section()
click to toggle source
# File lib/toys/utils/help_text.rb, line 387 def add_synopsis_section @lines << "" @lines << bold("SYNOPSIS") add_synopsis_clause(namespace_synopsis) unless @subtools.empty? add_synopsis_clause(tool_synopsis(@tool)) end
arg_name(arg_info)
click to toggle source
# File lib/toys/utils/help_text.rb, line 597 def arg_name(arg_info) case arg_info.type when :required underline(arg_info.display_name) when :optional "[#{underline(arg_info.display_name)}]" when :remaining "[#{underline(arg_info.display_name)}...]" end end
assemble()
click to toggle source
# File lib/toys/utils/help_text.rb, line 356 def assemble add_name_section add_synopsis_section add_description_section add_flag_group_sections add_positional_arguments_section add_subtool_list_section add_source_section @result = @lines.output.string end
bold(str)
click to toggle source
# File lib/toys/utils/help_text.rb, line 624 def bold(str) @lines.apply_styles(str, :bold) end
flag_spec_string(flag, in_synopsis = false)
click to toggle source
# File lib/toys/utils/help_text.rb, line 537 def flag_spec_string(flag, in_synopsis = false) flag.flag_syntax.map do |fs| str = bold(fs.str_without_value) if fs.flag_type != :value str elsif fs.value_type == :optional "#{str}#{fs.value_delim}[#{underline(fs.value_label)}]" else "#{str}#{fs.value_delim}#{underline(fs.value_label)}" end end.join(in_synopsis ? " | " : ", ") end
full_executable_name(tool_for_name)
click to toggle source
# File lib/toys/utils/help_text.rb, line 482 def full_executable_name(tool_for_name) bold(([@executable_name] + tool_for_name.full_name).join(" ")) end
indent2_str(str)
click to toggle source
# File lib/toys/utils/help_text.rb, line 636 def indent2_str(str) "#{' ' * (@indent + @indent2)}#{str}" end
indent_str(str)
click to toggle source
# File lib/toys/utils/help_text.rb, line 632 def indent_str(str) "#{' ' * @indent}#{str}" end
namespace_synopsis()
click to toggle source
# File lib/toys/utils/help_text.rb, line 475 def namespace_synopsis synopsis = [full_executable_name(@tool), underline("TOOL"), "[#{underline('ARGUMENTS')}...]"] wrap_indent_indent2(WrappableString.new(synopsis)) end
tool_synopsis(tool_for_name)
click to toggle source
# File lib/toys/utils/help_text.rb, line 402 def tool_synopsis(tool_for_name) synopsis = [full_executable_name(tool_for_name)] @tool.flag_groups.each do |flag_group| case flag_group when FlagGroup::Required add_required_group_to_synopsis(flag_group, synopsis) when FlagGroup::ExactlyOne add_exactly_one_group_to_synopsis(flag_group, synopsis) when FlagGroup::AtMostOne add_at_most_one_group_to_synopsis(flag_group, synopsis) when FlagGroup::AtLeastOne add_at_least_one_group_to_synopsis(flag_group, synopsis) else add_ordinary_group_to_synopsis(flag_group, synopsis) end end @tool.positional_args.each do |arg_info| synopsis << arg_name(arg_info) end wrap_indent_indent2(WrappableString.new(synopsis)) end
underline(str)
click to toggle source
# File lib/toys/utils/help_text.rb, line 628 def underline(str) @lines.apply_styles(str, :underline) end
wrap_indent(input)
click to toggle source
# File lib/toys/utils/help_text.rb, line 608 def wrap_indent(input) return WrappableString.wrap_lines(input, nil) unless @wrap_width WrappableString.wrap_lines(input, @wrap_width - @indent) end
wrap_indent2(input)
click to toggle source
# File lib/toys/utils/help_text.rb, line 613 def wrap_indent2(input) return WrappableString.wrap_lines(input, nil) unless @wrap_width WrappableString.wrap_lines(input, @wrap_width - @indent - @indent2) end
wrap_indent_indent2(input)
click to toggle source
# File lib/toys/utils/help_text.rb, line 618 def wrap_indent_indent2(input) return WrappableString.wrap_lines(input, nil) unless @wrap_width WrappableString.wrap_lines(input, @wrap_width - @indent, @wrap_width - @indent - @indent2) end