Skip to content

Commit

Permalink
Add backtrace, info, catch commands
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Nov 19, 2022
1 parent 64c0bea commit c347b4e
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The following commands are available on IRB.
* Show the source code around binding.irb again.
* `debug`
* Start the debugger of debug.gem.
* `break`, `delete`, `next`, `step`, `continue`, `finish`
* `break`, `delete`, `next`, `step`, `continue`, `finish`, `backtrace`, `info`, `catch`
* Start the debugger of debug.gem and run the command on it.

## Documentation
Expand Down
6 changes: 1 addition & 5 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
# * Show the source code around binding.irb again.
# * debug
# * Start the debugger of debug.gem.
# * `break`, `delete`, `next`, `step`, `continue`, `finish`
# * break, delete, next, step, continue, finish, backtrace, info, catch
# * Start the debugger of debug.gem and run the command on it.
#
# == Configuration
Expand Down Expand Up @@ -472,10 +472,6 @@ class Irb
def initialize(workspace = nil, input_method = nil)
@context = Context.new(self, workspace, input_method)
@context.main.extend ExtendCommandBundle
@context.command_aliases.each do |alias_name, cmd_name|
next if @context.symbol_alias?(alias_name) || @context.main.respond_to?(alias_name)
@context.main.install_alias_method(alias_name, cmd_name)
end
@signal_status = :IN_IRB
@scanner = RubyLex.new
end
Expand Down
21 changes: 21 additions & 0 deletions lib/irb/cmd/backtrace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative "debug"

module IRB
# :stopdoc:

module ExtendCommand
class Backtrace < Debug
def self.transform_args(args)
args&.dump
end

def execute(*args)
super(pre_cmds: ["backtrace", *args].join(" "))
end
end
end

# :startdoc:
end
21 changes: 21 additions & 0 deletions lib/irb/cmd/catch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative "debug"

module IRB
# :stopdoc:

module ExtendCommand
class Catch < Debug
def self.transform_args(args)
args&.dump
end

def execute(*args)
super(pre_cmds: ["catch", *args].join(" "))
end
end
end

# :startdoc:
end
31 changes: 9 additions & 22 deletions lib/irb/cmd/info.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
# frozen_string_literal: false
# frozen_string_literal: true

require_relative "nop"
require_relative "debug"

module IRB
# :stopdoc:

module ExtendCommand
class Info < Nop
def execute
Class.new {
def inspect
str = "Ruby version: #{RUBY_VERSION}\n"
str += "IRB version: #{IRB.version}\n"
str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n"
str += ".irbrc path: #{IRB.rc_file}\n" if File.exist?(IRB.rc_file)
str += "RUBY_PLATFORM: #{RUBY_PLATFORM}\n"
str += "LANG env: #{ENV["LANG"]}\n" if ENV["LANG"] && !ENV["LANG"].empty?
str += "LC_ALL env: #{ENV["LC_ALL"]}\n" if ENV["LC_ALL"] && !ENV["LC_ALL"].empty?
str += "East Asian Ambiguous Width: #{Reline.ambiguous_width.inspect}\n"
if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
codepage = `chcp`.b.sub(/.*: (\d+)\n/, '\1')
str += "Code page: #{codepage}\n"
end
str
end
alias_method :to_s, :inspect
}.new
class Info < Debug
def self.transform_args(args)
args&.dump
end

def execute(*args)
super(pre_cmds: ["info", *args].join(" "))
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/irb/cmd/irb_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: false

require_relative "nop"

module IRB
# :stopdoc:

module ExtendCommand
class IrbInfo < Nop
def execute
Class.new {
def inspect
str = "Ruby version: #{RUBY_VERSION}\n"
str += "IRB version: #{IRB.version}\n"
str += "InputMethod: #{IRB.CurrentContext.io.inspect}\n"
str += ".irbrc path: #{IRB.rc_file}\n" if File.exist?(IRB.rc_file)
str += "RUBY_PLATFORM: #{RUBY_PLATFORM}\n"
str += "LANG env: #{ENV["LANG"]}\n" if ENV["LANG"] && !ENV["LANG"].empty?
str += "LC_ALL env: #{ENV["LC_ALL"]}\n" if ENV["LC_ALL"] && !ENV["LC_ALL"].empty?
str += "East Asian Ambiguous Width: #{Reline.ambiguous_width.inspect}\n"
if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
codepage = `chcp`.b.sub(/.*: (\d+)\n/, '\1')
str += "Code page: #{codepage}\n"
end
str
end
alias_method :to_s, :inspect
}.new
end
end
end

# :startdoc:
end
14 changes: 13 additions & 1 deletion lib/irb/extend-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def irb_context
[
:irb_break, :Break, "cmd/break",
],
[
:irb_catch, :Catch, "cmd/catch",
],
[
:irb_next, :Next, "cmd/next",
],
Expand All @@ -142,14 +145,23 @@ def irb_context
:irb_finish, :Finish, "cmd/finish",
[:finish, NO_OVERRIDE],
],
[
:irb_backtrace, :Backtrace, "cmd/backtrace",
[:backtrace, NO_OVERRIDE],
[:bt, NO_OVERRIDE],
],
[
:irb_debug_info, :Info, "cmd/info",
[:info, NO_OVERRIDE],
],

[
:irb_help, :Help, "cmd/help",
[:help, NO_OVERRIDE],
],

[
:irb_info, :Info, "cmd/info"
:irb_info, :IrbInfo, "cmd/irb_info"
],

[
Expand Down
1 change: 1 addition & 0 deletions lib/irb/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def IRB.init_config(ap_path)
:'@' => :whereami,
# Keyword aliases
:break => :irb_break,
:catch => :irb_catch,
:next => :irb_next,
}
end
Expand Down
49 changes: 49 additions & 0 deletions test/irb/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,55 @@ def foo
EOC
end

def test_backtrace
write_ruby <<~'RUBY'
puts "start IRB"
def foo
binding.irb
end
foo
RUBY
start_terminal(25, 80, %W{ruby -I#{@pwd}/lib #{@ruby_file}}, startup_message: 'start IRB')
write("backtrace\n")
close
assert_include_screen(<<~EOC)
(rdbg:irb) backtrace
=>#0 Object#foo at #{@ruby_file}:3
#1 <main> at #{@ruby_file}:5
EOC
end

def test_info
write_ruby <<~'RUBY'
puts "start IRB"
a = 1
binding.irb
RUBY
start_terminal(25, 80, %W{ruby -I#{@pwd}/lib #{@ruby_file}}, startup_message: 'start IRB')
write("info\n")
close
assert_include_screen(<<~EOC)
(rdbg:irb) info
%self = main
a = 1
EOC
end

def test_catch
write_ruby <<~'RUBY'
puts "start IRB"
binding.irb
raise NotImplementedError
RUBY
start_terminal(25, 80, %W{ruby -I#{@pwd}/lib #{@ruby_file}}, startup_message: 'start IRB')
write("catch NotImplementedError\n")
write("continue\n")
close
assert_include_screen(<<~EOC)
Stop by #0 BP - Catch "NotImplementedError"
EOC
end

private

def assert_include_screen(expected)
Expand Down

0 comments on commit c347b4e

Please sign in to comment.