1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Depracated show-command and moved its functionality to show-source

Also, show-doc on commands now displays their banner
This commit is contained in:
Reginald Tan 2012-08-13 23:54:07 -04:00
parent 4577269779
commit 3836179181
9 changed files with 141 additions and 85 deletions

View file

@ -1,9 +1,12 @@
require 'pry/helpers/documentation_helpers'
class Pry
# The super-class of all commands, new commands should be created by calling
# {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
# which creates a ClassCommand. Please don't use this class directly.
class Command
extend Helpers::DocumentationHelpers
# represents a void return value for a command
VOID_VALUE = Object.new
@ -49,6 +52,14 @@ class Pry
def block
@block || instance_method(:process) && instance_method(:process)
end
def source
strip_leading_whitespace(block.source)
end
def source_location
block.source_location
end
end
# Make those properties accessible to instances
@ -58,6 +69,8 @@ class Pry
def block; self.class.block; end
def command_options; self.class.options; end
def command_name; command_options[:listing]; end
def source; self.class.source; end
def source_location; self.class.source_location; end
class << self
def name

View file

@ -8,40 +8,15 @@ class Pry
opts = Slop.parse!(args) do |opt|
opt.banner unindent <<-USAGE
Usage: show-command [OPTIONS] [CMD]
Show the source for command CMD.
e.g: show-command show-method
NOTE: show-command is DEPRACTED. Use show-source [command_name] instead.
USAGE
opt.on :l, "line-numbers", "Show line numbers."
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
opt.on :h, :help, "This message." do
output.puts opt.help
end
end
return if opts.present?(:help)
command_name = args.shift
if !command_name
raise CommandError, "You must provide a command name."
end
if find_command(command_name)
block = Pry::Method.new(find_command(command_name).block)
return unless block.source
set_file_and_dir_locals(block.source_file)
output.puts make_header(block)
output.puts
code = Pry::Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers')).to_s
render_output(code, opts)
else
raise CommandError, "No such command: #{command_name}."
end
render_output opts.banner
end
end
end

View file

@ -111,6 +111,24 @@ class Pry
doc
end
def process_command
name = args.join(" ").gsub(/\"/,"")
command = find_command(name)
doc = command.new.help
doc = strip_leading_whitespace(doc)
file_name, line = command.source_location
set_file_and_dir_locals(file_name)
result = ""
result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n"
result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{doc.lines.count}\n\n"
result << doc
result << "\n"
end
def module_start_line(mod, candidate=0)
if opts.present?(:'base-one')
1

View file

@ -111,6 +111,23 @@ class Pry
result
end
def process_command
name = args.join(" ").gsub(/\"/,"")
command = find_command(name)
file_name, line = command.source_location
set_file_and_dir_locals(file_name)
code = Pry::Code.new(command.source, line).with_line_numbers(use_line_numbers?).to_s
result = ""
result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n"
result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{code.lines.count}\n\n"
result << "\n"
result << code
end
def use_line_numbers?
opts.present?(:b) || opts.present?(:l)
end

View file

@ -33,13 +33,21 @@ class Pry
:module
elsif target.eval("defined? #{input} ") =~ /variable|constant/
:variable_or_constant
elsif find_command(input)
:command
else
:unknown
end
rescue SyntaxError
if find_command(input)
:command
else
:unknown
end
end
def process(name)
input = args.join(" ")
input = args.join(" ").gsub(/\"/,"")
type = input_type(input, target)
code_or_doc = case type
@ -53,8 +61,10 @@ class Pry
process_module
when :variable_or_constant
process_variable_or_constant
when :command
process_command
else
command_error("method or module for '#{input}' could not be found or derived", false)
command_error("method/module/command for '#{input}' could not be found or derived", false)
end
render_output(code_or_doc, opts)

View file

@ -619,8 +619,10 @@ describe "Pry::Command" do
mock_pry("my---test").should =~ /my-testmy-test/
end
it "should show the source of the process method" do
mock_pry("show-command my-test").should =~ /output.puts command_name/
if !mri18_and_no_real_source_location?
it "should show the source of the process method" do
mock_pry("show-source my-test").should =~ /output.puts command_name/
end
end
end

View file

@ -319,5 +319,35 @@ if !mri18_and_no_real_source_location?
end
end
describe "on commands" do
# mostly copied & modified from test_help.rb
before do
@oldset = Pry.config.commands
@set = Pry.config.commands = Pry::CommandSet.new do
import Pry::Commands
end
end
after do
Pry.config.commands = @oldset
end
it 'should display help for a specific command' do
mock_pry('show-doc ls').should =~ /Usage: ls/
end
it 'should display help for a regex command with a "listing"' do
@set.command /bar(.*)/, "Test listing", :listing => "foo" do; end
mock_pry('show-doc foo').should =~ /Test listing/
end
it 'should display help for a command with a spaces in its name' do
@set.command "command with spaces", "description of a command with spaces" do; end
mock_pry('show-doc "command with spaces"').should =~ /description of a command with spaces/
end
end
end
end

View file

@ -470,58 +470,4 @@ describe "Pry::DefaultCommands::Introspection" do
end
end
# show-command only works in implementations that support Proc#source_location
if Proc.method_defined?(:source_location)
describe "show-command" do
before do
@str_output = StringIO.new
end
it 'should show source for an ordinary command' do
set = Pry::CommandSet.new do
import_from Pry::Commands, "show-command"
command "foo" do
:body_of_foo
end
end
redirect_pry_io(InputTester.new("show-command foo"), @str_output) do
Pry.new(:commands => set).rep
end
@str_output.string.should =~ /:body_of_foo/
end
it 'should show source for a command with spaces in its name' do
set = Pry::CommandSet.new do
import_from Pry::Commands, "show-command"
command "foo bar" do
:body_of_foo_bar
end
end
redirect_pry_io(InputTester.new("show-command \"foo bar\""), @str_output) do
Pry.new(:commands => set).rep
end
@str_output.string.should =~ /:body_of_foo_bar/
end
it 'should show source for a command by listing name' do
set = Pry::CommandSet.new do
import_from Pry::Commands, "show-command"
command /foo(.*)/, "", :listing => "bar" do
:body_of_foo_regex
end
end
redirect_pry_io(InputTester.new("show-command bar"), @str_output) do
Pry.new(:commands => set).rep
end
@str_output.string.should =~ /:body_of_foo_regex/
end
end
end
end

View file

@ -553,6 +553,51 @@ if !mri18_and_no_real_source_location?
end
end
end
describe "on commands" do
before do
@oldset = Pry.config.commands
@set = Pry.config.commands = Pry::CommandSet.new do
import Pry::Commands
end
end
after do
Pry.config.commands = @oldset
end
it 'should show source for an ordinary command' do
@set.command "foo", :body_of_foo do; end
string = mock_pry("show-source foo")
string.should =~ /:body_of_foo/
end
it "should output source of commands using special characters" do
@set.command "!", "Clear the input buffer" do; end
string = mock_pry("show-source !")
string.should =~ /Clear the input buffer/
end
it 'should show source for a command with spaces in its name' do
@set.command "foo bar", :body_of_foo_bar do; end
string = mock_pry("show-source \"foo bar\"")
string.should =~ /:body_of_foo_bar/
end
it 'should show source for a command by listing name' do
@set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
string = mock_pry("show-source bar")
string.should =~ /:body_of_foo_bar_regex/
end
end
end
end