From 42063de30df1d3c3215a78f199d99f4035a3083c Mon Sep 17 00:00:00 2001 From: Mark Delk Date: Thu, 26 Jun 2025 16:46:13 -0500 Subject: [PATCH] whereami: support -f to show the full file like pry --- lib/irb/command/whereami.rb | 9 ++++++--- lib/irb/workspace.rb | 11 ++++++++--- test/irb/test_command.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/irb/command/whereami.rb b/lib/irb/command/whereami.rb index c8439f121..fd60c30d3 100644 --- a/lib/irb/command/whereami.rb +++ b/lib/irb/command/whereami.rb @@ -6,10 +6,13 @@ module IRB module Command class Whereami < Base category "Context" - description "Show the source code around binding.irb again." + description "Show the source code around binding.irb again. `-f` shows the full file." + + def execute(arg) + code = irb_context.workspace.code_around_binding( + show_full_file: arg.split.first == "-f" + ) - def execute(_arg) - code = irb_context.workspace.code_around_binding if code puts code else diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index ced9d7866..63e8283fe 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -125,7 +125,7 @@ def filter_backtrace(bt) bt end - def code_around_binding + def code_around_binding(show_full_file: false) file, pos = @binding.source_location if defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file] @@ -141,8 +141,13 @@ def code_around_binding lines = Color.colorize_code(code).lines pos -= 1 - start_pos = [pos - 5, 0].max - end_pos = [pos + 5, lines.size - 1].min + if show_full_file + start_pos = 0 + end_pos = lines.size - 1 + else + start_pos = [pos - 5, 0].max + end_pos = [pos + 5, lines.size - 1].min + end line_number_fmt = Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD]) fmt = " %2s #{line_number_fmt}: %s" diff --git a/test/irb/test_command.rb b/test/irb/test_command.rb index fd98a5a1b..b24b557d5 100644 --- a/test/irb/test_command.rb +++ b/test/irb/test_command.rb @@ -595,6 +595,14 @@ def test_whereami_alias assert_empty err assert_match(/^From: .+ @ line \d+ :\n/, out) end + + def test_whereami_file + out, err = execute_lines( + "whereami -f\n", + ) + assert_empty err + assert_match(/^From: .+ @ line \d+ :\n\s+1:/, out) + end end class LsTest < CommandTestCase