pry--pry/lib/pry/default_commands/input.rb

115 lines
4.0 KiB
Ruby
Raw Normal View History

class Pry
module DefaultCommands
2011-05-07 05:32:05 +00:00
Input = Pry::CommandSet.new do
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!"
eval_string.replace("")
end
command "show-input", "Show the current eval_string" do
render_output(false, 0, Pry.color ? CodeRay.scan(eval_string, :ruby).term : eval_string)
end
command /amend-line-?(\d+)?/, "Experimental amend-line, where the N in amend-line-N represents line to replace. Aliases: %N",
:interpolate => false, :listing => "amend-line-N" do |line_number, replacement_line|
replacement_line = "" if !replacement_line
input_array = eval_string.each_line.to_a
line_num = line_number ? line_number.to_i : input_array.size - 1
input_array[line_num] = arg_string + "\n"
eval_string.replace input_array.join
end
alias_command /%(\d+)?/, /amend-line-?(\d+)?/, ""
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|
history = Readline::HISTORY.to_a
opt.banner "Usage: hist [--replay START..END] [--clear] [--grep PATTERN] [--head N] [--tail N] [--help]\n"
2011-05-04 18:26:27 +00:00
opt.on :g, :grep, 'A pattern to match against the history.', true do |pattern|
pattern = Regexp.new arg_string.split(/ /)[1]
2011-05-04 18:26:27 +00:00
history.pop
history.map!.with_index do |element, index|
if element =~ pattern
"#{text.blue index}: #{element}"
end
end
stagger_output history.compact.join "\n"
2011-05-04 18:26:27 +00:00
end
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
lines = text.with_line_numbers list.join("\n"), 0
2011-05-15 10:17:45 +00:00
stagger_output lines
end
end
opt.on :t, :tail, 'Display the last N items of history', :optional => true, :as => Integer do |limit|
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
opt.on :e, :exclude, 'Exclude pry commands from the history.' do
unless opt.grep?
history.map!.with_index do |element, index|
unless command_processor.valid_command? element
"#{text.blue index}: #{element}"
end
2011-05-09 06:07:04 +00:00
end
stagger_output history.compact.join "\n"
2011-05-09 06:07:04 +00:00
end
end
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?
actions = Array(history[range]).join("\n") + "\n"
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?
Readline::HISTORY.shift until Readline::HISTORY.empty?
output.puts 'History cleared.'
end
end
opt.on :h, :help, 'Show this message.', :tail => true do
2011-05-04 20:37:33 +00:00
unless opt.grep?
output.puts opt.help
end
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
end
end
end
end
end