Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] whereami arguments specifying the lines before and after the current line independently #2219

Open
takaebato opened this issue Sep 20, 2021 · 4 comments

Comments

@takaebato
Copy link

takaebato commented Sep 20, 2021

Requested feature

I'd like to propose additional arguments of whereami that behaves as described below↓.

whereami n1 n2  #=> shows lines from current_line_num - n1 to current_line_num + n2
# whereami n behaves the same way as before.

Reason

I realize this additional argument of whereami by overriding some methods in my .pryrc (shown in the bottom), and have found it useful, especially when using binding.pry.

In my case, I set an alias of whereami 10 30 (and one with -n) to show the lines before and after the current line as a ratio of 1:3 which fits the height of my terminal.
The main idea is that we care less about the previous lines, but we care more about the following lines when using binding.pry, since we

  • check how many lines to go next or continue.
  • copy and paste methods below the current line.

You can use whereami 30 to show many lines, but you have to scroll down and type q to exit, which takes time. This is almost the same when using whereami [-m|-c|-f].

If you use whereami n1 n2, you can determine how many lines to show before and after the current line, which is great.
I gave an example of my binding.pry workflow, but since the command is versatile, it can be applied in various situations.

Summary

In short, I propose the feature for the following reasons.

  • It is a versatile feature that could be useful in various situations. My binding.pry workflow is one example.
  • For users, the feature is pure addition, so it does not change the existing behavior of whereami and other commands.
  • I think it could be implemented without impacting gems dependent on pry.

I'd be happy to hear your thoughts, and implement the feature if it is accepted!

(The codes in my .pryrc↓)

class Pry
  class Code
    def around(lineno, *lines)
      return self unless lineno

      before_lines = lines.first || 1
      after_lines = lines.second || before_lines

      select do |loc|
        loc.lineno >= lineno - before_lines && loc.lineno <= lineno + after_lines
      end
    end
  end
  
  class Command
    class Whereami
      def code_window
        Pry::Code.from_file(@file).around(@line, *window_size)
      end

      def window_size
        if args.empty?
          pry_instance.config.default_window_size
        else
          args.map(&:to_i)
        end
      end
    end
  end
end
@andrehjr
Copy link
Member

Would you like to submit Pull Request @taka-ebato ?

@takaebato
Copy link
Author

@andrehjr
Thanks for your reply. I submitted PR #2229.
Would you mind reviewing it?

@andrehjr
Copy link
Member

Thanks for the PR @taka-ebato! I'll try to review it later today.

Happy new year :)

@andrehjr andrehjr reopened this Dec 31, 2021
@takaebato
Copy link
Author

@andrehjr Any time is okay, please take your time :) Happy new year!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants