Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
janlelis committed Nov 10, 2010
1 parent b33fc4b commit 53a8c0c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 150 deletions.
Binary file removed .README.swp
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.swp
*~
doc
pkg
95 changes: 63 additions & 32 deletions README
Original file line number Diff line number Diff line change
@@ -1,57 +1,78 @@
FancyIrb patches your IRB to create a smooth output experience.

I really like irb_rocket, which outputs the evaluation result as comment and colorized 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 it. It's not perfect, but it ...

Features
== Features
* Use fancy colors! You can colorize the prompts, irb errors, +stderr+ and +stdout+
* Output results as Ruby comment #=> (rocket)
* Define yourself, how to get the output value, using procs
* Output results as Ruby comment
* Enhance your output value, using procs

== Motivation
I really like the {irb_rocket}[https://github.com/genki/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}[https://github.com/cldwalker/hirb], {interactive_editor}[https://github.com/jberkel/interactive_editor], etc.

Usage
== 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_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 => nil,
:result_prompt => :blue,
:input_prompt => nil,
:irb_errors => :red,
:stderr => :light_red,
:stdout => :yellow,
:stdout => :dark_gray,
:input => nil,
:output => true, # wirble's colorization
},
: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 filter/enhance/log your output
:output_procs => [default_colorizer_proc], # you can modify/enhance/log your output
}

==== Available colors
=== Rocket mode

Rocket mode means: Output result as comment if there is enough space left on the terminal line.

=== 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]

==== Output procs
1234
the proc takes an IRB::Context object and should return the result string
FancyIrb.set_result_proc do |context|
=== Modify your output

You can modify how to get and display the input. The <tt>result_proc</tt> is a proc which takes the irb context object and should return the value. You can change it with <tt>FancyIrb.set_result_proc do (your code) end</tt>. After that, each proc in <tt>output_procs</tt> gets triggered. They take the value and can return a modified one. You can use the <tt>FancyIrb.add_output_proc</tt> 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
end
}

===== Default output procs
...
By default, there's only the colorizer proc ...
===== 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

Expand All @@ -63,21 +84,31 @@ By default, there's only the colorizer proc ...
:result_proc => proc{ |context|
context.last_value.awesome_inspect
}

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

== Digging further
FancyIrb.options
== TODO
=== Known bugs
* If stdout of the current command scrolls, the rocket is displayed not higher than the first terminal line
* 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
* Refactor some code duplications

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

== Known bugs
== Copyright / Credits

* if stdout of the current command scrolls, the rocket is displayed in the first terminal line
Inspired by the irb_rocket gem from genki.

Feel free to fix one ;)
Copyright (c) 2010 Jan Lelis, http://rbjl.net, released under the MIT license.

== Todos, maybe
* lengths without colors
* "always rocket"-mode
J-_-L
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "fancy_irb"
gem.summary = %q{FancyIrb patches your IRB to create a smooth output experience.}
gem.description = %q{FncyIrb patches your IRB to create a smooth output experience.
gem.description = %q{FancyIrb patches your IRB to create a smooth output experience.
* Use fancy colors! You can colorize the prompts, irb errors, +stderr+ and +stdout+
* Output results as Ruby comment #=> (rocket)
* Enhance your output value, using procs}
Expand Down
83 changes: 0 additions & 83 deletions fancy-irb.gemspec

This file was deleted.

50 changes: 50 additions & 0 deletions fancy_irb.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{fancy_irb}
s.version = "0.6.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jan Lelis"]
s.date = %q{2010-11-09}
s.description = %q{FncyIrb patches your IRB to create a smooth output experience.
* Use fancy colors! You can colorize the prompts, irb errors, +stderr+ and +stdout+
* Output results as Ruby comment #=> (rocket)
* Enhance your output value, using procs}
s.email = %q{mail@janlelis.de}
s.extra_rdoc_files = [
"LICENSE",
"README"
]
s.files = [
"LICENSE",
"README",
"Rakefile",
"VERSION",
"fancy-irb.gemspec",
"lib/fancy_irb.rb",
"lib/fancy_irb/irb_ext.rb"
]
s.homepage = %q{http://github.com/janlelis/fancy_irb}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{FancyIrb patches your IRB to create a smooth output experience.}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<wirble>, [">= 0"])
else
s.add_dependency(%q<wirble>, [">= 0"])
end
else
s.add_dependency(%q<wirble>, [">= 0"])
end
end

Binary file removed lib/.fancy_irb.rb.swp
Binary file not shown.
50 changes: 21 additions & 29 deletions lib/fancy_irb.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require 'stringio'
require 'wirble'

class << FancyIrb = Module.new
module FancyIrb
VERSION = ( File.read File.expand_path( '../VERSION', File.dirname(__FILE__)) ).chomp
end

class << FancyIrb
# setup instance variable accessors
attr_reader :options
def [](key, key2 = nil)
Expand All @@ -18,11 +22,12 @@ def [](key, key2 = nil)
attr_accessor :stdout_colorful

def start(user_options = {})
# track some irb stuff
@height_counter = []
@real_lengths = { :output => 1, :input_prompt => 9999 } # or whatever
@stdout_colorful = false

# set default and parse user options
# set defaults and parse user options
default_result_proc = proc{ |context|
if context.inspect?
context.last_value.inspect
Expand All @@ -41,29 +46,21 @@ def start(user_options = {})
}

default_options = {
:rocket_mode => true,
:rocket_prompt => '#=> ',
:result_prompt => '=> ',
:colorize => {
:rocket_prompt => nil,
:result_prompt => nil,
: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 => nil,
:stderr => nil,
:stdout => nil,
:input => :red,
:output => nil, # wirble's colorization
# :rocket_prompt => :blue,
# :result_prompt => nil,
# :input_prompt => nil,
# :irb_errors => :red,
# :stderr => :light_red,
# :stdout => :yellow,
# :input => nil,
# :output => true, # wirble's colorization
:irb_errors => :red,
:stderr => :light_red,
:stdout => :dark_gray,
:input => nil,
:output => true, # wirble's output colorization
},
:result_proc => default_result_proc,
:output_procs => [default_colorizer_proc],
:result_proc => default_result_proc, # how to get the output result
:output_procs => [default_colorizer_proc], # you can modify/enhance/log your output
}

@options = default_options
Expand All @@ -86,7 +83,7 @@ def start(user_options = {})
}

# hook code into IRB
require './fancy_irb/irb_ext' #TODO
require 'fancy_irb/irb_ext'

"Enjoy your FancyIrb :)"
end
Expand Down Expand Up @@ -124,8 +121,3 @@ def write_stream(stream, data, color = nil)
)
end
end

FancyIrb.start

# gets height bug
# wrong input colorization bug
Loading

0 comments on commit 53a8c0c

Please sign in to comment.