Fresh Ruby Enhanced SHell
Ruby
Switch branches/tags
Nothing to show
Pull request Compare This branch is 11 commits behind janlelis:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
.gemspec
.gitignore
CHANGELOG.rdoc
LICENSE
README.rdoc
Rakefile
deps.rip

README.rdoc

Fresh Ruby Enhanced SHell

We love Ruby. And we love the command line. So… the shell needs to be rubyfied ;).

How does it work?

Basically, fresh is a Ruby console like irb: Enter a Ruby expression and it gets evaluated. But not everything is interpreted as Ruby: The input is thrown against a regular expression to determine if it is meant to be a Ruby or a system command.

May sound like voodoo, but works surprisingly well in practice ;).

Get fresh

Install the gem with:

gem install ripl-fresh

Start it with:

ripl fresh

(or just fresh)

Note, that it will also load your .irbrc file. To avoid this, you can do:

ripl -f fresh

Usage & configuration options

For an example session, see this blog entry.

The main regexp to determine if the command should be interpreted as system command is similar to this one: /^[a-z_-]\s.*/i (match a single word followed by at least one space). It can be adjusted in Ripl.config[:fresh_match_regexp].

If this regexp matches or the input is a single word ([a-z_-]+), fresh searches through the Ripl.config[:fresh_ruby_words] array for exceptions that still should get interpreted as Ruby (e.g. def ).

Then, the Ripl.config[:fresh_system_words] arrays is checked, if it contains the command. This array defaults to your ENV['PATH']. You can add your own system commands there.

There is also a third kind of command mode (besides :ruby and :system): :mixed. They look and feel like system commands, but redirect to the Ruby method described by the first word. You can register them in Ripl.config[:fresh_mixed_words] (e.g. cd).

As a fallback, fresh checks Kernel.respond_to?, if there is a ruby method with that name.

If the regexp does match, but the command could not be found in any of the three word arrays, the command mode will be set to Ripl.config[:fresh_match_default] (:ruby). If the regexp does not match, Ripl.config[:fresh_default] is used (also defaults to :ruby).

Of course, there is a way to explicitly set your command mode: You can prefix your input with a space to force Ruby mode as well as you can prefix it with ^ to force system mode. The strings used for this can be customized in Ripl.config[:fresh_system_prefix] and Ripl.config[:fresh_ruby_prefix].

You need to take a look at get_input method in the source file to 100% understand the command mode detection way.

Prompt

There is Riplc.config[:fresh_prompt] option, which takes a wide range of possible values. You can pass in a proc or a direct string. Furthermore, you can pass a symbol to get one of the following:

  • :default - usual fresh (directory) prompt

  • :PS1 - use PS1 environment variable

  • :ripl - don't change prompt

  • :irb - use irbs :PROMPT_I (if set)

  • :simple - “>> ”

Defaults

# prefixes
Ripl.config[:fresh_system_prefix]  = %w[^]
Ripl.config[:fresh_ruby_prefix]    = [' ']
# word arrays
Ripl.config[:fresh_ruby_words]     = %w[begin case class def for if module undef unless until while puts warn print p pp ap raise fail loop require load lambda proc system]
Ripl.config[:fresh_system_words]   =
  ENV['PATH'].split(File::PATH_SEPARATOR).uniq.map {|e|
    File.directory?(e) ? Dir.entries(e) : []
  }.flatten.uniq - ['.', '..'] 
Ripl.config[:fresh_mixed_words]    = %w[cd] 
# main regexp
Ripl.config[:fresh_match_regexp]    = /^([a-z\/_-]+)\s+(?!(?:[=%*]|!=|\+=|-=|\/=))/i
# regex matched but word not in one of the three arrays, possible values: :ruby, :system, :mixed
Ripl.config[:fresh_match_default]   = :ruby
# regex did not match
Ripl.config[:fresh_default]         = :ruby
# configure prompt
Ripl.config[:fresh_prompt] = :default

Customization

Besides customizing your fresh with the configuration options, you can further enhance it with Ruby plugins, because it's based on ripl. Just install the ripl-plugin_name and add it to your .riplrc file:

require 'ripl/plugin_name'

Currently, most plugins enable colors and IRB -like features.

TODO

There are lots of things which can get better:

  • Improve auto-completion

  • More cool (and colorful?) :mixed Ruby commands

  • Improve interaction between system and Ruby commands

    • ripl-multi_line for system commands

    • Result of system commands should be available for ruby, not only printed to stdout

  • Respect “…” (single argument) for :mixed commands

  • Improve default configuration

  • Add tests

  • Fresh ideas

Feel free to fork in your improvements ;)

Other gems you might find interesting

  • rush - Another Ruby shell, different concept

  • ripl - An irb alternative, fresh is based on it

Copyright

Copyright © 2010 Jan Lelis <code-needs-smileys.com> released under the MIT license.

J-_-L