\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /proc/self/root/opt/chef/embedded/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/commands/

Viewing File: whereami.rb

# frozen_string_literal: true

require 'method_source'

class Pry
  class Command
    class Whereami < Pry::ClassCommand
      def initialize(*)
        super

        @method_code = nil
      end

      class << self
        attr_accessor :method_size_cutoff
      end

      @method_size_cutoff = 30

      match 'whereami'
      description 'Show code surrounding the current context.'
      group 'Context'

      banner <<-'BANNER'
        Usage: whereami [-qn] [LINES]

        Describe the current location. If you use `binding.pry` inside a method then
        whereami will print out the source for that method.

        If a number is passed, then LINES lines before and after the current line will be
        shown instead of the method itself.

        The `-q` flag can be used to suppress error messages in the case that there's
        no code to show. This is used by pry in the default before_session hook to show
        you when you arrive at a `binding.pry`.

        The `-n` flag can be used to hide line numbers so that code can be copy/pasted
        effectively.

        When pry was started on an Object and there is no associated method, whereami
        will instead output a brief description of the current object.
      BANNER

      def setup
        if target.respond_to?(:source_location)
          file, @line = target.source_location
          @file = expand_path(file)
        else
          @file = expand_path(target.eval('__FILE__'))
          @line = target.eval('__LINE__')
        end
        @method = Pry::Method.from_binding(target)
      end

      def options(opt)
        opt.on :q, :quiet, "Don't display anything in case of an error"
        opt.on :n, :"no-line-numbers", "Do not display line numbers"
        opt.on :m, :method, "Show the complete source for the current method."
        opt.on :c, :class, "Show the complete source for the current class or module."
        opt.on :f, :file, "Show the complete source for the current file."
      end

      def code
        @code ||= if opts.present?(:m)
                    method_code || raise(CommandError, "Cannot find method code.")
                  elsif opts.present?(:c)
                    class_code || raise(CommandError, "Cannot find class code.")
                  elsif opts.present?(:f)
                    Pry::Code.from_file(@file)
                  elsif args.any?
                    code_window
                  else
                    default_code
                  end
      end

      def code?
        !!code
      rescue MethodSource::SourceNotFoundError
        false
      end

      def bad_option_combination?
        [opts.present?(:m), opts.present?(:f),
         opts.present?(:c), args.any?].count(true) > 1
      end

      def location
        "#{@file}:#{@line} #{@method && @method.name_with_owner}"
      end

      def process
        if bad_option_combination?
          raise CommandError, "Only one of -m, -c, -f, and  LINES may be specified."
        end

        return if nothing_to_do?

        if internal_binding?(target)
          handle_internal_binding
          return
        end

        set_file_and_dir_locals(@file)

        pretty_code = code.with_line_numbers(use_line_numbers?)
          .with_marker(marker)
          .highlighted
        pry_instance.pager.page(
          "\n#{bold('From:')} #{location}:\n\n" + pretty_code + "\n"
        )
      end

      private

      def nothing_to_do?
        opts.quiet? && (internal_binding?(target) || !code?)
      end

      def use_line_numbers?
        !opts.present?(:n)
      end

      def marker
        !opts.present?(:n) && @line
      end

      def top_level?
        target_self == Pry.main
      end

      def handle_internal_binding
        if top_level?
          output.puts "At the top level."
        else
          output.puts "Inside #{Pry.view_clip(target_self)}."
        end
      end

      def small_method?
        @method.source_range.count < self.class.method_size_cutoff
      end

      def default_code
        if method_code && small_method?
          method_code
        else
          code_window
        end
      end

      def code_window
        Pry::Code.from_file(@file).around(@line, window_size)
      end

      def method_code
        return @method_code if @method_code

        @method_code = Pry::Code.from_method(@method) if valid_method?
      end

      # This either returns the `target_self`
      # or it returns the class of `target_self` if `target_self` is not a class.
      # @return [Pry::WrappedModule]
      def target_class
        return Pry::WrappedModule(target_self) if target_self.is_a?(Module)

        Pry::WrappedModule(target_self.class)
      end

      def class_code
        @class_code ||=
          begin
            mod = @method ? Pry::WrappedModule(@method.owner) : target_class
            idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
            idx && Pry::Code.from_module(mod, idx)
          end
      end

      def valid_method?
        @method && @method.source? && expand_path(@method.source_file) == @file &&
          @method.source_range.include?(@line)
      end

      def expand_path(filename)
        return unless filename
        return filename if Pry.eval_path == filename

        File.expand_path(filename)
      end

      def window_size
        if args.empty?
          pry_instance.config.default_window_size
        else
          args.first.to_i
        end
      end
    end

    Pry::Commands.add_command(Pry::Command::Whereami)
    Pry::Commands.alias_command '@', 'whereami'
    Pry::Commands.alias_command(/whereami[!?]+/, 'whereami')
  end
end