awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

[Feature] 'depth' in options #343

Open maciejpk opened 6 years ago

maciejpk commented 6 years ago

This is actually a feature request that shouldn't be hard to implement. What I actually would like to have, is a depth of "awesome printing", so when for example I have an array like:

arr = [
  [1,2,3],
  [4,5,6],
  [7,8,9]
]

then for depth == 2 it would print it the usual way, which would be ap arr, but if the depth == 1 then it would be displayed like in the above manner.

Reason for this feature? Pluck command. Let's take an ActiveRecord and do something like this ap User.where(...).pluck(:id, :created_at, :updated_at) It would be printed in an ugly way, which would be something alike

[
    [ 0] [
        [0] 6,
        [1] Fri, 22 Jun 2018 10:31:53 UTC +00:00,
        [2] Fri, 22 Jun 2018 10:31:53 UTC +00:00
    ],
    [ 1] [
        [0] 7,
        [1] Tue, 26 Jun 2018 09:00:50 UTC +00:00,
        [2] Tue, 26 Jun 2018 09:00:50 UTC +00:00
    ],
    [ 2] [
        [0] 8,
        [1] Tue, 26 Jun 2018 09:01:09 UTC +00:00,
        [2] Tue, 26 Jun 2018 09:01:09 UTC +00:00
    ],
    [ 3] [
        [0] 10,
        [1] Wed, 27 Jun 2018 07:28:20 UTC +00:00,
        [2] Wed, 27 Jun 2018 07:28:20 UTC +00:00
    ]
]

when instead I would like it to show each record as a row, so that would look something like this

[
    [ 0] [6, Fri, 22 Jun 2018 10:31:53 UTC +00:00, Fri, 22 Jun 2018 10:31:53 UTC +00:00],
    [ 1] [7, Tue, 26 Jun 2018 09:00:50 UTC +00:00, Tue, 26 Jun 2018 09:00:50 UTC +00:00],
    [ 2] [8, Tue, 26 Jun 2018 09:01:09 UTC +00:00, Tue, 26 Jun 2018 09:01:09 UTC +00:00],
    [ 3] [10, Wed, 27 Jun 2018 07:28:20 UTC +00:00, Wed, 27 Jun 2018 07:28:20 UTC +00:00]
]
bmon commented 5 years ago

I've also found myself wanting this, the example above can be patched in (i.e in ~/.pryrc) like so:

require "awesome_print"
module AwesomePrint
  module Formatters
    class ArrayFormatter
      alias old_gen generate_printable_array
      def generate_printable_array
        options[:multiline] = false
        old_gen
      end
    end
  end
end
AwesomePrint.pry!

Doesn't support depth, and if you want to start printing out complex objects it will wrap and look very ugly, but for a quick hack it does the trick pretty well.