FancyIrb patches your IRB to create a smooth output experience.
-
Use fancy colors! You can colorize the prompts, irb errors,
stderr
andstdout
-
Output evaluation results as Ruby comments
-
Enhance your output value using procs
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.
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, # 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 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.
When using double-width unicode chars, you should set :east_asian_width
to true
. It is deactivated because of performance issues.
See <tt>Wirb::COLORS.keys</tt>
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 = proc{ |context| if context.inspect? context.last_value.inspect else context.last_value end }
default_colorizer_proc = proc{ |value| FancyIrb.real_lengths[:output] = value.size if defined?(Wirb) && FancyIrb[:colorize, :output] Wirb.colorize_result value else value end }
You need ansicon and everything will be fine :D
FancyIrb.start
FancyIrb.start :colorize => nil
require 'ap' FancyIrb.start :rocket_mode => false, :colorize => { :output => false, :result_prompt => :yellow }, :result_proc => proc{ |context| context.last_value.awesome_inspect }
FancyIrb.start FancyIrb.add_output_proc do |value| value + ' :)' end
-
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
-
Count string lengths without ansi escape sequences (would be more flexible than remembering)
-
“Always rocket”-mode
Feel free to fix a bug or implement a todo ;)
Inspired by the irb_rocket gem from genki.
Copyright © 2010-2011 Jan Lelis <rbjl.net> released under the MIT license.
J-_-L