2011-05-01 10:25:37 +00:00
class Pry
module DefaultCommands
2011-05-07 05:32:05 +00:00
Documentation = Pry :: CommandSet . new do
2011-05-01 10:25:37 +00:00
command " ri " , " View ri documentation. e.g `ri Array # each` " do | * args |
run " .ri " , * args
end
2011-10-15 07:51:56 +00:00
command " show-doc " , " Show the comments above METH. Type `show-doc --help` for more info. Aliases: \ ? " , :shellwords = > false do | * args |
2011-10-16 06:20:54 +00:00
opts , meth = parse_options! ( args , :method_object ) do | opt |
2011-09-10 20:22:47 +00:00
opt . banner unindent <<-USAGE
2011-10-15 07:03:41 +00:00
Usage : show - doc [ OPTIONS ] [ METH ]
2011-09-10 20:22:47 +00:00
Show the comments above method METH . Tries instance methods first and then methods by default .
e . g show - doc hello_method
USAGE
2011-05-19 15:53:44 +00:00
opt . on :f , :flood , " Do not use a pager to view text longer than one screen. "
2011-05-01 10:25:37 +00:00
end
2011-10-15 07:03:41 +00:00
raise Pry :: CommandError , " No documentation found. " if meth . doc . nil? || meth . doc . empty?
2011-05-01 10:25:37 +00:00
2011-10-15 07:03:41 +00:00
doc = process_comment_markup ( meth . doc , meth . source_type )
output . puts make_header ( meth , doc )
2011-10-26 04:23:56 +00:00
output . puts " #{ text . bold ( " Owner: " ) } #{ meth . owner || " N/A " } "
output . puts " #{ text . bold ( " Visibility: " ) } #{ meth . visibility } "
output . puts " #{ text . bold ( " Signature: " ) } #{ meth . signature } "
2011-10-15 07:03:41 +00:00
output . puts
2011-12-04 20:43:44 +00:00
if opts . present? ( :flood )
output . puts doc
else
stagger_output doc
end
2011-05-01 10:25:37 +00:00
end
2011-09-18 06:36:39 +00:00
alias_command " ? " , " show-doc "
2011-05-01 10:25:37 +00:00
2011-10-15 07:51:56 +00:00
command " stat " , " View method information and set _file_ and _dir_ locals. Type `stat --help` for more info. " , :shellwords = > false do | * args |
2011-05-01 10:25:37 +00:00
target = target ( )
2011-10-16 06:20:54 +00:00
opts , meth = parse_options! ( args , :method_object ) do | opt |
2011-09-10 20:22:47 +00:00
opt . banner unindent <<-USAGE
Usage : stat [ OPTIONS ] [ METH ]
Show method information for method METH and set _file_ and _dir_ locals .
e . g : stat hello_method
USAGE
2011-05-01 10:25:37 +00:00
end
2011-09-25 05:13:01 +00:00
output . puts unindent <<-EOS
Method Information :
- -
2011-09-25 06:53:08 +00:00
Name : #{meth.name}
2011-09-25 05:13:01 +00:00
Owner : #{meth.owner ? meth.owner : "Unknown"}
Visibility : #{meth.visibility}
2011-09-25 20:45:15 +00:00
Type : #{meth.is_a?(::Method) ? "Bound" : "Unbound"}
2011-09-25 05:13:01 +00:00
Arity : #{meth.arity}
Method Signature : #{meth.signature}
Source Location : #{meth.source_location ? meth.source_location.join(":") : "Not found."}
EOS
2011-05-01 10:25:37 +00:00
end
2011-12-29 12:54:31 +00:00
require 'pry/command_context'
2011-12-29 22:55:40 +00:00
command ( " gist " , " Gist a method or expression history to github. Type `gist --help` for more info. " , { :requires_gem = > " gist " , :shellwords = > false , :definition = > Pry :: CommandContext . new {
2011-12-29 12:54:31 +00:00
2011-12-30 11:05:36 +00:00
class << self
attr_accessor :opts
attr_accessor :content
attr_accessor :code_type
attr_accessor :input_ranges
end
2011-12-29 12:54:31 +00:00
2011-12-30 11:05:36 +00:00
def call ( * args )
require 'gist'
2011-12-29 12:54:31 +00:00
2011-12-30 11:11:41 +00:00
gather_options ( args )
2011-12-30 11:05:36 +00:00
process_options
perform_gist
end
2011-12-30 11:11:41 +00:00
def gather_options ( args )
2011-12-30 11:05:36 +00:00
self . input_ranges = [ ]
2011-12-30 11:11:41 +00:00
self . opts = parse_options! ( args ) do | opt |
2011-12-30 11:05:36 +00:00
opt . banner unindent <<-USAGE
2011-12-29 12:54:31 +00:00
Usage : gist [ OPTIONS ] [ METH ]
Gist method ( doc or source ) or input expression to github .
Ensure the ` gist ` gem is properly working before use . http : / / github . com / defunkt / gist for instructions .
e . g : gist - m my_method
e . g : gist - d my_method
e . g : gist - i 1 .. 10
USAGE
2011-12-30 11:05:36 +00:00
opt . on :d , :doc , " Gist a method's documentation. " , true
opt . on :m , :method , " Gist a method's source. " , true
opt . on :f , :file , " Gist a file. " , true
opt . on :p , :public , " Create a public gist (default: false) " , :default = > false
opt . on :l , :lines , " Only gist a subset of lines (only works with -m and -f) " , :optional = > true , :as = > Range , :default = > 1 .. - 1
opt . on :i , :in , " Gist entries from Pry's input expression history. Takes an index or range. " , :optional = > true ,
:as = > Range , :default = > - 5 .. - 1 do | range |
input_ranges << absolute_index_range ( range , _pry_ . input_array . length )
end
end
end
2011-12-30 11:11:41 +00:00
def process_options
if opts . present? ( :in )
in_option
elsif opts . present? ( :file )
file_option
elsif opts . present? ( :doc )
doc_option
elsif opts . present? ( :method )
method_option
end
end
2011-12-30 11:05:36 +00:00
def in_option
self . code_type = :ruby
self . content = " "
input_ranges . each do | range |
input_expressions = _pry_ . input_array [ range ] || [ ]
input_expressions . each_with_index . map do | code , index |
corrected_index = index + range . first
if code && code != " "
self . content << code
if code !~ / ; \ Z /
self . content << " #{ comment_expression_result_for_gist ( Pry . config . gist . inspecter . call ( _pry_ . output_array [ corrected_index ] ) ) } "
end
end
end
end
end
def file_option
whole_file = File . read ( File . expand_path ( opts [ :f ] ) )
if opts . present? ( :lines )
self . content = restrict_to_lines ( whole_file , opts [ :l ] )
else
self . content = whole_file
end
end
def doc_option
meth = get_method_or_raise ( opts [ :d ] , target , { } )
self . content = meth . doc
self . code_type = meth . source_type
text . no_color do
self . content = process_comment_markup ( self . content , self . code_type )
end
self . code_type = :plain
end
def method_option
meth = get_method_or_raise ( opts [ :m ] , target , { } )
method_source = meth . source
if opts . present? ( :lines )
self . content = restrict_to_lines ( method_source , opts [ :l ] )
else
self . content = method_source
end
self . code_type = meth . source_type
end
def perform_gist
type_map = { :ruby = > " rb " , :c = > " c " , :plain = > " plain " }
# prevent Gist from exiting the session on error
begin
extname = opts . present? ( :file ) ? " . #{ gist_file_extension ( opts [ :f ] ) } " : " . #{ type_map [ self . code_type ] } "
link = Gist . write ( [ :extension = > extname ,
:input = > self . content ] ,
! opts [ :p ] )
rescue SystemExit
end
if link
Gist . copy ( link )
output . puts " Gist created at #{ link } and added to clipboard. "
end
end
def restrict_to_lines ( content , lines )
line_range = one_index_range ( lines )
content . lines . to_a [ line_range ] . join
end
def gist_file_extension ( file_name )
file_name . split ( " . " ) . last
end
def comment_expression_result_for_gist ( result )
content = " "
result . lines . each_with_index do | line , index |
if index == 0
content << " # => #{ line } "
else
content << " # #{ line } "
end
end
content
end
} } )
2011-12-29 12:54:31 +00:00
end
2011-12-28 05:22:20 +00:00
end
2011-12-04 07:56:53 +00:00
end