FancyIrb patches your IRB to create a smooth output experience.
Features
-
Use fancy colors! You can colorize the prompts, irb errors,
stderr
andstdout
-
Output evaluation results as Ruby comments (hash rocket)
-
Filter your output value using procs
Motivation
I really like the irb_rocket gem, which outputs Ruby results using a hash rocket 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 rubygem is compatible with other great irb gems like hirb, interactive_editor, etc.
Usage
require 'fancy_irb'
FancyIrb.start
You can pass an options hash. These are the default values:
= {
: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 => [:red, :bright],
:stdout => nil,
:input => nil,
:output => true, # wirb'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
:east_asian_width => false, # set to true if you have double-width characters (slower)
}
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.
Wrong display widths?
When using double-width unicode chars, you should set :east_asian_width
to true
. It is deactivated because of performance issues.
Customize colors
See paint
Modify your IRB 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?(Wirb) && FancyIrb[:colorize, :output]
Wirb.colorize_result value
else
value
end
}
FancyIrb on Windows?
You need ansicon and everything will be fine :D
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
Copyright / Credits
Inspired by the irb_rocket gem from genki.
Copyright © 2010-2012 Jan Lelis <rbjl.net> released under the MIT license.
J-_-L