Restore groups, convert most commands to class syntax

This commit is contained in:
Ryan Fitzgerald 2012-08-11 18:26:59 -07:00
parent 8ce49ee081
commit 1af4207c63
38 changed files with 267 additions and 136 deletions

View File

@ -1,5 +1,6 @@
class Pry
Pry::Commands.create_command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/) do
group 'Editing'
description "Amend a line of input in multi-line mode."
command_options :interpolate => false, :listing => "amend-line"

View File

@ -1,5 +1,10 @@
class Pry
Pry::Commands.create_command "!", "Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop.", :use_prefix => false do
Pry::Commands.create_command "!" do
group 'Editing'
description "Clear the input buffer. Useful if the parsing process goes " \
"wrong and you get stuck in the read loop."
command_options :use_prefix => false
def process
output.puts "Input buffer cleared!"
eval_string.replace("")

View File

@ -1,5 +1,11 @@
class Pry
Pry::Commands.command "!pry", "Start a Pry session on current self; this even works mid multi-line expression." do
target.pry
Pry::Commands.create_command "!pry" do
group 'Navigating Pry'
description "Start a Pry session on current self; this even works mid " \
"multi-line expression."
def process
target.pry
end
end
end

View File

@ -1,5 +1,9 @@
class Pry
Pry::Commands.create_command "cat", "Show code from a file, Pry's input buffer, or the last exception." do
Pry::Commands.create_command "cat" do
group 'Input and Output'
description "Show code from a file, Pry's input buffer, or the last " \
"exception."
banner <<-USAGE
Usage: cat FILE
cat --ex [STACK_INDEX]

View File

@ -1,5 +1,6 @@
class Pry
Pry::Commands.create_command "edit" do
group 'Editing'
description "Invoke the default editor on a file."
banner <<-BANNER

View File

@ -1,5 +1,6 @@
class Pry
Pry::Commands.create_command "edit-method" do
group 'Editing'
description "Edit the source code for a method."
banner <<-BANNER

View File

@ -1,5 +1,6 @@
class Pry
Pry::Commands.create_command "exit" do
group 'Navigating Pry'
description "Pop the previous binding (does NOT exit program). Aliases: quit"
banner <<-BANNER

View File

@ -1,13 +1,19 @@
class Pry
Pry::Commands.command "exit-all", "End the current Pry session (popping all bindings) and returning to caller. Accepts optional return value. Aliases: !!@" do
# calculate user-given value
exit_value = target.eval(arg_string)
Pry::Commands.create_command "exit-all" do
group 'Navigating Pry'
description "End the current Pry session (popping all bindings) and " \
"returning to caller. Accepts optional return value. Aliases: !!@"
# clear the binding stack
_pry_.binding_stack.clear
def process
# calculate user-given value
exit_value = target.eval(arg_string)
# break out of the repl loop
throw(:breakout, exit_value)
# clear the binding stack
_pry_.binding_stack.clear
# break out of the repl loop
throw(:breakout, exit_value)
end
end
Pry::Commands.alias_command "!!@", "exit-all"

View File

@ -1,7 +1,12 @@
class Pry
Pry::Commands.command "exit-program", "End the current program. Aliases: quit-program, !!!" do
Pry.save_history if Pry.config.history.should_save
Kernel.exit target.eval(arg_string).to_i
Pry::Commands.create_command "exit-program" do
group 'Navigating Pry'
description "End the current program. Aliases: quit-program, !!!"
def process
Pry.save_history if Pry.config.history.should_save
Kernel.exit target.eval(arg_string).to_i
end
end
Pry::Commands.alias_command "quit-program", "exit-program"

View File

@ -1,5 +1,9 @@
class Pry
Pry::Commands.create_command "gem-cd", "Change working directory to specified gem's directory.", :argument_required => true do |gem|
Pry::Commands.create_command "gem-cd" do |gem|
group 'Gems'
description "Change working directory to specified gem's directory."
command_options :argument_required => true
banner <<-BANNER
Usage: gem-cd GEM_NAME

View File

@ -1,5 +1,9 @@
class Pry
Pry::Commands.create_command "gem-install", "Install a gem and refresh the gem cache.", :argument_required => true do |gem|
Pry::Commands.create_command "gem-install" do |gem|
group 'Gems'
description "Install a gem and refresh the gem cache."
command_options :argument_required => true
banner <<-BANNER
Usage: gem-install GEM_NAME

View File

@ -1,5 +1,8 @@
class Pry
Pry::Commands.create_command "gem-list", "List and search installed gems." do |pattern|
Pry::Commands.create_command "gem-list" do |pattern|
group 'Gems'
description "List and search installed gems."
banner <<-BANNER
Usage: gem-list [REGEX]

View File

@ -1,7 +1,11 @@
class Pry
Pry::Commands.create_command "gist", "Gist a method or expression history to GitHub.", :requires_gem => "jist" do
Pry::Commands.create_command "gist" do
include Pry::Helpers::DocumentationHelpers
group 'Misc'
description "Gist a method or expression history to GitHub."
command_options :requires_gem => 'jist', :shellwords => false
banner <<-USAGE
Usage: gist [OPTIONS] [METH]
Gist method (doc or source) or input expression to GitHub.
@ -17,8 +21,6 @@ class Pry
e.g: gist -m my_method --clip # Copy my_method source to clipboard, do not gist it.
USAGE
command_options :shellwords => false
attr_accessor :content
attr_accessor :filename

View File

@ -1,6 +1,7 @@
class Pry
Pry::Commands.create_command "hist", "Show and replay Readline history. Aliases: history" do
Pry::Commands.create_command "hist" do
group "Editing"
description "Show and replay Readline history. Aliases: history"
banner <<-USAGE
Usage: hist

View File

@ -1,6 +1,7 @@
class Pry
Pry::Commands.create_command "import-set", "Import a command set" do
Pry::Commands.create_command "import-set" do
group "Commands"
description "Import a command set."
def process(command_set_name)
raise CommandError, "Provide a command set name" if command_set.nil?

View File

@ -1,6 +1,7 @@
class Pry
Pry::Commands.create_command "install-command", "Install a disabled command." do |name|
Pry::Commands.create_command "install-command" do
group 'Commands'
description "Install a disabled command."
banner <<-BANNER
Usage: install-command COMMAND

View File

@ -1,17 +1,23 @@
class Pry
Pry::Commands.command "jump-to", "Jump to a binding further up the stack, popping all bindings below." do |break_level|
break_level = break_level.to_i
nesting_level = _pry_.binding_stack.size - 1
Pry::Commands.create_command "jump-to" do
group 'Navigating Pry'
description "Jump to a binding further up the stack, popping all " \
"bindings below."
case break_level
when nesting_level
output.puts "Already at nesting level #{nesting_level}"
when (0...nesting_level)
_pry_.binding_stack.slice!(break_level + 1, _pry_.binding_stack.size)
def process(break_level)
break_level = break_level.to_i
nesting_level = _pry_.binding_stack.size - 1
else
max_nest_level = nesting_level - 1
output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
case break_level
when nesting_level
output.puts "Already at nesting level #{nesting_level}"
when (0...nesting_level)
_pry_.binding_stack.slice!(break_level + 1, _pry_.binding_stack.size)
else
max_nest_level = nesting_level - 1
output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
end
end
end
end

View File

@ -1,8 +1,8 @@
class Pry
Pry::Commands.create_command "ls","Show the list of vars and methods in the current scope.",
:shellwords => false, :interpolate => false do
Pry::Commands.create_command "ls" do
group "Context"
description "Show the list of vars and methods in the current scope."
command_options :shellwords => false, :interpolate => false
def options(opt)
opt.banner unindent <<-USAGE

View File

@ -1,12 +1,17 @@
class Pry
Pry::Commands.command "nesting", "Show nesting information." do
output.puts "Nesting status:"
output.puts "--"
_pry_.binding_stack.each_with_index do |obj, level|
if level == 0
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))} (Pry top level)"
else
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))}"
Pry::Commands.create_command "nesting" do
group 'Navigating Pry'
description "Show nesting information."
def process
output.puts "Nesting status:"
output.puts "--"
_pry_.binding_stack.each_with_index do |obj, level|
if level == 0
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))} (Pry top level)"
else
output.puts "#{level}. #{Pry.view_clip(obj.eval('self'))}"
end
end
end
end

View File

@ -2,6 +2,7 @@ class Pry
Pry::Commands.create_command "play" do
include Pry::Helpers::DocumentationHelpers
group 'Editing'
description "Play back a string variable or a method or a file as input."
banner <<-BANNER

View File

@ -1,5 +1,8 @@
class Pry
Pry::Commands.create_command "pry-backtrace", "Show the backtrace for the Pry session." do
Pry::Commands.create_command "pry-backtrace" do
group 'Context'
description "Show the backtrace for the Pry session."
banner <<-BANNER
Usage: pry-backtrace [OPTIONS] [--help]

View File

@ -1,5 +1,10 @@
class Pry
Pry::Commands.command "pry-version", "Show Pry version." do
output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
Pry::Commands.create_command "pry-version" do
group 'Misc'
description "Show Pry version."
def process
output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
end
end
end

View File

@ -1,7 +1,10 @@
class Pry
# N.B. using a regular expresion here so that "raise-up 'foo'" does the right thing.
Pry::Commands.create_command(/raise-up(!?\b.*)/, :listing => 'raise-up') do
Pry::Commands.create_command(/raise-up(!?\b.*)/) do
group 'Context'
description "Raise an exception out of the current pry instance."
command_options :listing => 'raise-up'
banner <<-BANNER
Raise up, like exit, allows you to quit pry. Instead of returning a value however, it raises an exception.
If you don't provide the exception to be raised, it will use the most recent exception (in pry _ex_).

View File

@ -1,15 +1,20 @@
class Pry
Pry::Commands.command "reload-method", "Reload the source file that contains the specified method" do |meth_name|
meth = get_method_or_raise(meth_name, target, {}, :omit_help)
Pry::Commands.create_command "reload-method" do
group 'Misc'
description "Reload the source file that contains the specified method"
if meth.source_type == :c
raise CommandError, "Can't reload a C method."
elsif meth.dynamically_defined?
raise CommandError, "Can't reload an eval method."
else
file_name = meth.source_file
load file_name
output.puts "Reloaded #{file_name}."
def process(meth_name)
meth = get_method_or_raise(meth_name, target, {}, :omit_help)
if meth.source_type == :c
raise CommandError, "Can't reload a C method."
elsif meth.dynamically_defined?
raise CommandError, "Can't reload an eval method."
else
file_name = meth.source_file
load file_name
output.puts "Reloaded #{file_name}."
end
end
end
end

View File

@ -1,6 +1,11 @@
class Pry
Pry::Commands.command "reset", "Reset the REPL to a clean state." do
output.puts "Pry reset."
exec "pry"
Pry::Commands.create_command "reset" do
group 'Context'
description "Reset the REPL to a clean state."
def process
output.puts "Pry reset."
exec "pry"
end
end
end

View File

@ -1,5 +1,8 @@
class Pry
Pry::Commands.create_command "ri", "View ri documentation. e.g `ri Array#each`" do
Pry::Commands.create_command "ri" do
group 'Introspection'
description "View ri documentation. e.g `ri Array#each`"
banner <<-BANNER
Usage: ri [spec]
e.g. ri Array#each

View File

@ -1,5 +1,8 @@
class Pry
Pry::Commands.create_command "save-file", "Export to a file using content from the REPL." do
Pry::Commands.create_command "save-file" do
group 'Input and Output'
description "Export to a file using content from the REPL."
banner <<-USAGE
Usage: save-file [OPTIONS] [FILE]
Save REPL content to a file.

View File

@ -1,19 +1,26 @@
class Pry
Pry::Commands.command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>", :use_prefix => false, :takes_block => true) do |cmd|
if cmd =~ /^cd\s+(.+)/i
dest = $1
begin
Dir.chdir File.expand_path(dest)
rescue Errno::ENOENT
raise CommandError, "No such directory: #{dest}"
end
else
pass_block(cmd)
Pry::Commands.create_command(/\.(.*)/) do
group 'Input and Output'
description "All text following a '.' is forwarded to the shell."
command_options :listing => ".<shell command>", :use_prefix => false,
:takes_block => true
if command_block
command_block.call `#{cmd}`
def process(cmd)
if cmd =~ /^cd\s+(.+)/i
dest = $1
begin
Dir.chdir File.expand_path(dest)
rescue Errno::ENOENT
raise CommandError, "No such directory: #{dest}"
end
else
Pry.config.system.call(output, cmd, _pry_)
pass_block(cmd)
if command_block
command_block.call `#{cmd}`
else
Pry.config.system.call(output, cmd, _pry_)
end
end
end
end

View File

@ -1,14 +1,19 @@
class Pry
Pry::Commands.command "shell-mode", "Toggle shell mode. Bring in pwd prompt and file completion." do
case _pry_.prompt
when Pry::SHELL_PROMPT
_pry_.pop_prompt
_pry_.custom_completions = Pry::DEFAULT_CUSTOM_COMPLETIONS
else
_pry_.push_prompt Pry::SHELL_PROMPT
_pry_.custom_completions = Pry::FILE_COMPLETIONS
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target,
_pry_.instance_eval(&Pry::FILE_COMPLETIONS)
Pry::Commands.create_command "shell-mode" do
group 'Input and Output'
description "Toggle shell mode. Bring in pwd prompt and file completion."
def process
case _pry_.prompt
when Pry::SHELL_PROMPT
_pry_.pop_prompt
_pry_.custom_completions = Pry::DEFAULT_CUSTOM_COMPLETIONS
else
_pry_.push_prompt Pry::SHELL_PROMPT
_pry_.custom_completions = Pry::FILE_COMPLETIONS
Readline.completion_proc = Pry::InputCompleter.build_completion_proc target,
_pry_.instance_eval(&Pry::FILE_COMPLETIONS)
end
end
end

View File

@ -1,42 +1,47 @@
class Pry
Pry::Commands.command "show-command", "Show the source for CMD." do |*args|
target = target()
Pry::Commands.create_command "show-command" do
group 'Introspection'
description "Show the source for CMD."
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
USAGE
def process(*args)
target = target()
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
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
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
end
next if opts.present?(:help)
return if opts.present?(:help)
command_name = args.shift
if !command_name
raise CommandError, "You must provide a command name."
end
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)
if find_command(command_name)
block = Pry::Method.new(find_command(command_name).block)
next unless block.source
set_file_and_dir_locals(block.source_file)
return unless block.source
set_file_and_dir_locals(block.source_file)
output.puts make_header(block)
output.puts
output.puts make_header(block)
output.puts
code = Pry::Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers')).to_s
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}."
render_output(code, opts)
else
raise CommandError, "No such command: #{command_name}."
end
end
end
end

View File

@ -1,9 +1,14 @@
class Pry
Pry::Commands.create_command "show-doc", "Show the documentation for a method or class. Aliases: \?", :shellwords => false do
Pry::Commands.create_command "show-doc" do
include Pry::Helpers::ModuleIntrospectionHelpers
include Pry::Helpers::DocumentationHelpers
extend Pry::Helpers::BaseHelpers
group 'Introspection'
description "Show the documentation for a method or class. Aliases: \?"
command_options :shellwords => false
command_options :requires_gem => "ruby18_source_location" if mri_18?
banner <<-BANNER
Usage: show-doc [OPTIONS] [METH]
Aliases: ?
@ -14,8 +19,6 @@ class Pry
e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
BANNER
options :requires_gem => "ruby18_source_location" if mri_18?
def setup
require 'ruby18_source_location' if mri_18?
end

View File

@ -1,5 +1,8 @@
class Pry
Pry::Commands.create_command "show-input", "Show the contents of the input buffer for the current multi-line expression." do
Pry::Commands.create_command "show-input" do
group 'Editing'
description "Show the contents of the input buffer for the current multi-line expression."
def process
output.puts Code.new(eval_string).with_line_numbers
end

View File

@ -3,6 +3,7 @@ class Pry
include Pry::Helpers::ModuleIntrospectionHelpers
extend Pry::Helpers::BaseHelpers
group 'Introspection'
description "Show the source for a method or class. Aliases: $, show-method"
banner <<-BANNER

View File

@ -1,10 +1,15 @@
class Pry
Pry::Commands.command "simple-prompt", "Toggle the simple prompt." do
case _pry_.prompt
when Pry::SIMPLE_PROMPT
_pry_.pop_prompt
else
_pry_.push_prompt Pry::SIMPLE_PROMPT
Pry::Commands.create_command "simple-prompt" do
group 'Misc'
description "Toggle the simple prompt."
def process
case _pry_.prompt
when Pry::SIMPLE_PROMPT
_pry_.pop_prompt
else
_pry_.push_prompt Pry::SIMPLE_PROMPT
end
end
end
end

View File

@ -1,5 +1,9 @@
class Pry
Pry::Commands.create_command "stat", "View method information and set _file_ and _dir_ locals.", :shellwords => false do
Pry::Commands.create_command "stat" do
group 'Introspection'
description "View method information and set _file_ and _dir_ locals."
command_options :shellwords => false
banner <<-BANNER
Usage: stat [OPTIONS] [METH]
Show method information for method METH and set _file_ and _dir_ locals.

View File

@ -1,11 +1,17 @@
class Pry
Pry::Commands.command "switch-to", "Start a new sub-session on a binding in the current stack (numbered by nesting)." do |selection|
selection = selection.to_i
Pry::Commands.create_command "switch-to" do
group 'Navigating Pry'
description "Start a new sub-session on a binding in the current stack " \
"(numbered by nesting)."
if selection < 0 || selection > _pry_.binding_stack.size - 1
raise CommandError, "Invalid binding index #{selection} - use `nesting` command to view valid indices."
else
Pry.start(_pry_.binding_stack[selection])
def process(selection)
selection = selection.to_i
if selection < 0 || selection > _pry_.binding_stack.size - 1
raise CommandError, "Invalid binding index #{selection} - use `nesting` command to view valid indices."
else
Pry.start(_pry_.binding_stack[selection])
end
end
end
end

View File

@ -1,6 +1,11 @@
class Pry
Pry::Commands.command "toggle-color", "Toggle syntax highlighting." do
Pry.color = !Pry.color
output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
Pry::Commands.create_command "toggle-color" do
group 'Misc'
description "Toggle syntax highlighting."
def process
Pry.color = !Pry.color
output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
end
end
end

View File

@ -1,5 +1,7 @@
class Pry
Pry::Commands.create_command(/wtf([?!]*)/, "Show the backtrace of the most recent exception") do
Pry::Commands.create_command(/wtf([?!]*)/) do
group 'Context'
description "Show the backtrace of the most recent exception"
options :listing => 'wtf?'
banner <<-BANNER