Skip to content

Commit 6cfe385

Browse files
HeyNonsterbquorning
andcommitted
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 <[email protected]>
1 parent bad4d38 commit 6cfe385

File tree

3 files changed

+96
-32
lines changed

3 files changed

+96
-32
lines changed

lib/debug/session.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,15 @@ def register_default_command
739739

740740
# * `whereami`
741741
# * Show the current frame with source code.
742-
register_command 'whereami', unsafe: false do
743-
request_tc [:show, :whereami]
742+
# * `whereami -n`
743+
# * Show the current frame with source code without line numbers.
744+
register_command 'whereami', unsafe: false do |arg|
745+
case arg ? arg.strip : nil
746+
when "-n"
747+
request_tc [:show, :whereami, {no_lineno: true}]
748+
else
749+
request_tc [:show, :whereami]
750+
end
744751
end
745752

746753
# * `edit`

lib/debug/thread_client.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,13 @@ def get_src(frame,
466466
max_lines:,
467467
start_line: nil,
468468
end_line: nil,
469-
dir: +1)
469+
dir: +1,
470+
no_lineno: CONFIG[:no_lineno]
471+
)
470472
if file_lines = frame.file_lines
471473
frame_line = frame.location.lineno - 1
472474

473-
if CONFIG[:no_lineno]
475+
if no_lineno
474476
lines = file_lines
475477
else
476478
lines = file_lines.map.with_index do |e, i|
@@ -509,15 +511,15 @@ def get_src(frame,
509511
exit!
510512
end
511513

512-
def show_src(frame_index: @current_frame_index, update_line: false, ignore_show_line: false, max_lines: CONFIG[:show_src_lines], **options)
514+
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)
513515
if frame = get_frame(frame_index)
514516
begin
515517
if ignore_show_line
516518
prev_show_line = frame.show_line
517519
frame.show_line = nil
518520
end
519521

520-
start_line, end_line, lines = *get_src(frame, max_lines: max_lines, **options)
522+
start_line, end_line, lines = *get_src(frame, max_lines: max_lines, no_lineno: no_lineno, **options)
521523

522524
if start_line
523525
if update_line
@@ -1119,7 +1121,7 @@ def wait_next_action_
11191121
show_src(update_line: true, **(args.first || {}))
11201122

11211123
when :whereami
1122-
show_src ignore_show_line: true
1124+
show_src(ignore_show_line: true, **(args.first || {}))
11231125
show_frames CONFIG[:show_frames]
11241126

11251127
when :edit

test/console/whereami_test.rb

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ class WhereamiTest < ConsoleTestCase
77
def program
88
<<~RUBY
99
1| a = 1
10-
2| a = 1
11-
3| a = 1
12-
4| a = 1
13-
5| a = 1
14-
6| a = 1
15-
7| a = 1
16-
8| a = 1
17-
9| a = 1
18-
10| a = 1
19-
11| b = 1
20-
12| b = 1
21-
13| b = 1
22-
14| b = 1
23-
15| b = 1
24-
16| b = 1
25-
17| b = 1
26-
18| b = 1
27-
19| b = 1
28-
20| b = 1
29-
21| c = 1
10+
2| b = 1
11+
3| c = 1
12+
4| d = 1
13+
5| e = 1
14+
6| f = 1
15+
7| g = 1
16+
8| h = 1
17+
9| i = 1
18+
10| j = 1
19+
11| k = 1
20+
12| l = 1
21+
13| m = 1
22+
14| n = 1
23+
15| o = 1
24+
16| p = 1
25+
17| q = 1
26+
18| r = 1
27+
19| s = 1
28+
20| t = 1
29+
21| u = 1
3030
RUBY
3131
end
3232

@@ -36,20 +36,75 @@ def test_whereami_displays_current_frames_code
3636
type "list"
3737

3838
# after 2 list commands, we should advance to the next 10 lines and not able to see the current frame's source
39-
assert_no_line_text(/=> 1\| a = 1/)
40-
assert_line_text(/b = 1/)
39+
assert_no_line_text(/=> 10\| j = 1/)
40+
assert_line_text(/k = 1/)
4141

4242
type "whereami"
4343

4444
# with whereami, we should see the current frame's source but have no visual outside the closest 10 lines
45-
assert_no_line_text(/b = 1/)
45+
assert_no_line_text(/k = 1/)
4646
assert_line_text(/=> 1\| a = 1/)
4747

4848
type "list"
4949

5050
# list command should work as normal after whereami is executed
51-
assert_no_line_text(/b = 1/)
52-
assert_line_text(/c = 1/)
51+
assert_no_line_text(/t = 1/)
52+
assert_line_text(/u = 1/)
53+
54+
type "continue"
55+
end
56+
end
57+
58+
def test_whereami_n_argument_removes_line_numbers
59+
debug_code(program) do
60+
type "whereami"
61+
62+
assert_line_text([
63+
/1\| a = 1/,
64+
/2\| b = 1/,
65+
/3\| c = 1/,
66+
/4\| d = 1/,
67+
/5\| e = 1/,
68+
/6\| f = 1/,
69+
/7\| g = 1/,
70+
/8\| h = 1/,
71+
/9\| i = 1/,
72+
/10\| j = 1/
73+
])
74+
75+
type "whereami -n"
76+
77+
# with -n there should be no line markers at all
78+
assert_no_line_text(/\|/)
79+
assert_line_text([
80+
/a = 1/,
81+
/b = 1/,
82+
/c = 1/,
83+
/d = 1/,
84+
/e = 1/,
85+
/f = 1/,
86+
/g = 1/,
87+
/h = 1/,
88+
/i = 1/,
89+
/j = 1/
90+
])
91+
92+
93+
# whereami should work as normal again
94+
type "whereami"
95+
96+
assert_line_text([
97+
/1\| a = 1/,
98+
/2\| b = 1/,
99+
/3\| c = 1/,
100+
/4\| d = 1/,
101+
/5\| e = 1/,
102+
/6\| f = 1/,
103+
/7\| g = 1/,
104+
/8\| h = 1/,
105+
/9\| i = 1/,
106+
/10\| j = 1/
107+
])
53108

54109
type "continue"
55110
end

0 commit comments

Comments
 (0)