We love Ruby. And we love the command line. So… the shell needs to be rubyfied ;).
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 ;).
Install the gem with:
gem install ripl-fresh
Start it with:
(or just fresh)
Note, that it will also load your .irbrc file. To avoid this, you can do:
ripl -f fresh
Just start the shell and play around, to get a fresh feeling. 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: /^\w+\s+.*/ (match a single word followed by at least one space). See below for details.
You can use the output of system commands and redirect it to a Ruby variable (or similar Ruby expression) like this:
ls => variable
Please note: The “=> variable” part has to be at the line ending. The output of the ls command is now stored as array in variable and can be used in the next line. There are three variations of this command:
=>> append result to the array specified (or create it)
~> use command as string instead of an array
~>> use command as string instead of an array and append (or create it)
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 - “>> ”
There are three different command modes: :ruby, :system and :mixed.
:ruby is usual Ruby, :system means system command and :mixed looks like system command, but is just redirected to the Ruby method with that name.
The input is matched against the regexps in Ripl.config[:fresh_patterns]. These regexps also contain named groups, to determine the command or if the command should be stored in a variable.
When a regexp has matched, the command is searched for in Ripl.config[:fresh_ruby_commands], Ripl.config[:fresh_system_commands] and Ripl.config[:fresh_mixed_commands] (in this order).
The ruby command array contains some common Ruby words, e.g. def . The system command array is set to your ENV['PATH']. The mixed array currently contains only cd. You can adjust the arrays as you want to.
As a fallback, fresh checks Kernel.respond_to?, if there is a ruby method with that name.
If the regexp did match, but the command could not be found in any of the three word arrays and respond_to? has not been successful, the command mode will be set to Ripl.config[:fresh_unknown_command_mode] (default :ruby). If the regexp did not match, Ripl.config[:fresh_default_mope] is used (default :ruby).
You need to take a look at get_input method in the source file to 100% understand the command mode detection way.
The Ripl.config[:fresh_patterns] contains three Regexps, which do the following:
match ^ to force system mode
match a single word
match a word followed by a space
See lib/ripl/fresh/config.rb for all configuration options and its defaults.
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:
There are lots of things which can get better:
Refactor a little bit
RVM support possible?
More cool (and colorful?) :mixed Ruby commands
Respect “…” (single argument) for :mixed commands
Be compatible (and installable) with JRuby/Rubinius
Feel free to fork in your improvements ;)
rush - Another Ruby shell, different concept
ripl - An irb alternative, fresh is based on it
rubsh - Some similar ideas
urchin - More unix like approach
rubish - And another approach
Copyright © 2010 Jan Lelis <code-needs-smileys.com> released under the MIT license.