-
Notifications
You must be signed in to change notification settings - Fork 0
/
diag.rb
241 lines (202 loc) · 6.75 KB
/
diag.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require_relative "linewriter"
require "set"
class Diagnostics
class DiagnosticError < RuntimeError; end
def initialize(pos: [], on_error: nil, quiet: nil, verbose: nil)
@pos = pos
@on_error = on_error
@quiet = quiet
@verbose = verbose
end
def fork
Diagnostics.new(pos: @pos.clone, on_error: @on_error, quiet: @quiet, verbose: @verbose)
end
def push(str) @pos.push(str); self end
def pop; @pos.pop(); self end
def pos(str)
push(str)
begin
return yield
ensure
pop()
end
end
def trace(lvl, str)
$stderr << "[#{@pos.join(" :: ")}] #{lvl}: #{str}\n"
str
end
def ctrace(lvl, fmt, str)
$stderr << "\e[1m[#{@pos.join(" :: ")}]\e[m \e[#{fmt}m#{lvl}:\e[m #{str}\n"
str
end
def good(lvl, fmt, str)
ctrace(lvl, fmt, str) unless @quiet && @quiet.()
str
end
def verb(lvl, fmt, str)
ctrace(lvl, fmt, str) if @verbose && @verbose.()
str
end
def bad(lvl, fmt, str)
ctrace(lvl, fmt, str)
@on_error.() if @on_error
str
end
def bad_r(lvl, fmt, str)
DiagnosticError.new bad(lvl, fmt, str)
end
def hl_uname(name, last = true)
name = name.to_s unless name.is_a?(String)
if name =~ /^(@|@@|\$)(.*)$/
"\e[38;5;2m#{$1}\e[38;5;4m#{$2}"
else
"\e[38;5;#{last ? "4" : "2"}m#{name}"
end
end
def hl_qname(name)
name = name.to_s unless name.is_a?(String)
name = name.split(/(::|#|\.)/)
return "" if name.empty?
name.each_with_index.map do |part, idx|
next "" if part.empty?
idx % 2 == 0 ? hl_uname(part, idx == name.length - 1) : "\e[m#{part}"
end.join
end
private def hl_fold(obj)
remain = 32
catch :break do
count = lambda do |obj|
throw :break unless remain > 0
case obj
when Array
return false if obj.length <= 1
remain -= 2 * [1, obj.length].max
obj.each{|e| count.(e) }
when Hash
return false if obj.length <= 1
remain -= 2 + (obj.empty? ? 0 : 2) + 4 * [0, obj.length - 2].max
obj.each{|k, v| count.(k); count.(v) }
when Set
return false if obj.length <= 1
remain -= 1 + obj.class.name.length + 2 * [1, obj.length].max
obj.each{|e| count.(e) }
else
has_no_inspect = obj.class.method_defined?(:diag_no_inspect)
if obj.method(:inspect).owner == Kernel || has_no_inspect
no_inspect = has_no_inspect ? Set.new(obj.diag_no_inspect) : Set.new
remain -= 23 + obj.class.name.length
first = true
obj.instance_variables.select do |el|
next false if no_inspect.include?(el)
remain -= if first
first = false
3
else 5 end
true
end.each{|e| count.(obj.instance_variable_get(e)) }
else remain -= obj.inspect.length end
end
end
count.(obj)
end
return remain <= 0
end
def hl(obj, long: false)
lsp = long ? " " : ""
case obj
when Array
items = obj.map{|e| hl(e, long: long) }
if long && hl_fold(obj)
LineWriter.lines do |l|
l << "\e[m["
l.fmt with_indent: " " do
items.each_with_index do |el, idx|
l.peek << "\e[m," unless idx == 0
el.to_s.split("\n").each{|e| l << e }
end
end
l << "\e[m]"
end
else
"[#{items.join("\e[m, ")}]"
end
when Hash, Set
items = if obj.is_a?(Set)
obj.map{|e| hl(e, long: long) }
else
obj.select{|k, _| k.is_a?(Symbol) }.map do |key, val|
"\e[38;5;3m#{key.to_s}\e[m: #{hl(val, long: long)}"
end.concat(obj.select{|k, _| !k.is_a?(Symbol) }.map do |key, val|
"#{hl(key, long: long)}\e[m#{lsp}=>#{lsp}#{hl(val, long: long)}"
end)
end
s = if long && hl_fold(obj)
LineWriter.lines do |l|
l << "\e[m{"
l.fmt with_indent: " " do
items.each_with_index do |el, idx|
l.peek << "\e[m," unless idx == 0
el.split("\n").each{|e| l << e }
end
end
l << "\e[m}"
end
else
"\e[m{#{items.join("\e[m, ")}\e[m}"
end
if obj.is_a?(Hash)
"\e[m#{s}"
else
"\e[38;5;9m#\e[m<#{hl_qname(obj.class.name)}\e[m: #{s}>"
end
when Process::Status
[
*("exited with code \e[38;5;#{obj.success? ? 2 : 1}m#{obj.exitstatus}" if obj.exited?),
*("\e[38;5;3mkilled by signal \e[35;5;5m#{Signal.signame(obj.termsig)}" if obj.signaled?),
*("\e[38;5;4mstopped by signal \e[38;5;5m#{Signal.signame(obj.stopsig)}" if obj.stopped?),
].tap{|a| a << "\e[38;5;13mrunning..." if a.empty? }.map{|p| "#{p}\e[m" }.join(" | ")
else
has_no_inspect = obj.class.method_defined?(:diag_no_inspect)
if obj.method(:inspect).owner == Kernel || has_no_inspect
no_inspect = has_no_inspect ? Set.new(obj.diag_no_inspect) : Set.new
prefix = "\e[38;5;9m#\e[m<#{hl_qname(obj.class.name)}\e[m:\e[38;5;5m0x#{obj.object_id.to_s(16)}"
items = obj.instance_variables.select{|e| !no_inspect.include?(e) }.map do |e|
hl_uname(e) << "\e[m#{lsp}=#{lsp}#{hl(obj.instance_variable_get(e), long: long)}"
end
if long && hl_fold(obj)
LineWriter.lines do |l|
l << prefix
l.fmt with_indent: " " do
items.each_with_index do |el, idx|
l.peek << "\e[m," unless idx == 0
el.split("\n").each{|e| l << e }
end
end
l << "\e[m>"
end
else
"#{prefix} #{items.join("\e[m, ")}\e[m>"
end
else
"\e[38;5;#{case obj
when Symbol, true, false, nil; "3"
when String; "6"
when Numeric; "5"
else "13"
end}m#{obj.inspect}\e[m"
end
end
end
def debug(str) good("debug", "38;5;8;1", str) end
def info(str) good("info", "38;5;8;1", str) end
def warn(str) good("warning", "38;5;13;1", str) end
def debug_v(str) verb("debug", "38;5;8;1", str) end
def info_v(str) verb("info", "38;5;8;1", str) end
def warn_v(str) verb("warning", "38;5;13;1", str) end
def error(str) bad("error", "38;5;1;1", str) end
def fatal(str) bad("fatal", "38;5;1;1", str) end
def error_r(str) bad_r("error", "38;5;1;1", str) end
def fatal_r(str) bad_r("fatal", "38;5;1;1", str) end
def p(obj, long: false) debug(hl(obj, long: long)) end
def p_v(obj, long: false) debug_v(hl(obj, long: long)) end
end