Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
FancyIrb patches your IRB to create a smooth output experience.
Ruby
tree: 1dbd01d51d

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
fancy_irb.gemspec

README.rdoc

FancyIrb patches your IRB to create a smooth output experience.

Features

  • Use fancy colors! You can colorize the prompts, irb errors, stderr and stdout

  • Output evaluation results as Ruby comments

  • Enhance your output value using procs

Motivation

I really like the irb_rocket gem, which outputs the evaluation result as comment and colorizes errors. Unfortunately, the implementation leads to bugs, because it tries to run the whole command before printing anything to stdout. For this reason, I've rewritten (and extended) it.

This plugin is compatible with other great gems like hirb, interactive_editor, etc.

Usage

require 'fancy_irb'
FancyIrb.start

You can pass an options hash. These are the default values:

default_options = {
  :rocket_mode     => true,   # activate or deactivate #=> rocket output
  :rocket_prompt   => '#=> ', # prompt to use for the rocket
  :result_prompt   => '=> ',  # prompt to use for normal output
  :colorize => {              # colors hash. Set to nil to deactivate colorizing
    :rocket_prompt => :blue,
    :result_prompt => :blue,
    :input_prompt  => nil,
    :irb_errors    => :red,
    :stderr        => :light_red,
    :stdout        => :dark_gray,
    :input         => nil,
    :output        => true, # wirble's output colorization
   },
  :result_proc     => default_result_proc,       # how to get the output result (see below)
  :output_procs    => [default_colorizer_proc],  # you can modify/enhance/log your output
}

Rocket mode

Rocket mode means: Output result as comment if there is enough space left on the terminal line and stdout does not output more than the current terminal height.

Available colors

>> Wirble::Colorize::Color::COLORS.keys
=> [:light_purple, :yellow, :light_gray, :white, :black, :dark_gray, :red, :green, :light_green, :brown, :light_blue, :light_red, :cyan, :blue, :light_cyan, :purple, :nothing]

Modify your output

You can modify how to get and display the input. The result_proc is a proc which takes the irb context object and should return the value. You can change it with FancyIrb.set_result_proc do (your code) end. After that, each proc in output_procs gets triggered. They take the value and can return a modified one. You can use the FancyIrb.add_output_proc method for adding new output filter procs.

default_result_proc
default_result_proc = proc{ |context|
  if context.inspect?
    context.last_value.inspect
  else
    context.last_value
  end
}
default_colorizer_proc
default_colorizer_proc = proc{ |value|
  FancyIrb.real_lengths[:output] =    value.size
  if defined?(Wirble) && FancyIrb[:colorize, :output]
    Wirble::Colorize.colorize value
  else
    value 
  end
}

Example configurations

Default

FancyIrb.start

No colorization

FancyIrb.start :colorize => nil

Use awesome_print for inspecting

require 'ap'
FancyIrb.start :rocket_mode   => false,
               :colorize      => { :output => false,
                                   :result_prompt => :yellow },
               :result_proc   => proc{ |context|
                                  context.last_value.awesome_inspect
                                 }

Smileyfy output

FancyIrb.start
FancyIrb.add_output_proc do |value|
  value + ' :)'
end

TODO

Known bugs

  • Something is wrong with input-newline when colorizing input

  • Not all input methods are patched properly (to work with the rocket) –> focusing on the often used ones

Features, maybe

  • Count string lengths without ansi escape sequences (would be more flexible than remembering)

  • “Always rocket”-mode

  • Allow custom ansi escape strings instead of wirble's colors

  • Refactor some code duplications

Feel free to fix a bug or implement a todo ;)

Copyright / Credits

Inspired by the irb_rocket gem from genki.

Copyright © 2010 Jan Lelis, rbjl.net, released under the MIT license.

J-_-L

Something went wrong with that request. Please try again.