2011-05-01 10:25:37 +00:00
class Pry
module DefaultCommands
2011-05-07 05:32:05 +00:00
Input = Pry :: CommandSet . new do
2011-05-01 10:25:37 +00:00
command " ! " , " Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop. " do
output . puts " Input buffer cleared! "
2011-05-31 08:45:42 +00:00
eval_string . replace ( " " )
2011-05-01 10:25:37 +00:00
end
2011-05-27 16:18:34 +00:00
command " show-input " , " Show the current eval_string " do
render_output ( false , 0 , Pry . color ? CodeRay . scan ( eval_string , :ruby ) . term : eval_string )
end
2011-06-12 06:14:23 +00:00
command ( / amend-line-?( \ d+)?(?: \ . \ .(-? \ d+))? / , " Amend a line of input in multi-line mode. `amend-line-N`, where the N in `amend-line-N` represents line to replace. \n Can also specify a range of lines using `amend-line-N..M` syntax. Passing '!' as replacement content deletes the line(s) instead. Aliases: %N \n e.g amend-line-1 puts 'hello world!' \n e.g amend-line-1..4 ! \n " ,
:interpolate = > false , :listing = > " amend-line-N " ) do | start_line_number , end_line_number , replacement_line |
2011-05-05 10:06:56 +00:00
replacement_line = " " if ! replacement_line
2011-05-27 16:18:34 +00:00
input_array = eval_string . each_line . to_a
2011-06-12 06:14:23 +00:00
end_line_number = start_line_number . to_i if ! end_line_number
line_range = start_line_number ? ( start_line_number . to_i .. end_line_number . to_i ) : input_array . size - 1
# delete selected lines if replacement line is '!'
if arg_string == " ! "
input_array . slice! ( line_range )
else
input_array [ line_range ] = arg_string + " \n "
end
2011-05-27 16:18:34 +00:00
eval_string . replace input_array . join
2011-05-05 10:06:56 +00:00
end
2011-06-12 06:14:23 +00:00
alias_command ( / %( \ d+)?(?: \ . \ .(-? \ d+))? / , / amend-line-?( \ d+)?(?: \ . \ .(-? \ d+))? / , " " )
2011-05-05 10:06:56 +00:00
2011-05-01 10:25:37 +00:00
command " hist " , " Show and replay Readline history. Type `hist --help` for more info. " do | * args |
2011-05-04 17:41:02 +00:00
Slop . parse ( args ) do | opt |
2011-05-20 06:39:45 +00:00
history = Readline :: HISTORY . to_a
2011-05-15 10:50:00 +00:00
opt . banner " Usage: hist [--replay START..END] [--clear] [--grep PATTERN] [--head N] [--tail N] [--help] \n "
2011-05-01 10:25:37 +00:00
2011-05-04 18:26:27 +00:00
opt . on :g , :grep , 'A pattern to match against the history.' , true do | pattern |
2011-05-28 16:02:11 +00:00
pattern = Regexp . new arg_string . split ( / / ) [ 1 ]
2011-05-04 18:26:27 +00:00
history . pop
2011-05-05 10:06:56 +00:00
2011-05-10 12:21:17 +00:00
history . map! . with_index do | element , index |
2011-05-04 23:45:38 +00:00
if element =~ pattern
2011-05-10 12:21:17 +00:00
" #{ text . blue index } : #{ element } "
2011-05-04 23:45:38 +00:00
end
end
2011-05-10 12:21:17 +00:00
stagger_output history . compact . join " \n "
2011-05-04 18:26:27 +00:00
end
2011-05-01 10:25:37 +00:00
2011-05-20 06:39:45 +00:00
opt . on :head , 'Display the first N items of history' , :optional = > true , :as = > Integer do | limit |
2011-05-15 10:17:45 +00:00
unless opt . grep?
2011-05-16 21:59:57 +00:00
limit || = 10
2011-05-15 10:17:45 +00:00
list = history . first limit
2011-05-20 06:39:45 +00:00
lines = text . with_line_numbers list . join ( " \n " ) , 0
2011-05-15 10:17:45 +00:00
stagger_output lines
end
end
2011-05-15 10:50:00 +00:00
opt . on :t , :tail , 'Display the last N items of history' , :optional = > true , :as = > Integer do | limit |
2011-05-20 06:39:45 +00:00
unless opt . grep?
limit || = 10
2011-05-15 10:17:45 +00:00
offset = history . size - limit
offset = offset < 0 ? 0 : offset
list = history . last limit
lines = text . with_line_numbers list . join ( " \n " ) , offset
stagger_output lines
end
end
2011-05-29 15:42:36 +00:00
opt . on :s , :show , 'Show the history corresponding to the history line (or range of lines).' , true , :as = > Range do | range |
unless opt . grep?
start_line = range . is_a? ( Range ) ? range . first : range
lines = text . with_line_numbers Array ( history [ range ] ) . join ( " \n " ) , start_line
stagger_output lines
end
end
2011-05-28 15:41:15 +00:00
opt . on :e , :exclude , 'Exclude pry commands from the history.' do
2011-05-09 06:25:16 +00:00
unless opt . grep?
2011-05-10 12:21:17 +00:00
history . map! . with_index do | element , index |
2011-05-09 06:25:16 +00:00
unless command_processor . valid_command? element
2011-05-10 12:21:17 +00:00
" #{ text . blue index } : #{ element } "
2011-05-09 06:25:16 +00:00
end
2011-05-09 06:07:04 +00:00
end
2011-05-10 12:21:17 +00:00
stagger_output history . compact . join " \n "
2011-05-09 06:07:04 +00:00
end
end
2011-05-04 18:46:49 +00:00
opt . on :r , :replay , 'The line (or range of lines) to replay.' , true , :as = > Range do | range |
2011-05-04 20:37:33 +00:00
unless opt . grep?
2011-05-04 23:16:56 +00:00
actions = Array ( history [ range ] ) . join ( " \n " ) + " \n "
2011-05-04 18:46:49 +00:00
Pry . active_instance . input = StringIO . new ( actions )
end
end
opt . on :c , :clear , 'Clear the history' do
2011-05-04 20:37:33 +00:00
unless opt . grep?
2011-05-25 02:07:29 +00:00
Readline :: HISTORY . shift until Readline :: HISTORY . empty?
2011-05-04 18:46:49 +00:00
output . puts 'History cleared.'
end
end
2011-05-01 10:25:37 +00:00
opt . on :h , :help , 'Show this message.' , :tail = > true do
2011-05-04 20:37:33 +00:00
unless opt . grep?
2011-05-04 23:16:56 +00:00
output . puts opt . help
2011-05-04 18:46:49 +00:00
end
2011-05-01 10:25:37 +00:00
end
2011-05-04 17:55:34 +00:00
opt . on_empty do
2011-05-15 10:17:45 +00:00
lines = text . with_line_numbers history . join ( " \n " ) , 0
stagger_output lines
2011-05-04 17:55:34 +00:00
end
2011-05-01 10:25:37 +00:00
end
end
end
end
end