Coder Social home page Coder Social logo

beanieboi / amazing_print Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amazing-print/amazing_print

0.0 0.0 0.0 933 KB

Pretty print your Ruby objects with style -- in full color and with proper indentation

License: MIT License

Ruby 100.00%

amazing_print's Introduction

Amazing Print

RubyGems Travis CI Code Climate Code Climate Coverage RubyGems Gitter

AmazingPrint is a fork of AwesomePrint which became stale and should be used in it's place to avoid conflicts. It is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.

GitHub API demo

Supported Versions

  • Ruby >= 2.3
  • Rails >= 5.1

Installation

# Installing as Ruby gem
$ gem install amazing_print

# Cloning the repository
$ git clone git://github.com/amazing-print/amazing_print.git

Usage

require "amazing_print"
ap object, options = {}

Default options:

indent:        4,      # Number of spaces for indenting.
index:         true,   # Display array indices.
html:          false,  # Use ANSI color codes rather than HTML.
multiline:     true,   # Display in multiple lines.
plain:         false,  # Use colors.
raw:           false,  # Do not recursively format instance variables.
sort_keys:     false,  # Do not sort hash keys.
sort_vars:     true,   # Sort instance variables.
limit:         false,  # Limit arrays & hashes. Accepts bool or int.
ruby19_syntax: false,  # Use Ruby 1.9 hash syntax in output.
class_name:    :class, # Method called to report the instance class name. (e.g. :to_s)
object_id:     true,   # Show object id.
color: {
  args:       :pale,
  array:      :white,
  bigdecimal: :blue,
  class:      :yellow,
  date:       :greenish,
  falseclass: :red,
  integer:    :blue,
  float:      :blue,
  hash:       :pale,
  keyword:    :cyan,
  method:     :purpleish,
  nilclass:   :red,
  rational:   :blue,
  string:     :yellowish,
  struct:     :pale,
  symbol:     :cyanish,
  time:       :greenish,
  trueclass:  :green,
  variable:   :cyanish
}

Supported color names:

:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale

Use Object#ai to return an ASCII encoded string:

irb> "awesome print".ai
=> "\e[0;33m\"awesome print\"\e[0m"

Examples

$ cat > 1.rb
require "amazing_print"
data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
ap data
^D
$ ruby 1.rb
[
    [0] false,
    [1] 42,
    [2] [
        [0] "forty",
        [1] "two"
    ],
    [3] {
           :class => Time < Object,
             :now => Fri Apr 02 19:55:53 -0700 2010,
        :distance => 4.2e+43
    }
]

$ cat > 2.rb
require "amazing_print"
data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
ap data, :indent => -2  # <-- Left align hash keys.
^D
$ ruby 2.rb
{
  :class    => Time < Object,
  :now      => Fri Apr 02 19:55:53 -0700 2010,
  :distance => 4.2e+43
}

$ cat > 3.rb
require "amazing_print"
data = [ false, 42, %w(forty two) ]
data << data  # <-- Nested array.
ap data, :multiline => false
^D
$ ruby 3.rb
[ false, 42, [ "forty", "two" ], [...] ]

$ cat > 4.rb
require "amazing_print"
class Hello
  def self.world(x, y, z = nil, &blk)
  end
end
ap Hello.methods - Class.methods
^D
$ ruby 4.rb
[
    [0] world(x, y, *z, &blk) Hello
]

$ cat > 5.rb
require "amazing_print"
ap (''.methods - Object.methods).grep(/!/)
^D
$ ruby 5.rb
[
    [ 0] capitalize!()           String
    [ 1]      chomp!(*arg1)      String
    [ 2]       chop!()           String
    [ 3]     delete!(*arg1)      String
    [ 4]   downcase!()           String
    [ 5]     encode!(*arg1)      String
    [ 6]       gsub!(*arg1)      String
    [ 7]     lstrip!()           String
    [ 8]       next!()           String
    [ 9]    reverse!()           String
    [10]     rstrip!()           String
    [11]      slice!(*arg1)      String
    [12]    squeeze!(*arg1)      String
    [13]      strip!()           String
    [14]        sub!(*arg1)      String
    [15]       succ!()           String
    [16]   swapcase!()           String
    [17]         tr!(arg1, arg2) String
    [18]       tr_s!(arg1, arg2) String
    [19]     upcase!()           String
]

