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 class Pry
Pry::Commands.create_command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/) do Pry::Commands.create_command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/) do
group 'Editing'
description "Amend a line of input in multi-line mode." description "Amend a line of input in multi-line mode."
command_options :interpolate => false, :listing => "amend-line" command_options :interpolate => false, :listing => "amend-line"

View File

@ -1,5 +1,10 @@
class Pry 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 def process
output.puts "Input buffer cleared!" output.puts "Input buffer cleared!"
eval_string.replace("") eval_string.replace("")

View File

@ -1,5 +1,11 @@
class Pry class Pry
Pry::Commands.command "!pry", "Start a Pry session on current self; this even works mid multi-line expression." do Pry::Commands.create_command "!pry" do
target.pry 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
end end

View File

@ -1,5 +1,9 @@
class Pry 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 banner <<-USAGE
Usage: cat FILE Usage: cat FILE
cat --ex [STACK_INDEX] cat --ex [STACK_INDEX]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,9 @@
class Pry 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 banner <<-BANNER
Usage: gem-cd GEM_NAME Usage: gem-cd GEM_NAME

View File

@ -1,5 +1,9 @@
class Pry 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 banner <<-BANNER
Usage: gem-install GEM_NAME Usage: gem-install GEM_NAME

View File

@ -1,5 +1,8 @@
class Pry 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 banner <<-BANNER
Usage: gem-list [REGEX] Usage: gem-list [REGEX]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,8 @@
class Pry 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 banner <<-BANNER
Usage: pry-backtrace [OPTIONS] [--help] Usage: pry-backtrace [OPTIONS] [--help]

View File

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

View File

@ -1,7 +1,10 @@
class Pry class Pry
# N.B. using a regular expresion here so that "raise-up 'foo'" does the right thing. # 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." description "Raise an exception out of the current pry instance."
command_options :listing => 'raise-up'
banner <<-BANNER banner <<-BANNER
Raise up, like exit, allows you to quit pry. Instead of returning a value however, it raises an exception. 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_). 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 class Pry
Pry::Commands.command "reload-method", "Reload the source file that contains the specified method" do |meth_name| Pry::Commands.create_command "reload-method" do
meth = get_method_or_raise(meth_name, target, {}, :omit_help) group 'Misc'
description "Reload the source file that contains the specified method"
if meth.source_type == :c def process(meth_name)
raise CommandError, "Can't reload a C method." meth = get_method_or_raise(meth_name, target, {}, :omit_help)
elsif meth.dynamically_defined?
raise CommandError, "Can't reload an eval method." if meth.source_type == :c
else raise CommandError, "Can't reload a C method."
file_name = meth.source_file elsif meth.dynamically_defined?
load file_name raise CommandError, "Can't reload an eval method."
output.puts "Reloaded #{file_name}." else
file_name = meth.source_file
load file_name
output.puts "Reloaded #{file_name}."
end
end end
end end
end end

View File

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

View File

@ -1,5 +1,8 @@
class Pry 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 banner <<-BANNER
Usage: ri [spec] Usage: ri [spec]
e.g. ri Array#each e.g. ri Array#each

View File

@ -1,5 +1,8 @@
class Pry 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 banner <<-USAGE
Usage: save-file [OPTIONS] [FILE] Usage: save-file [OPTIONS] [FILE]
Save REPL content to a file. Save REPL content to a file.

View File

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

View File

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

View File

@ -1,42 +1,47 @@
class Pry class Pry
Pry::Commands.command "show-command", "Show the source for CMD." do |*args| Pry::Commands.create_command "show-command" do
target = target() group 'Introspection'
description "Show the source for CMD."
opts = Slop.parse!(args) do |opt| def process(*args)
opt.banner unindent <<-USAGE target = target()
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." opts = Slop.parse!(args) do |opt|
opt.on :f, :flood, "Do not use a pager to view text longer than one screen." opt.banner unindent <<-USAGE
opt.on :h, :help, "This message." do Usage: show-command [OPTIONS] [CMD]
output.puts opt.help 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
end
next if opts.present?(:help) return if opts.present?(:help)
command_name = args.shift command_name = args.shift
if !command_name if !command_name
raise CommandError, "You must provide a command name." raise CommandError, "You must provide a command name."
end end
if find_command(command_name) if find_command(command_name)
block = Pry::Method.new(find_command(command_name).block) block = Pry::Method.new(find_command(command_name).block)
next unless block.source return unless block.source
set_file_and_dir_locals(block.source_file) set_file_and_dir_locals(block.source_file)
output.puts make_header(block) output.puts make_header(block)
output.puts 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) render_output(code, opts)
else else
raise CommandError, "No such command: #{command_name}." raise CommandError, "No such command: #{command_name}."
end
end end
end end
end end

View File

@ -1,9 +1,14 @@
class Pry 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::ModuleIntrospectionHelpers
include Pry::Helpers::DocumentationHelpers include Pry::Helpers::DocumentationHelpers
extend Pry::Helpers::BaseHelpers 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 banner <<-BANNER
Usage: show-doc [OPTIONS] [METH] Usage: show-doc [OPTIONS] [METH]
Aliases: ? Aliases: ?
@ -14,8 +19,6 @@ class Pry
e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches) e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
BANNER BANNER
options :requires_gem => "ruby18_source_location" if mri_18?
def setup def setup
require 'ruby18_source_location' if mri_18? require 'ruby18_source_location' if mri_18?
end end

View File

@ -1,5 +1,8 @@
class Pry 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 def process
output.puts Code.new(eval_string).with_line_numbers output.puts Code.new(eval_string).with_line_numbers
end end

View File

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

View File

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

View File

@ -1,5 +1,9 @@
class Pry 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 banner <<-BANNER
Usage: stat [OPTIONS] [METH] Usage: stat [OPTIONS] [METH]
Show method information for method METH and set _file_ and _dir_ locals. Show method information for method METH and set _file_ and _dir_ locals.

View File

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

View File

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

View File

@ -1,5 +1,7 @@
class Pry 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?' options :listing => 'wtf?'
banner <<-BANNER banner <<-BANNER