mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Most command-related methods now accept either command name or listing name.
This should make it easier when aliasing / deleting / importing commands. No longer need to specify full regex. Also updated tests.
This commit is contained in:
parent
263d621625
commit
a3723baaab
3 changed files with 100 additions and 19 deletions
|
@ -153,12 +153,14 @@ class Pry
|
|||
# output.puts "parameter passed was #{n}"
|
||||
# end
|
||||
def before_command(name, &block)
|
||||
prev_block = commands[name].block
|
||||
cmd = find_command_by_name_or_listing(name)
|
||||
prev_block = cmd.block
|
||||
|
||||
wrapper_block = proc do |*args|
|
||||
instance_exec(*args, &block)
|
||||
instance_exec(*args, &prev_block)
|
||||
end
|
||||
commands[name].block = wrapper_block
|
||||
commands[cmd.name].block = wrapper_block
|
||||
end
|
||||
|
||||
# Execute a block of code after a command is invoked. The block also
|
||||
|
@ -171,12 +173,14 @@ class Pry
|
|||
# output.puts "command complete!"
|
||||
# end
|
||||
def after_command(name, &block)
|
||||
prev_block = commands[name].block
|
||||
cmd = find_command_by_name_or_listing(name)
|
||||
prev_block = cmd.block
|
||||
|
||||
wrapper_block = proc do |*args|
|
||||
instance_exec(*args, &prev_block)
|
||||
instance_exec(*args, &block)
|
||||
end
|
||||
commands[name].block = wrapper_block
|
||||
commands[cmd.name].block = wrapper_block
|
||||
end
|
||||
|
||||
def each &block
|
||||
|
@ -186,7 +190,10 @@ class Pry
|
|||
# Removes some commands from the set
|
||||
# @param [Array<String>] names name of the commands to remove
|
||||
def delete(*names)
|
||||
names.each { |name| commands.delete name }
|
||||
names.each do |name|
|
||||
cmd = find_command_by_name_or_listing(name)
|
||||
commands.delete cmd.name
|
||||
end
|
||||
end
|
||||
|
||||
# Imports all the commands from one or more sets.
|
||||
|
@ -204,15 +211,31 @@ class Pry
|
|||
# @param [Array<String>] names Commands to import
|
||||
def import_from(set, *names)
|
||||
helper_module.send :include, set.helper_module
|
||||
names.each { |name| commands[name] = set.commands[name] }
|
||||
names.each do |name|
|
||||
cmd = set.find_command_by_name_or_listing(name)
|
||||
commands[cmd.name] = cmd
|
||||
end
|
||||
end
|
||||
|
||||
def find_command_by_name_or_listing(name_or_listing)
|
||||
if commands[name_or_listing]
|
||||
cmd = commands[name_or_listing]
|
||||
else
|
||||
_, cmd = commands.find { |name, command| command.options[:listing] == name_or_listing }
|
||||
end
|
||||
|
||||
raise ArgumentError, "Cannot find a command with name: '#{name_or_listing}'!" if !cmd
|
||||
cmd
|
||||
end
|
||||
protected :find_command_by_name_or_listing
|
||||
|
||||
# Aliases a command
|
||||
# @param [String] new_name New name of the command.
|
||||
# @param [String] old_name Old name of the command.
|
||||
# @param [String, nil] desc New description of the command.
|
||||
def alias_command(new_name, old_name, desc="")
|
||||
commands[new_name] = commands[old_name].dup
|
||||
orig_command = find_command_by_name_or_listing(old_name)
|
||||
commands[new_name] = orig_command.dup
|
||||
commands[new_name].name = new_name
|
||||
commands[new_name].description = desc
|
||||
end
|
||||
|
@ -238,16 +261,22 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# Sets the description for a command (replacing the old
|
||||
# description.)
|
||||
# @param [String] name The command name.
|
||||
# Sets or gets the description for a command (replacing the old
|
||||
# description). Returns current description if no description
|
||||
# parameter provided.
|
||||
# @param [String, Regexp] name The command name.
|
||||
# @param [String] description The command description.
|
||||
# @example
|
||||
# @example Setting
|
||||
# MyCommands = Pry::CommandSet.new do
|
||||
# desc "help", "help description"
|
||||
# end
|
||||
def desc(name, description)
|
||||
commands[name].description = description
|
||||
# @example Getting
|
||||
# Pry.config.commands.desc "amend-line"
|
||||
def desc(name, description=nil)
|
||||
cmd = find_command_by_name_or_listing(name)
|
||||
return cmd.description if !description
|
||||
|
||||
cmd.description = description
|
||||
end
|
||||
|
||||
# Defines helpers methods for this command sets.
|
||||
|
|
|
@ -50,6 +50,15 @@ describe Pry::CommandSet do
|
|||
}.should.raise(Pry::NoCommandError)
|
||||
end
|
||||
|
||||
it 'should be able to remove its own commands, by listing name' do
|
||||
@set.command(/^foo1/, 'desc', :listing => 'foo') {}
|
||||
@set.delete 'foo'
|
||||
|
||||
lambda {
|
||||
@set.run_command @ctx, /^foo1/
|
||||
}.should.raise(Pry::NoCommandError)
|
||||
end
|
||||
|
||||
it 'should be able to import some commands from other sets' do
|
||||
run = false
|
||||
|
||||
|
@ -68,6 +77,19 @@ describe Pry::CommandSet do
|
|||
}.should.raise(Pry::NoCommandError)
|
||||
end
|
||||
|
||||
it 'should be able to import some commands from other sets using listing name' do
|
||||
run = false
|
||||
|
||||
other_set = Pry::CommandSet.new do
|
||||
command(/^foo1/, 'desc', :listing => 'foo') { run = true }
|
||||
end
|
||||
|
||||
@set.import_from(other_set, 'foo')
|
||||
|
||||
@set.run_command @ctx, /^foo1/
|
||||
run.should == true
|
||||
end
|
||||
|
||||
it 'should be able to import a whole set' do
|
||||
run = []
|
||||
|
||||
|
@ -108,6 +130,18 @@ describe Pry::CommandSet do
|
|||
run.should == true
|
||||
end
|
||||
|
||||
it "should be able to alias a method by the command's listing name" do
|
||||
run = false
|
||||
@set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }
|
||||
|
||||
@set.alias_command 'bar', 'foo'
|
||||
@set.commands['bar'].name.should == 'bar'
|
||||
@set.commands['bar'].description.should == ''
|
||||
|
||||
@set.run_command @ctx, 'bar'
|
||||
run.should == true
|
||||
end
|
||||
|
||||
it 'should be able to change the descriptions of methods' do
|
||||
@set.command('foo', 'bar') {}
|
||||
@set.desc 'foo', 'baz'
|
||||
|
@ -238,6 +272,15 @@ describe Pry::CommandSet do
|
|||
foo.should == [2, 1]
|
||||
end
|
||||
|
||||
it 'should be called before the original command, using listing name' do
|
||||
foo = []
|
||||
@set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
|
||||
@set.before_command('foo') { foo << 2 }
|
||||
@set.run_command(@ctx, /^foo1/)
|
||||
|
||||
foo.should == [2, 1]
|
||||
end
|
||||
|
||||
it 'should share the context with the original command' do
|
||||
@ctx.target = "test target string"
|
||||
before_val = nil
|
||||
|
@ -273,6 +316,15 @@ describe Pry::CommandSet do
|
|||
foo.should == [1, 2]
|
||||
end
|
||||
|
||||
it 'should be called after the original command, using listing name' do
|
||||
foo = []
|
||||
@set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
|
||||
@set.after_command('foo') { foo << 2 }
|
||||
@set.run_command(@ctx, /^foo1/)
|
||||
|
||||
foo.should == [1, 2]
|
||||
end
|
||||
|
||||
it 'should share the context with the original command' do
|
||||
@ctx.target = "test target string"
|
||||
after_val = nil
|
||||
|
|
|
@ -1282,8 +1282,8 @@ describe Pry do
|
|||
it 'should set the hooks default, and the default should be overridable' do
|
||||
Pry.input = InputTester.new("exit-all")
|
||||
Pry.hooks = Pry::Hooks.new.
|
||||
add_hook(:before_session) { |out,_,_| out.puts "HELLO" }.
|
||||
add_hook(:after_session) { |out,_,_| out.puts "BYE" }
|
||||
add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }.
|
||||
add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" }
|
||||
|
||||
str_output = StringIO.new
|
||||
Pry.new(:output => str_output).repl
|
||||
|
@ -1295,8 +1295,8 @@ describe Pry do
|
|||
str_output = StringIO.new
|
||||
Pry.new(:output => str_output,
|
||||
:hooks => Pry::Hooks.new.
|
||||
add_hook( :before_session) { |out,_,_| out.puts "MORNING" }.
|
||||
add_hook(:after_session) { |out,_,_| out.puts "EVENING" }
|
||||
add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }.
|
||||
add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" }
|
||||
).repl
|
||||
|
||||
str_output.string.should =~ /MORNING/
|
||||
|
@ -1307,7 +1307,7 @@ describe Pry do
|
|||
str_output = StringIO.new
|
||||
Pry.new(:output => str_output,
|
||||
:hooks => Pry::Hooks.new.
|
||||
add_hook(:before_session) { |out,_,_| out.puts "OPEN" }
|
||||
add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" }
|
||||
).repl
|
||||
|
||||
str_output.string.should =~ /OPEN/
|
||||
|
@ -1316,7 +1316,7 @@ describe Pry do
|
|||
str_output = StringIO.new
|
||||
Pry.new(:output => str_output,
|
||||
:hooks => Pry::Hooks.new.
|
||||
add_hook(:after_session) { |out,_,_| out.puts "CLOSE" }
|
||||
add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" }
|
||||
).repl
|
||||
|
||||
str_output.string.should =~ /CLOSE/
|
||||
|
|
Loading…
Add table
Reference in a new issue