$ cat > 6.rb
require "amazing_print"
ap 42 == ap(42)
^D
$ ruby 6.rb
42
true
$ cat 7.rb
require "amazing_print"
some_array = (1..1000).to_a
ap some_array, :limit => true
^D
$ ruby 7.rb
[
    [  0] 1,
    [  1] 2,
    [  2] 3,
    [  3] .. [996],
    [997] 998,
    [998] 999,
    [999] 1000
]

$ cat 8.rb
require "amazing_print"
some_array = (1..1000).to_a
ap some_array, :limit => 5
^D
$ ruby 8.rb
[
    [  0] 1,
    [  1] 2,
    [  2] .. [997],
    [998] 999,
    [999] 1000
]

Example (Rails console)

$ rails console
rails> require "amazing_print"
rails> ap Account.limit(2).all
[
    [0] #<Account:0x1033220b8> {
                     :id => 1,
                :user_id => 5,
            :assigned_to => 7,
                   :name => "Hayes-DuBuque",
                 :access => "Public",
                :website => "http://www.hayesdubuque.com",
        :toll_free_phone => "1-800-932-6571",
                  :phone => "(111)549-5002",
                    :fax => "(349)415-2266",
             :deleted_at => nil,
             :created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
             :updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
                  :email => "[email protected]",
        :background_info => nil
    },
    [1] #<Account:0x103321ff0> {
                     :id => 2,
                :user_id => 4,
            :assigned_to => 4,
                   :name => "Ziemann-Streich",
                 :access => "Public",
                :website => "http://www.ziemannstreich.com",
        :toll_free_phone => "1-800-871-0619",
                  :phone => "(042)056-1534",
                    :fax => "(106)017-8792",
             :deleted_at => nil,
             :created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
             :updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
                  :email => "[email protected]",
        :background_info => nil
    }
]
rails> ap Account
class Account < ActiveRecord::Base {
                 :id => :integer,
            :user_id => :integer,
        :assigned_to => :integer,
               :name => :string,
             :access => :string,
            :website => :string,
    :toll_free_phone => :string,
              :phone => :string,
                :fax => :string,
         :deleted_at => :datetime,
         :created_at => :datetime,
         :updated_at => :datetime,
              :email => :string,
    :background_info => :string
}
rails>

IRB integration

To use amazing_print as default formatter in irb and Rails console add the following code to your ~/.irbrc file:

require "amazing_print"
AmazingPrint.irb!

PRY integration

If you miss amazing_print's way of formatting output, here's how you can use it in place of the formatting which comes with pry. Add the following code to your ~/.pryrc:

require "amazing_print"
AmazingPrint.pry!

Logger Convenience Method

amazing_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes letting you call:

logger.ap object

By default, this logs at the :debug level. You can override that globally with:

:log_level => :info

in the custom defaults (see below). You can also override on a per call basis with:

logger.ap object, :warn

ActionView Convenience Method

amazing_print adds the 'ap' method to the ActionView::Base class making it available within Rails templates. For example:

<%= ap @accounts.first %>   # ERB
!= ap @accounts.first       # HAML

With other web frameworks (ex: in Sinatra templates) you can explicitly request HTML formatting:

<%= ap @accounts.first, :html => true %>

String Convenience Methods

Use methods such as .red to set string color:

irb> puts "red text".red
red text # (it's red)

Setting Custom Defaults

You can set your own default options by creating .aprc file in your home directory. Within that file assign your defaults to AmazingPrint.defaults. For example:

# ~/.aprc file.
AmazingPrint.defaults = {
  :indent => -2,
  :color => {
    :hash  => :pale,
    :class => :white
  }
}

Versioning

AmazingPrint follows the Semantic Versioning standard.

Contributing

See CONTRIBUTING.md for information.

License

Copyright (c) 2010-2016 Michael Dvorkin and contributors

http://www.dvorkin.net

%w(mike dvorkin.net) * "@" || "twitter.com/mid"

Released under the MIT license. See LICENSE file for details.

amazing_print's People

Contributors

1z3n avatar aars avatar adrianomitre avatar basicxman avatar beanieboi avatar clonezone avatar dgynn avatar dnnx avatar drewski371-bot avatar fujiin avatar georgeu2000 avatar gerrywastaken avatar gitter-badger avatar gudleik avatar harlemsquirrel avatar imajes avatar kstephens avatar maurogeorge avatar mikemcquaid avatar mmoll avatar mmullis avatar nikolajsen avatar nviennot avatar olleolleolle avatar paddor avatar rafbm avatar shyndman avatar tatsuyafw avatar tobias avatar waldyr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.