mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
0320ae8471
At the moment Pry::Hooks are not local to each instance, so the hook was only being added once. This caused problems when you opened two binding.pry's in one program, as the second one's watch expressions appeared to be ignored.
94 lines
2.5 KiB
Ruby
94 lines
2.5 KiB
Ruby
class Pry
|
|
class Command::WatchExpression < Pry::ClassCommand
|
|
require 'pry/commands/watch_expression/expression.rb'
|
|
|
|
match 'watch'
|
|
group 'Context'
|
|
description 'Evaluate an expression after every command and display it when its value changes.'
|
|
command_options :use_prefix => false
|
|
|
|
banner <<-'BANNER'
|
|
Usage: watch [EXPRESSION]
|
|
watch
|
|
watch --delete [INDEX]
|
|
|
|
Evaluate an expression after every command and display it when its value changes.
|
|
BANNER
|
|
|
|
def options(opt)
|
|
opt.on :d, :delete,
|
|
"Delete the watch expression with the given index. If no index is given; clear all watch expressions.",
|
|
:optional_argument => true, :as => Integer
|
|
opt.on :l, :list,
|
|
"Show all current watch expressions and their values. Calling watch with no expressions or options will also show the watch expressions."
|
|
end
|
|
|
|
def process
|
|
case
|
|
when opts.present?(:delete)
|
|
delete opts[:delete]
|
|
when opts.present?(:list) || args.empty?
|
|
list
|
|
else
|
|
add_hook
|
|
add_expression(args)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def expressions
|
|
Pry.config.watch_expressions ||= []
|
|
end
|
|
|
|
def delete(index)
|
|
if index
|
|
output.puts "Deleting watch expression ##{index}: #{expressions[index-1]}"
|
|
expressions.delete_at(index-1)
|
|
else
|
|
output.puts "Deleting all watched expressions"
|
|
expressions.clear
|
|
end
|
|
end
|
|
|
|
def list
|
|
if expressions.empty?
|
|
output.puts "No watched expressions"
|
|
else
|
|
Pry::Pager.with_pager(output) do |pager|
|
|
pager.puts "Listing all watched expressions:"
|
|
pager.puts ""
|
|
expressions.each_with_index do |expr, index|
|
|
pager.print text.with_line_numbers(expr.to_s, index+1)
|
|
end
|
|
pager.puts ""
|
|
end
|
|
end
|
|
end
|
|
|
|
def eval_and_print_changed(output)
|
|
expressions.each do |expr|
|
|
expr.eval!
|
|
if expr.changed?
|
|
output.puts "#{text.blue "watch"}: #{expr.to_s}"
|
|
end
|
|
end
|
|
end
|
|
|
|
def add_expression(arguments)
|
|
expressions << Expression.new(target, arg_string)
|
|
output.puts "Watching #{Code.new(arg_string)}"
|
|
end
|
|
|
|
def add_hook
|
|
hook = [:after_eval, :watch_expression]
|
|
unless Pry.hooks.hook_exists?(*hook)
|
|
Pry.hooks.add_hook(*hook) do |_, _pry_|
|
|
eval_and_print_changed _pry_.output
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
Pry::Commands.add_command(Pry::Command::WatchExpression)
|
|
end
|