Sha256: b588eb7bda955f19c2386b131f697f078cd276990ff22de485acab98e11b1b5c

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

class HelpFormatter < RDoc::Markup::ToAnsi
  def initialize(ansi)
    @ansi = ansi
    super()
    @headings.clear
    return unless @ansi
    @headings[1] = ["\e[1m", "\e[m"] # bold
    @headings[2] = ["\e[4m", "\e[m"] # underline
    @headings[3] = ["\e[3m", "\e[m"] # italic
  end

  def init_tags
    return unless @ansi
    add_tag :BOLD, "\e[1m", "\e[m" # bold
    add_tag :EM,   "\e[3m", "\e[m" # italic
    add_tag :TT,   "\e[3m", "\e[m" # italic
  end

  def accept_list_item_start(list_item)
    @indent += 2
    super
    @res.pop if @res.last == ?\n
    @indent -= 2
  end

   def accept_verbatim(verbatim)
    indent = ?\s * (@indent + (@ansi ? 2 : 4))
    verbatim.parts.map(&:each_line).flat_map(&:entries).each.with_index do |line, index|
      case
      when !@ansi
        @res << indent << line
      when line.start_with?("$ ")
        @res << indent << "\e[90m$\e[;3m " << line[2..-1] << "\e[m"
      else
        @res << indent << "\e[90m> " << line << "\e[m"
      end
    end
    @res << ?\n
  end

  def accept_heading(heading)
    @indent += 2 unless heading.level == 1
    super
    @indent -= 2 unless heading.level == 1
  end

  def accept_paragraph(paragraph)
    @indent += 2
    text = paragraph.text.tr_s(?\r, ?\s).tr_s(?\n, ?\s)
    wrap attributes text
    @indent -= 2
    @res << ?\n
  end

  def start_accepting
    super
    @res = @ansi ? ["\e[0m"] : []
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nswtopo-3.1.3 lib/nswtopo/help_formatter.rb
nswtopo-3.1.2 lib/nswtopo/help_formatter.rb
nswtopo-3.1.1 lib/nswtopo/help_formatter.rb
nswtopo-3.1 lib/nswtopo/help_formatter.rb
nswtopo-3.0.1 lib/nswtopo/help_formatter.rb
nswtopo-3.0 lib/nswtopo/help_formatter.rb
nswtopo-2.0.0 lib/nswtopo/help_formatter.rb
nswtopo-2.0.0.pre.beta1 lib/nswtopo/help_formatter.rb