Sha256: e4e7a611ac66384f7ce85be423fcb518e488f9c02a381533cf6713d20106906c

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

class PryMoves::Formatter

  attr_accessor :colorize

  def initialize colorize = true
    @colorize = colorize
  end

  MAX_PARAMS = 5
  def method_signature(binding)
    meth = binding.eval('__method__')
    meth_obj = meth ? Pry::Method.from_binding(binding) : nil
    if !meth_obj
      ""
    elsif meth_obj.undefined?
      "#{meth_obj.name}(UNKNOWN) (undefined method)"
    else
      args = meth_obj.parameters.map.with_index do |(type, name), i|
        if name
          value = format_arg binding, name.to_s
          show_value = true
        else
          name = (type == :block ? 'block' : "arg#{i + 1}")
        end
        name = case type
          when :req   then "#{name} ="
          when :key   then "#{name}:"
          when :opt   then "#{name}=?"
          when :rest  then "*#{name}"
          when :block then "&#{name}"
          else '?'
        end
        show_value ? "#{name} #{value}" : name
      end
      if args.count > MAX_PARAMS
        args = args.first(MAX_PARAMS) + ["(#{args.count - MAX_PARAMS} more params)…"]
      end
      "#{meth_obj.name}(#{args.join(', ')})"
    end
  end

  def format_arg binding, arg_name
    arg = begin
      binding.eval(arg_name.to_s)
    rescue Exception
      "?"
    end
    format_obj arg
  end

  def first_line str
    str.split("\n").first
  end

  def cut_string str
    return str unless str
    str.length > 50 ? "#{str.first 50}..." : str
  end

  def shorten_path(path)
    project_root = PryMoves.project_root
    path = path.gsub( /^#{project_root}\//, '')
    PryMoves::Backtrace.trim_path ?
      File.basename(path, ".*") : path
  end

  def format_obj(obj)
    if obj.is_a? String
      format_obj2 cut_string first_line obj
    else
      first_line format_obj2 obj
    end
  end

  def format_obj2(obj)
    if @colorize
      PryMoves::Painter.colorize obj
    else
      i = obj.inspect
      i.start_with?('#<') ? obj.class.to_s : i
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pry-moves-1.0.15 lib/pry-moves/formatter.rb
pry-moves-1.0.14 lib/pry-moves/formatter.rb
pry-moves-1.0.13 lib/pry-moves/formatter.rb
pry-moves-1.0.12 lib/pry-moves/formatter.rb