class Pry::Command::WatchExpression

Public Instance Methods

options(opt) click to toggle source
# File lib/pry/commands/watch_expression.rb, line 31
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
process() click to toggle source
# File lib/pry/commands/watch_expression.rb, line 42
def process
  if opts.present?(:delete)
    delete opts[:delete]
  elsif opts.present?(:list) || args.empty?
    list
  else
    add_hook
    add_expression(args)
  end
end

Private Instance Methods

add_expression(_arguments) click to toggle source

TODO: fix arguments. github.com/pry/pry/commit/b031df2f2f5850ee6e9018f33d35f3485a9b0423

# File lib/pry/commands/watch_expression.rb, line 93
def add_expression(_arguments)
  expressions << Expression.new(pry_instance, target, arg_string)
  output.puts "Watching #{Code.new(arg_string).highlighted}"
end
add_hook() click to toggle source
# File lib/pry/commands/watch_expression.rb, line 98
def add_hook
  hook = [:after_eval, :watch_expression]
  return if pry_instance.hooks.hook_exists?(*hook)

  pry_instance.hooks.add_hook(*hook) do |_, pry_instance|
    eval_and_print_changed pry_instance.output
  end
end
delete(index) click to toggle source
# File lib/pry/commands/watch_expression.rb, line 59
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
eval_and_print_changed(output) click to toggle source
# File lib/pry/commands/watch_expression.rb, line 84
def eval_and_print_changed(output)
  expressions.each do |expr|
    expr.eval!
    output.puts "#{blue 'watch'}: #{expr}" if expr.changed?
  end
end
expressions() click to toggle source
# File lib/pry/commands/watch_expression.rb, line 55
def expressions
  state.watch_expressions ||= []
end
list() click to toggle source
# File lib/pry/commands/watch_expression.rb, line 69
def list
  if expressions.empty?
    output.puts "No watched expressions"
  else
    pry_instance.pager.open do |pager|
      pager.puts "Listing all watched expressions:"
      pager.puts ""
      expressions.each_with_index do |expr, index|
        pager.print with_line_numbers(expr.to_s, index + 1)
      end
      pager.puts ""
    end
  end
end