From 6cfe3856d845630d87eaedf385a01a95dad3bcec Mon Sep 17 00:00:00 2001 From: Nony Dutton Date: Thu, 15 Jan 2026 12:45:13 +0100 Subject: [PATCH] Allow for passing `-n` option to `whereami` `pry`'s implementation of `whereami` allows for passing quite a few arguments: ``` whereami --help 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. -q, --quiet Don't display anything in case of an error -n, --no-line-numbers Do not display line numbers -m, --method Show the complete source for the current method. -c, --class Show the complete source for the current class or module. -f, --file Show the complete source for the current file. -h, --help Show this message. ``` This commit is an attempt to work toward feature parity with `pry` by first implementing `-n` as an option to remove line numbers from the output. In doing so, we've added a `no_lineno` kwarg to `get_src` and `show_src`, defaulting to `CONFIG[:no_lineno]` (which was previously hardcoded inside `get_src`). This ensures that the default behavior remains the same but gives us the option of removing line numbers with an argument for `whereami` and possibly other commands in the future. Co-authored-by: Benjamin Quorning --- lib/debug/session.rb | 11 +++- lib/debug/thread_client.rb | 12 ++-- test/console/whereami_test.rb | 105 ++++++++++++++++++++++++++-------- 3 files changed, 96 insertions(+), 32 deletions(-) diff --git a/lib/debug/session.rb b/lib/debug/session.rb index 3a101d6ee..66ceeb67f 100644 --- a/lib/debug/session.rb +++ b/lib/debug/session.rb @@ -739,8 +739,15 @@ def register_default_command # * `whereami` # * Show the current frame with source code. - register_command 'whereami', unsafe: false do - request_tc [:show, :whereami] + # * `whereami -n` + # * Show the current frame with source code without line numbers. + register_command 'whereami', unsafe: false do |arg| + case arg ? arg.strip : nil + when "-n" + request_tc [:show, :whereami, {no_lineno: true}] + else + request_tc [:show, :whereami] + end end # * `edit` diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb index 0a92fe356..7bfabe0d6 100644 --- a/lib/debug/thread_client.rb +++ b/lib/debug/thread_client.rb @@ -466,11 +466,13 @@ def get_src(frame, max_lines:, start_line: nil, end_line: nil, - dir: +1) + dir: +1, + no_lineno: CONFIG[:no_lineno] + ) if file_lines = frame.file_lines frame_line = frame.location.lineno - 1 - if CONFIG[:no_lineno] + if no_lineno lines = file_lines else lines = file_lines.map.with_index do |e, i| @@ -509,7 +511,7 @@ def get_src(frame, exit! end - def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_line: false, max_lines: CONFIG[:show_src_lines], **options) + def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_line: false, max_lines: CONFIG[:show_src_lines], no_lineno: CONFIG[:no_lineno], **options) if frame = get_frame(frame_index) begin if ignore_show_line @@ -517,7 +519,7 @@ def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_ frame.show_line = nil end - start_line, end_line, lines = *get_src(frame, max_lines: max_lines, **options) + start_line, end_line, lines = *get_src(frame, max_lines: max_lines, no_lineno: no_lineno, **options) if start_line if update_line @@ -1119,7 +1121,7 @@ def wait_next_action_ show_src(update_line: true, **(args.first || {})) when :whereami - show_src ignore_show_line: true + show_src(ignore_show_line: true, **(args.first || {})) show_frames CONFIG[:show_frames] when :edit diff --git a/test/console/whereami_test.rb b/test/console/whereami_test.rb index 3d937fdea..5a6b142c5 100644 --- a/test/console/whereami_test.rb +++ b/test/console/whereami_test.rb @@ -7,26 +7,26 @@ class WhereamiTest < ConsoleTestCase def program <<~RUBY 1| a = 1 - 2| a = 1 - 3| a = 1 - 4| a = 1 - 5| a = 1 - 6| a = 1 - 7| a = 1 - 8| a = 1 - 9| a = 1 - 10| a = 1 - 11| b = 1 - 12| b = 1 - 13| b = 1 - 14| b = 1 - 15| b = 1 - 16| b = 1 - 17| b = 1 - 18| b = 1 - 19| b = 1 - 20| b = 1 - 21| c = 1 + 2| b = 1 + 3| c = 1 + 4| d = 1 + 5| e = 1 + 6| f = 1 + 7| g = 1 + 8| h = 1 + 9| i = 1 + 10| j = 1 + 11| k = 1 + 12| l = 1 + 13| m = 1 + 14| n = 1 + 15| o = 1 + 16| p = 1 + 17| q = 1 + 18| r = 1 + 19| s = 1 + 20| t = 1 + 21| u = 1 RUBY end @@ -36,20 +36,75 @@ def test_whereami_displays_current_frames_code type "list" # after 2 list commands, we should advance to the next 10 lines and not able to see the current frame's source - assert_no_line_text(/=> 1\| a = 1/) - assert_line_text(/b = 1/) + assert_no_line_text(/=> 10\| j = 1/) + assert_line_text(/k = 1/) type "whereami" # with whereami, we should see the current frame's source but have no visual outside the closest 10 lines - assert_no_line_text(/b = 1/) + assert_no_line_text(/k = 1/) assert_line_text(/=> 1\| a = 1/) type "list" # list command should work as normal after whereami is executed - assert_no_line_text(/b = 1/) - assert_line_text(/c = 1/) + assert_no_line_text(/t = 1/) + assert_line_text(/u = 1/) + + type "continue" + end + end + + def test_whereami_n_argument_removes_line_numbers + debug_code(program) do + type "whereami" + + assert_line_text([ + /1\| a = 1/, + /2\| b = 1/, + /3\| c = 1/, + /4\| d = 1/, + /5\| e = 1/, + /6\| f = 1/, + /7\| g = 1/, + /8\| h = 1/, + /9\| i = 1/, + /10\| j = 1/ + ]) + + type "whereami -n" + + # with -n there should be no line markers at all + assert_no_line_text(/\|/) + assert_line_text([ + /a = 1/, + /b = 1/, + /c = 1/, + /d = 1/, + /e = 1/, + /f = 1/, + /g = 1/, + /h = 1/, + /i = 1/, + /j = 1/ + ]) + + + # whereami should work as normal again + type "whereami" + + assert_line_text([ + /1\| a = 1/, + /2\| b = 1/, + /3\| c = 1/, + /4\| d = 1/, + /5\| e = 1/, + /6\| f = 1/, + /7\| g = 1/, + /8\| h = 1/, + /9\| i = 1/, + /10\| j = 1/ + ]) type "continue" end