Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created April 29, 2021 07:03
Show Gist options
  • Save JoshCheek/8238e2b7be68f799bada3f4a49e26ee5 to your computer and use it in GitHub Desktop.
Save JoshCheek/8238e2b7be68f799bada3f4a49e26ee5 to your computer and use it in GitHub Desktop.
Script I ran to parse a `pretty_inspect` output that was 84k lines long and show me the path to the lines of interest
ruby < omg -r coderay -lne '
BEGIN {
target = /DefaultTrailApplicability/ # needle
stack = [] # haystack
results = []
indentation = -> line { line.to_s[/\A\s*/].size }
String.class_eval { attr_accessor :lineno }
}
$_.lineno = $. # each line tracks its line number
(stack.pop; stack.push $_) if indentation[stack.last] == indentation[$_]
stack.push $_ if indentation[stack.last] < indentation[$_]
stack.pop while indentation[stack.last] > indentation[$_]
results, stack = [*results, *stack], [] if ~target
END {
lineno_width = results.last&.lineno.to_s.size * 4 / 3
code = CodeRay.encode results.join("\n"), :ruby, :terminal # syntax highlight results
code.gsub! target, "\e[47m\\\\&\e[0m" # highlight matches w/ white bg
results.map(&:lineno).zip(code.lines) do |lineno, line|
pretty_lineno = lineno.to_s.gsub /(?<=\d)(?=\d{3}+\b)/, ",\\\\&"
puts "#{pretty_lineno.rjust lineno_width} | #{line}"
end
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment