Created
November 1, 2015 12:48
-
-
Save danielvamosi/a9b6dafe5488667031d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'colorize' | |
# find strings in last evaluated output | |
Pry::Commands.command '??', 'where is it' do |*args| | |
prev_out = target.eval '_' | |
if prev_out.nil? | |
output.puts '_ is empty.' | |
elsif args.empty? | |
output.puts 'What?' | |
else | |
regexp = /(#{args.join('|')})/ | |
pretty_stream = PP.pp(prev_out, out = StringIO.new) | |
with_syntax_hl = CodeRay.scan(pretty_stream.string, :ruby).term | |
with_matches = with_syntax_hl.gsub(regexp) { |m| m.black.on_magenta } | |
output.puts with_matches | |
end | |
end | |
Pry::Commands.alias_command "whereisit", "??" | |
# pretty integers | |
Pry.config.hooks.add_hook(:after_eval, "pretty integers") do |result, pry| | |
if result.is_a? Integer | |
pry.output.puts result.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1_').reverse | |
end | |
end | |
# local variables and values in current scope | |
IGNORABLE_LOCAL_VARS = [:__, :_, :_ex_, :_pry_, :_out_, :_in_, :_dir_, :_file_] | |
Pry.config.hooks.add_hook(:before_session, :local_variables) do |output, binding, pry| | |
(binding.eval('local_variables').sort - IGNORABLE_LOCAL_VARS).each do |var| | |
output.print var.to_s.magenta.on_black.rjust(40, ' ') | |
output.print ' ' | |
output.puts binding.eval(var.to_s).inspect | |
end | |
2.times { output.puts } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment