module Pry::Helpers::CommandHelpers

def unindent(dirty_text, left_padding = 0)

Returns:
  • (String) - the text with indentation stripped

Parameters:
  • dirty_text (String) -- The text from which to remove indentation
def unindent(dirty_text, left_padding = 0)
  text = dirty_text.sub(/\A[ \t]+\z/, '') # Empty blank lines.
  # Find the longest common whitespace to all indented lines. Ignore lines
  # containing just -- or ++ as these seem to be used by comment authors
  # as delimiters.
  scanned_text = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/)
  margin = scanned_text.inject do |current_margin, next_indent|
    if next_indent.start_with?(current_margin)
      current_margin
    elsif current_margin.start_with?(next_indent)
      next_indent
    else
      ''
    end
  end
  text.gsub(/^#{margin}/, ' ' * left_padding)
end