Last active
May 19, 2025 21:13
-
-
Save gurgeous/b0045645a1c7f93194ca285ddebafad7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module AmazingPrint | |
# | |
# add custom colors | |
# | |
send(:remove_const, :CUSTOM_RGB_COLORS) if const_defined?(:CUSTOM_RGB_COLORS) | |
CUSTOM_RGB_COLORS = { | |
chartreuse: "#7fff00", | |
deepskyblue: "#00bfff", | |
gold: "#ffd700", | |
hotpink: "#ff69b4", | |
lawngreen: "#7cfc00", | |
limegreen: "#32cd32", | |
red: "#ff0000", | |
tomato: "#ff6347", | |
} | |
CUSTOM_RGB_COLORS.each do |name, hex| | |
channels = hex[1..].scan(/../).map { _1.to_i(16) } | |
ansi = "\e[38;2;#{channels.join(";")}m%s\e[0m" | |
Colors.define_singleton_method(:"rgb_#{name}") { |str, _html| ansi % str } | |
end | |
self.defaults = { | |
indent: -2, # negative = ljust | |
ruby19_syntax: true, | |
color: { | |
class: :rgb_gold, | |
string: :rgb_gold, | |
symbol: :rgb_chartreuse, | |
trueclass: :rgb_chartreuse, | |
nilclass: :rgb_tomato, | |
falseclass: :rgb_tomato, | |
integer: :rgb_deepskyblue, | |
float: :rgb_deepskyblue, | |
variable: :rgb_gold, | |
keyword: :rgb_gold, | |
method: :rgb_gold, | |
}, | |
} | |
# | |
# The simple formatter is used for many non-trivial types. Let's try to | |
# colorize it a bit. | |
# | |
class Formatter | |
def awesome_simple(o, type, inspector = @inspector) | |
AmazingPrint::Formatters::SimpleFormatter.new(o, type, inspector).format.tap do |str| | |
next if options[:plain] || !inspector.colorize? | |
next if str.length < 80 | |
# common constants | |
str.gsub!(/([:=] ?)\b(true|false|nil|\d+)([,}>])/) do | |
color = ($2 == "false" || $2 == "nil") ? :rgb_deepskyblue : :rgb_gold | |
"#{$1}#{Colors.send(color, $2, false)}#{$3}" | |
end | |
str.gsub!(/([:=] ?)\b(true|false|nil|\d+)([,}>])/) { "#{$1}#{Colors.rgb_deepskyblue($2, false)}#{$3}" } | |
# xxx= | |
str.gsub!(/ (@[a-z]\w*)=/) { " #{Colors.rgb_chartreuse($1, false)}=" } | |
# xxx: | |
str.gsub!(/([ {])([a-z]\w*): /) { "#{$1}#{Colors.rgb_chartreuse($2, false)}: " } | |
# "xxx" => | |
str.gsub!(/("[^"]+") =>/) { "#{Colors.rgb_chartreuse($1, false)} =>" } | |
# #<xxx::yyy:zzz | |
str.gsub!(/(#<[A-Z]\w+(::?\w+)+)/) { Colors.rgb_hotpink($1, false) } | |
end | |
end | |
end | |
# | |
# Remove boring values and use symbols for keys in AR | |
# | |
if defined?(ActiveRecord) || AmazingPrint.rails_console? | |
module ActiveRecord | |
def awesome_active_record_instance(object) | |
attrs = object.class.column_names.each_with_object({}) do |name, hash| | |
if object.has_attribute?(name) || object.new_record? | |
value = object.respond_to?(name) ? object.send(name) : object.read_attribute(name) | |
if value.present? | |
hash[name.to_sym] = value | |
end | |
end | |
end | |
return [awesome_simple(object.to_s, :active_record_instance), awesome_hash(attrs)].join(" ") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment