Nazar

Turn this:
>> User.all
User Load (0.4ms) SELECT "users".* FROM "users" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<User id: 1, email: "[email protected]", name: "Mrs. Charis Brown", description: "Quasi atque ea. Beatae corporis quia. Eveniet veli...", created_at: "2021-09-01 05:42:24.341344000 +0000", updated_at: "2021-09-20 09:36:29.289078000 +0000", active: false, last_login_at: nil>, #<User id: 2, email: "[email protected]", name: "Ms. Sherice Champlin", description: "Omnis ut illum. Non velit mollitia. Expedita facil...", created_at: "2021-09-10 07:27:00.182298000 +0000", updated_at: "2021-09-20 09:36:29.292065000 +0000", active: true, last_login_at: "2021-09-19 16:25:32.094418000 +0000">, #<User id: 3, email: "[email protected]", name: "Florentino Gleason", description: "Non recusandae eos. Et voluptates iusto. Commodi e...", created_at: "2021-09-07 18:24:45.608164000 +0000", updated_at: "2021-09-20 09:36:29.293572000 +0000", active: false, last_login_at: nil>, #<User id: 4, email: "[email protected]", name: "Judie Boehm", description: "Ut ea eum. Tempore voluptates praesentium. Animi s...", created_at: "2021-09-05 02:11:36.564693000 +0000", updated_at: "2021-09-20 09:36:29.295763000 +0000", active: true, last_login_at: "2021-09-19 20:17:24.074515000 +0000">, #<User id: 5, email: "[email protected]", name: "Mari Ullrich", description: "Aperiam quas voluptas. Autem alias quia. Aut persp...", created_at: "2021-09-09 05:23:03.229860000 +0000", updated_at: "2021-09-20 09:36:29.296928000 +0000", active: nil, last_login_at: nil>, #<User id: 6, email: "[email protected]", name: "Sam Larson DDS", description: "Sed velit consectetur. Quas cupiditate enim. Neque...", created_at: "2021-09-03 02:52:14.055696000 +0000", updated_at: "2021-09-20 09:36:29.299157000 +0000", active: true, last_login_at: "2021-09-20 05:01:39.019563000 +0000">, #<User id: 7, email: "[email protected]", name: "Ying Kub I", description: "Cupiditate ut consequatur. Eos ab laboriosam. Ipsa...", created_at: "2021-09-10 05:52:32.005877000 +0000", updated_at: "2021-09-20 09:36:29.301469000 +0000", active: true, last_login_at: "2021-09-19 21:30:48.854010000 +0000">, #<User id: 8, email: "[email protected]", name: "Johnie Corwin II", description: "Suscipit itaque adipisci. Assumenda ad error. Quas...", created_at: "2021-09-12 13:05:49.845361000 +0000", updated_at: "2021-09-20 09:36:29.302657000 +0000", active: nil, last_login_at: nil>, #<User id: 9, email: "[email protected]", name: "Sharie Weber", description: "Sapiente voluptatum mollitia. Dolores voluptatum a...", created_at: "2021-09-19 03:03:39.360526000 +0000", updated_at: "2021-09-20 09:36:29.304929000 +0000", active: true, last_login_at: "2021-09-20 06:46:05.846685000 +0000">, #<User id: 10, email: "[email protected]", name: "Yoshiko Mohr", description: "Similique laborum totam. Non cum atque. Placeat qu...", created_at: "2021-09-08 05:41:36.588541000 +0000", updated_at: "2021-09-20 09:36:29.306120000 +0000", active: nil, last_login_at: nil>, ...]>
Into this:
>> User.all
(0.6ms) SELECT sqlite_version(*)
User Load (0.8ms) SELECT "users".* FROM "users"
Installation
Per-project installation
While nothing stops you from adding Nazar to each project separately, I find it cumbersome to add it to every project I'm working on - it also alters REPL behaviour for other teammates, which is another reason why personally I don't recommend that approach. Anyway, here's quick how-to:
Add nazar to Gemfile:
group :development, :test do
gem 'nazar', require: false
end
Then, you have to call Nazar.enable!. If you're using Rails, you might want to add this snippet to config/application.rb:
console do
require 'nazar'
Nazar.enable! # See configuration section for more options and
# Opt-in setup section if you don't want to enable it for every item
end
Otherwise, call it in bin/console or any other script that launches your REPL.
Global installation
This is my recommended way to install this gem - that way it works automatically in all your projects, even in standalone irb or pry sessions.
- Create global gems path
mkdir -p ~/.gem/ruby/global - You're welcome to use any other path for your global gemset
- Install nazar in your global gemset
gem install nazar -i ~/.gem/ruby/global
- Load Nazar in your REPL rc file
Add following snippet to your
.irbrcor.pryrc:
global_gemset_path = File.('~/.gem/ruby/global/gems')
if Dir.exist?(global_gemset_path)
global_gems_path = Dir.glob("#{global_gemset_path}/*/lib")
$LOAD_PATH.unshift(*global_gems_path)
end
%w[nazar].each do |gem| # add more gems you want to load globally
begin
require gem
rescue LoadError
end
end
Nazar.enable! if defined?(Nazar)
Layouts
Nazar.config.formatter.layout can be set to either :horizontal, :vertical or :auto.
:auto uses horizontal layout if it fits in your console size, otherwise it falls back to :vertical layout.
Here's a preview of both layouts:
Horizontal
Vertical
User.first(5)
Shorthand method
Nazar defines top level __(data, **options) method, that can be useful for two things:
Inspecting (array of) Hashes/Structs
Nazar by design won't enhance output for any collection that does not consist of supported (eg. ActiveRecord/Sequel) items. If you have a data that is structurally compatible (ie: all items consists of the same keys), and want to enahance the output, you can simply use #__ method:
__([{foo: :bar, test: 123},{foo: :baz, test: 456}])
It works with Structs and Hashes (or, more specifically - with any object that reponds to #keys and #values)
You can also enforce layout and/or pagination:
__([{foo: :bar, test: 123},{foo: :baz, test: 456}], layout: :vertical)
Opt-in setup
If you use Nazar.load! instead of Nazar.enable!, it would not enhance output for every element in the console. Instead, you have to call #__ for each item that you want to enhance output
Nazar.load!
User.all # Returns default output
__ User.all # Returns output enhanced by Nazar
Configuration
By default, Nazar improves output for ActiveRecord (both collections and a single item) and for CSV Tables. You can configure that by calling #enable! with optional argument:
Nazar.enable!(extensions: [:active_record, :csv, :sequel]) will enhance output for Sequel as well.
You can also configure behaviour of Nazar:
| Config | Default | Description |
|---|---|---|
Nazar.config.colors.enabled |
true | Determines whether the output should be colorised or not |
Nazar.config.formatter.nil |
∅ |
Sets character printed if the value was nil |
Nazar.config.formatter.layout |
:auto |
Determines which layout should be used. See Layouts for more details |
Nazar.config.formatter.paginate |
true |
Determines if results too long/wide should be printed directly, or paginated with less. Horizontal layout is always paginated. |
Nazar.config.formatter.boolean |
['✓', '✗'] | First item in array is a character for true, second for false |
Nazar.config.enable_shorthand_method |
true | Determines if shorthand method should be defined. See Opt-in setup for more details |
Nazar.config.table.resize |
true |
Determines if the table should always be resized to take full width |
Nazar.config.table.min_width |
nil |
Sets minimum width. By default uses the longest header length |
Nazar.config.table.max_width |
nil |
Determines maximum table width. If resize is set to true, uses terminal width by default, otherwise uses content length |
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/krzyzak/nazar.