Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/irb/command/whereami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/irb/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions test/irb/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading