Object
# File lib/ap/awesome_print.rb, line 12 12: def initialize(options = {}) 13: @options = { 14: :multiline => true, 15: :plain => false, 16: :indent => 4, 17: :color => { 18: :array => :white, 19: :bigdecimal => :blue, 20: :class => :yellow, 21: :date => :greenish, 22: :falseclass => :red, 23: :fixnum => :blue, 24: :float => :blue, 25: :hash => :pale, 26: :struct => :pale, 27: :nilclass => :red, 28: :string => :yellowish, 29: :symbol => :cyanish, 30: :time => :greenish, 31: :trueclass => :green 32: } 33: } 34: 35: # Merge custom defaults and let explicit options parameter override them. 36: merge_custom_defaults! 37: merge_options!(options) 38: 39: @indentation = @options[:indent].abs 40: Thread.current[AP] ||= [] 41: end
Dispatcher that detects data nesting and invokes object-aware formatter.
# File lib/ap/awesome_print.rb, line 143 143: def awesome(object) 144: if Thread.current[AP].include?(object.object_id) 145: nested(object) 146: else 147: begin 148: Thread.current[AP] << object.object_id 149: send(:"awesome_#{printable(object)}", object) 150: ensure 151: Thread.current[AP].pop 152: end 153: end 154: end
Format an array.
# File lib/ap/awesome_print.rb, line 47 47: def awesome_array(a) 48: return "[]" if a == [] 49: 50: if @options[:multiline] 51: width = (a.size - 1).to_s.size 52: data = a.inject([]) do |arr, item| 53: index = colorize("#{indent}[#{arr.size.to_s.rjust(width)}] ", :array) 54: indented do 55: arr << (index << awesome(item)) 56: end 57: end 58: "[\n" << data.join(",\n") << "\n#{outdent}]" 59: else 60: data = a.inject([]) { |arr, item| arr << awesome(item) } 61: "[ #{data.join(', ')} ]" 62: end 63: end
Format BigDecimal and Rational objects by convering them to Float.
# File lib/ap/awesome_print.rb, line 130 130: def awesome_bigdecimal(n) 131: awesome_self(n.to_f, :as => :bigdecimal) 132: end
Format Class object.
# File lib/ap/awesome_print.rb, line 106 106: def awesome_class(c) 107: if superclass = c.superclass # <-- Assign and test if nil. 108: awesome_self(c, :with => " < #{superclass}") 109: else 110: awesome_self(c) 111: end 112: end
Format Dir object.
# File lib/ap/awesome_print.rb, line 123 123: def awesome_dir(d) 124: ls = `ls -alF #{d.path.shellescape}` 125: awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}") 126: end
Format File object.
# File lib/ap/awesome_print.rb, line 116 116: def awesome_file(f) 117: ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}` 118: awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}") 119: end
Format a hash. If @options[:indent] if negative left align hash keys.
# File lib/ap/awesome_print.rb, line 67 67: def awesome_hash(h) 68: return "{}" if h == {} 69: 70: data = h.keys.inject([]) do |arr, key| 71: plain_single_line do 72: arr << [ awesome(key), h[key] ] 73: end 74: end 75: 76: width = data.map { |key, | key.size }.max || 0 77: width += @indentation if @options[:indent] > 0 78: 79: data = data.inject([]) do |arr, (key, value)| 80: if @options[:multiline] 81: formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width)) 82: else 83: formatted_key = key 84: end 85: indented do 86: arr << (formatted_key << colorize(" => ", :hash) << awesome(value)) 87: end 88: end 89: if @options[:multiline] 90: "{\n" << data.join(",\n") << "\n#{outdent}}" 91: else 92: "{ #{data.join(', ')} }" 93: end 94: end
Catch all method to format an arbitrary object.
# File lib/ap/awesome_print.rb, line 137 137: def awesome_self(object, appear = {}) 138: colorize(object.inspect << appear[:with].to_s, appear[:as] || declassify(object)) 139: end
Format a Struct. If @options[:indent] if negative left align hash keys.
# File lib/ap/awesome_print.rb, line 98 98: def awesome_struct(s) 99: h = {} 100: s.each_pair { |k,v| h[k] = v } 101: awesome_hash(h) 102: end
Pick the color and apply it to the given string as necessary.
# File lib/ap/awesome_print.rb, line 189 189: def colorize(s, type) 190: if @options[:plain] || @options[:color][type].nil? 191: s 192: else 193: s.send(@options[:color][type]) 194: end 195: end
Turn class name into symbol, ex: Hello::World => :hello_world.
# File lib/ap/awesome_print.rb, line 179 179: def declassify(object) 180: if object.is_a?(Struct) 181: :struct 182: else 183: object.class.to_s.gsub(/:+/, "_").downcase.to_sym 184: end 185: end
# File lib/ap/awesome_print.rb, line 215 215: def indent 216: @indent = ' ' * @indentation 217: end
# File lib/ap/awesome_print.rb, line 208 208: def indented 209: @indentation += @options[:indent].abs 210: yield 211: ensure 212: @indentation -= @options[:indent].abs 213: end
Load ~/.aprc file with custom defaults that override default options.
# File lib/ap/awesome_print.rb, line 232 232: def merge_custom_defaults! 233: dotfile = File.join(ENV["HOME"], ".aprc") 234: if File.readable?(dotfile) 235: load dotfile 236: merge_options!(self.class.defaults) 237: end 238: rescue => e 239: $stderr.puts "Could not load #{dotfile}: #{e}" 240: end
Update @options by first merging the :color hash and then the remaining keys.
# File lib/ap/awesome_print.rb, line 225 225: def merge_options!(options = {}) 226: @options[:color].merge!(options.delete(:color) || {}) 227: @options.merge!(options) 228: end
Format nested data, for example: arr = [1, 2]; arr << arr => [1,2, [...]] hsh = { :a => 1 }; hsh[:b] = hsh => { :a => 1, :b => {...} }
# File lib/ap/awesome_print.rb, line 162 162: def nested(object) 163: case printable(object) 164: when :array then colorize("[...]", :array) 165: when :hash then colorize("{...}", :hash) 166: when :struct then colorize("{...}", :struct) 167: else colorize("...#{object.class}...", :class) 168: end 169: end
# File lib/ap/awesome_print.rb, line 219 219: def outdent 220: @outdent = ' ' * (@indentation - @options[:indent].abs) 221: end
Format hash keys as plain string regardless of underlying data type.
# File lib/ap/awesome_print.rb, line 199 199: def plain_single_line 200: plain, multiline = @options[:plain], @options[:multiline] 201: @options[:plain], @options[:multiline] = true, false 202: yield 203: ensure 204: @options[:plain], @options[:multiline] = plain, multiline 205: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.