2013-11-22 19:36:18 -05:00
|
|
|
class Pry
|
|
|
|
class Command::WatchExpression < Pry::ClassCommand
|
|
|
|
require 'pry/commands/watch_expression/expression.rb'
|
|
|
|
|
|
|
|
match 'watch'
|
|
|
|
group 'Context'
|
2014-02-04 13:18:10 -05:00
|
|
|
description 'Watch the value of an expression and print a notification whenever it changes.'
|
2018-10-12 15:09:29 -04:00
|
|
|
command_options use_prefix: false
|
2013-11-22 19:36:18 -05:00
|
|
|
|
|
|
|
banner <<-'BANNER'
|
|
|
|
Usage: watch [EXPRESSION]
|
|
|
|
watch
|
|
|
|
watch --delete [INDEX]
|
|
|
|
|
2014-02-04 13:18:10 -05:00
|
|
|
watch [EXPRESSION] adds an expression to the list of those being watched.
|
|
|
|
It will be re-evaluated every time you hit enter in pry. If its value has
|
|
|
|
changed, the new value will be printed to the console.
|
|
|
|
|
|
|
|
This is useful if you are step-through debugging and want to see how
|
|
|
|
something changes over time. It's also useful if you're trying to write
|
|
|
|
a method inside pry and want to check that it gives the right answers
|
|
|
|
every time you redefine it.
|
|
|
|
|
|
|
|
watch on its own displays all the currently watched expressions and their
|
|
|
|
values, and watch --delete [INDEX] allows you to delete expressions from
|
|
|
|
the list being watched.
|
2013-11-22 19:36:18 -05:00
|
|
|
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.",
|
2018-10-12 15:09:29 -04:00
|
|
|
optional_argument: true, as: Integer
|
2013-11-22 19:36:18 -05:00
|
|
|
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
|
2014-01-24 01:13:53 -05:00
|
|
|
case
|
|
|
|
when opts.present?(:delete)
|
|
|
|
delete opts[:delete]
|
|
|
|
when opts.present?(:list) || args.empty?
|
|
|
|
list
|
|
|
|
else
|
|
|
|
add_hook
|
|
|
|
add_expression(args)
|
|
|
|
end
|
2013-11-22 19:36:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def expressions
|
2014-02-04 17:17:27 -05:00
|
|
|
_pry_.config.watch_expressions ||= []
|
2013-11-22 19:36:18 -05:00
|
|
|
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
|
2014-04-30 05:08:29 -04:00
|
|
|
_pry_.pager.open do |pager|
|
2013-11-22 19:36:18 -05:00
|
|
|
pager.puts "Listing all watched expressions:"
|
|
|
|
pager.puts ""
|
|
|
|
expressions.each_with_index do |expr, index|
|
2017-11-16 11:54:14 -05:00
|
|
|
pager.print with_line_numbers(expr.to_s, index+1)
|
2013-11-22 19:36:18 -05:00
|
|
|
end
|
|
|
|
pager.puts ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-24 02:36:46 -05:00
|
|
|
def eval_and_print_changed(output)
|
2013-11-22 19:36:18 -05:00
|
|
|
expressions.each do |expr|
|
|
|
|
expr.eval!
|
|
|
|
if expr.changed?
|
2018-10-15 16:49:13 -04:00
|
|
|
output.puts "#{blue "watch"}: #{expr}"
|
2013-11-22 19:36:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-14 03:35:19 -04:00
|
|
|
# TODO: fix arguments.
|
|
|
|
# https://github.com/pry/pry/commit/b031df2f2f5850ee6e9018f33d35f3485a9b0423
|
2018-10-14 02:56:53 -04:00
|
|
|
def add_expression(_arguments)
|
2014-04-29 21:40:56 -04:00
|
|
|
expressions << Expression.new(_pry_, target, arg_string)
|
2014-07-20 20:31:02 -04:00
|
|
|
output.puts "Watching #{Code.new(arg_string).highlighted}"
|
2013-11-22 19:36:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_hook
|
|
|
|
hook = [:after_eval, :watch_expression]
|
2014-01-28 10:05:48 -05:00
|
|
|
unless _pry_.hooks.hook_exists?(*hook)
|
|
|
|
_pry_.hooks.add_hook(*hook) do |_, _pry_|
|
2014-01-24 02:36:46 -05:00
|
|
|
eval_and_print_changed _pry_.output
|
2013-11-22 19:36:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::WatchExpression)
|
|
|
|
end
|