How to paginate awesome_print results inside pry - ruby-on-rails

I'm using awesome_print gem to display output from pry to look pretty. Since, awesome_print uses line breaks, long outputs like ap html.chapters.order("position ASC") only shows the end part of the output, How Do I paginate the result to look like its, piped to less or more
Thanks.

Add this to the .pryrc:
begin
require 'awesome_print'
Pry.config.print = proc { |output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output) }
rescue LoadError => err
puts "no awesome_print :("
end
https://github.com/pry/pry/wiki/FAQ#wiki-awesome_print
And in case you are unfamiliar with .pryrc:
When pry starts, it checks for a .pryrc file in your home
directory(~/.pryrc) and also for a per-project .pryrc in the current
directory(./.pryrc). Both files are used if they exist, with the file
from your home directory being loaded first.
https://github.com/pry/pry/wiki/Pry-rc

Related

Permanently add a directory to Ruby $LOAD_PATH

I'd like the option to use awesome_print in every IRB console or Rails console.
The IRB console is pretty much working satisfactorily right now. If I run irb, I can type require 'awesome_print' and it works.
The Rails console isn't as easy. require 'awesome_print' doesn't work. I apparently have to do this:
> $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'
After that, require 'awesome_print' works fine.
But I definitely don't want to have to type $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib' and then require 'awesome_print' every single time I open a Rails console just to be able to use awesome_print. That seems ridiculous.
So, how can I permanently add a path to Ruby's $LOAD_PATH?
Note: I don't want to add awesome_print to the Gemfile of any particular project. I want awesome_print to be available to all my Ruby/Rails projects.
You could simply use a a ~/.irbrc file and do:
require 'awesome_print'
Now, open up another IRB prompt:
irb(main):003:0> ap hash
{
"a" => "b"
}
Edit: this isn't working in rails, seems to be a known issue.
puts the following to the .irbrc:
to_load = %w[
awesome_print
coderay
hirb
pry
pry-doc
pry-remote
pry-theme
slop
yard
].join('|')
regexp = Regexp.new( "(#{to_load})" )
Gem.path.each do |path|
Dir.new("#{path}/gems").each do |gem_path|
next if %w[ . .. ].any?{ |d| gem_path == d }
new_el = "#{path}/gems/#{gem_path}/lib"
$LOAD_PATH << new_el if new_el =~ regexp
end
end

Custom irb not printing objects

Trying to write a custom irb for a gem to ease debugging. At the point where the shell loads and you can use it like ruby console but running into this wall
MyClass.get_last_instance
=>
_.attributes
=> {'attribute'=> 'test'}
The instance was found but a blank string is echo'ed. Here are the requires involved in starting the shell
require 'irb'
require 'irb/completion'
require 'debugger'
I tried reading through the rails source code, didn't get very far, mostly because I didn't really know what I was looking for. I think I'm just missing a require of a part of rails that echos objects.
create a .irbrc in your home path for ubuntu/osx and use the below code, it will work. Also you can add additional gems also debugger or irb
# print SQL to STDOUT
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
end
# Autocomplete
require 'irb/completion'
# Prompt behavior
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
# History
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# Easily print methods local to an object's class
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
# copy a string to the clipboard
def pbcopy(string)
`echo "#{string}" | pbcopy`
string
end
require "rubygems"

Colors in irb / rails console

I'm testing a gem that outputs color in the terminal:
module Color
def self.colorize(text, color_code)
"#{color_code}#{text}e[0m"
end
def self.red(text)
self.colorize(text, "\033[1;31;12m")
end
end
I have a testing file in the same directory, called color_test.rb:
require_relative 'color.rb'
puts Color.red('I should be red')
This results in the following:
$ ruby color_test.rb
I should be red
And the test is actually red. Horray. However, the same is not happening in the rails console:
$ rails c
Loading development environment (Rails 4.1.1)
2.0.0-p247 :001 > require 'color'
=> true
2.0.0-p247 :003 > Chroma.colourise('text',"\033[1;31;12m")
=> "\e[1;31;12mtexte[0m"
So how do I escape it? (If that's even the term :P) I want to be able to display bold text and other styles in the console as well.
This is just for testing, so I'm okay with downloading some sort of extension for the rails console, however if there's a way to package this functionality in the gem and give the console colors, that would be pretty cool so if someone could show me how I'd be glad.
Try this:
text = 'red text'
puts "\033[31m#{text}\033[0m"
Another option is to extend String class
class String
def red
"\033[31m#{self}\033[0m"
end
def green
"\033[32m#{self}\033[0m"
end
end
Then you could do something like 'spinach'.green

Using Hirb and Awesome Print with Pry Rails

I've been using Pry with Rails for a while via the Pry-Rails Gem.
I want to add Hirb and Awesome Print to Pry, so I've added initialisation code to my ~/.pryrc file as described here and here:
# ~/.pryrc
require 'rubygems'
# Hirb for Tables
begin
require 'hirb'
Hirb.enable
old_print = Pry.config.print
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || old_print.call(output, value)
end
rescue LoadError => err
puts "no hirb :("
end
# Awesome Print
begin
require 'awesome_print'
Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError => err
puts "no awesome_print :("
end
However, when I run $rails c Pry can't find either Hirb or Awesome print.
Why is this?

ruby on rails roo gem cannot load zip/zipfilesystem

I am attempting to use the roo gem to process .xlsx spreadsheets that are uploaded by an outside party. I'm getting the following error:
LoadError (cannot load such file -- zip/zipfilesystem):
I've found a lot of questions similar to this one (such as cannot load such file -- zip/zip) and I've tried to follow their solutions. So far, to no avail.
I originally required 'roo' in the controller, and after getting this error tried requiring 'zip/zip', 'zip/zipfilesystem', and just 'zip'. None of these seem to fix anything. I've also tried adding :require => 'zip', :require => 'zip/zipfilesystem', :require => 'zip/zip' to the Gemfile, and none of that seemed to change anything. Here is some pertinent code:
in Gemfile:
# for spreadsheet upload management
gem 'roo'
gem 'rubyzip'
gem 'spreadsheet'
gem 'nokogiri'
installed versions:
nokogiri (1.6.0)
roo (1.12.1)
rubyzip (1.0.0)
spreadsheet (0.8.9)
in Controller:
require 'roo'
module BatchOrderProcessing
class DataFilesController < ApplicationController
def create
# some code here ...
when ".xlsx"
spreadsheet = Roo::Excelx.new(uploaded_io.path, nil, :ignore)
header = spreadsheet.row(1)
if # some validation stuff...
puts "spreadsheet format inappropriate"
redirect_to # some place
end
process_datafile(fname, spreadsheet)
# more code ...
end
private
def process_datafile(fname, spreadsheet)
#df = DataFile.new
#df[:filename] = ActiveRecord::Base.connection.quote(fname)
if #df.save
begin
# parse asynchronously
datafile_scheduler = Rufus::Scheduler.new
datafile_scheduler.in '3s' do
#df.process_spreadsheet(spreadsheet)
end
redirect_to #df
rescue => e
# more code ...
end
else
# more code ...
end
end
I think this thing is crapping out before it gets to the model (where the process_spreadsheet() code is), but just in case, here's some model code:
def process_spreadsheet(spreadsheet)
# do some stuff
puts "parsing spreadsheet"
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
row_array << row
invoice << row.to_s
# some more code....
dfi = DataFileItem.new()
dfi.attributes = row.to_hash.slice(*accessible_attributes)
dfi.data_file_id = self.id
dfi.save
self.data_file_items << dfi
# Update stuff in our DB based on rows in row_array...
end
I'm using rails 3.2.13 and ruby 2.0.0p195.
Am I requiring the wrong thing (or in the wrong way) somewhere? Let me know if any other code snippets would be helpful. Thaaaaanks.
rubyzip v1.0.0 was released 29 August 2013: https://github.com/rubyzip/rubyzip/releases
This is a new major version number, and more than one gem or project that depends on this has been caught out by the break with backwards-compatibility.
The quickest "get my code working like before" fix is to alter Gemfile reference to rubyzip:
gem 'rubyzip', '< 1.0.0'
In the longer-term, this may not be the best fix, it depends on how and/or why you are using rubyzip. I expect some gem publishers such as roo's authors will need to figure out how to transition nicely so that their own users don't end up with simultaneous requirements for incompatible versions of rubyzip.
Just opinion:
Seeing this in action has actually made me much less a fan of Ruby gems semantic versioning for major versions. If I ever break with backwards compatibility on any of my own projects, I think I'll just start a new gem, and put a notice on the old gem.
Try adding the zip-zip gem to your project. It provides a simple adapter for your dependencies using the RubyZip v0.9.9 interface allowing you to upgrade to RubyZip v1.0.0.
This has been fixed in the roo gem. You need to update to version 1.12.2 or higher to fix. See the issue here: https://github.com/Empact/roo/pull/65

Resources