/
display.rb
66 lines (50 loc) · 1.47 KB
/
display.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
require "byebug/command"
require "byebug/helpers/eval"
module Byebug
#
# Custom expressions to be displayed every time the debugger stops.
#
class DisplayCommand < Command
include Helpers::EvalHelper
self.allow_in_post_mortem = false
self.always_run = 2
def self.regexp
/^\s* disp(?:lay)? (?:\s+ (.+))? \s*$/x
end
def self.description
<<-DESCRIPTION
disp[lay][ <expression>]
#{short_description}
If <expression> specified, adds <expression> into display expression
list. Otherwise, it lists all expressions.
DESCRIPTION
end
def self.short_description
"Evaluates expressions every time the debugger stops"
end
def execute
return print_display_expressions unless @match && @match[1]
Byebug.displays.push [true, @match[1]]
display_expression(@match[1])
end
private
def display_expression(exp)
print pr("display.result", n: Byebug.displays.size,
exp: exp,
result: eval_expr(exp))
end
def print_display_expressions
result = prc("display.result", Byebug.displays) do |item, index|
active, exp = item
{ n: index + 1, exp: exp, result: eval_expr(exp) } if active
end
print result
end
def eval_expr(expression)
error_eval(expression).inspect
rescue StandardError
"(undefined)"
end
end
end