Giter Site home page Giter Site logo

ripl's Introduction

Description

ripl is a light shell that encourages common middleware for shells i.e. rack for ruby shells. It is also a modular alternative to irb. Like irb, it loads ~/.irbrc, has autocompletion and keeps history in ~/.irb_history. Unlike irb, it is highly customizable via plugins and supports commands i.e. ripl-play. This customizability makes it easy to build custom shells (i.e. for a gem or application) and complex shells (i.e. for the web). Works on ruby 1.8.7 and greater.

Install

If you have readline, install ripl with:

gem install ripl

If you don’t have readline, first install ripl with a pure ruby readline:

gem install ripl rb-readline -- --without-readline

Then, add the following to ~/.riplrc:

Ripl.config[:readline] = 'rb-readline'
Ripl.config[:completion] = {:readline => :ruby }

Setup

To make your first ripl experience smoother, install these plugins:

# Adds multi-line evaluation
gem install ripl-multi_line

# Ignore errors caused by irb-specific configuration in ~/.irbrc
gem install ripl-irb

# Add to ~/.riplrc
require 'ripl/multi_line'
require 'ripl/irb'

Usage

$ ripl
>> ...

Documentation

To view ripl’s man page:

# If installed with rubygems
$ gem install gem-man
$ gem man ripl

To view ripl’s documentation online, read [the source](github.com/cldwalker/ripl/blob/master/man/ripl.1.ronn) or [the html version](rawgit.com/cldwalker/ripl/master/man/ripl.1.html).

Coming from irb

When first trying ripl, you may experience errors in your ~/.irbrc due to an irb-specific configuration. In order to have ripl and irb coexist peacefully, you should silence these errors. To silence them without touching your ~/.irbrc, install the ripl-irb gem. This ripl plugin fakes irb’s existence and points to ripl equivalents for your irb configurations. Otherwise, if you don’t mind modifying ~/.irbrc, wrap your irb-specific configuration in a block as follow:

if defined? IRB
  IRB.conf[:BLAH] = 'blah'
  # ...
end

Comparison to Irb

  • Similar to irb

    • Reads ~/.irbrc on startup

    • Appends to ~/.irb_history on exit

    • Autocompletion (from bond)

    • _ for last result

    • Type exit, quit or press Ctrl-D to exit

    • 6 common commandline options: -f, -r, -I, -d, -h, -v

    • IRB.conf -> Ripl.config

    • Handles Ctrl-C quietly

  • Enhancements over irb

    • ~290 lines (doc included) vs irb’s 5000+ lines

    • Easily extendable with plugins

    • Tests and documentation!

    • Customizable completion and completion of method arguments (from bond)

    • Easy to create custom shells for gems and apps i.e. Ripl.start

    • Easy to create and invoke ripl commands

    • Create console commands in a simple, modular way

    • Custom commandline options can be added via a plugin

    • ~/.irbrc errors caught

    • Well-integrated internationalization (see ripl-i18n)

  • Different from irb

    • No multi-line evaluation by default (but there is a plugin, ripl-multi_line).

    • No irb subsessions or workspaces (though ripl has jumps via ripl-commands)

    • Some IRB.conf features aren’t supported yet (see ripl-irb for details)

Note: Irb features not in ripl can be implemented as plugins.

Plugins

A ripl plugin is a module that is included into Ripl::Shell or extended into Ripl::Runner. Being simply modules, they can be packaged as gems and reused across shells as needed. ripl highly encourages plugins by loading them as early as possible and allowing them to extend most of ripl’s functionality.

As an example plugin, let’s color error messages red:

require 'ripl'

# To try place in ~/.riplrc
module Ripl
  module RedError
    def format_error(error)
      "\e[31m#{super}\e[m"
    end
  end
end
Ripl::Shell.include Ripl::RedError

Note this plugin extends format_error() by invoking the original format_error() with super. This is possible for any method that is available for extension by plugins. To see what methods are available for extension, see Ripl::Shell::API and Ripl::Runner::API.

If we want to add a config for this plugin, we can simply add a key to Ripl.config that matches the underscored version of the plugin name i.e. Ripl.config.

For available plugins, see Ripl Plugins below.

Configuration

Since ripl is highly customizable, it loads ~/.riplrc before it does anything. This ruby file should require and/or define plugins. Any ripl configurations via Ripl.config should also be done here. For an example ~/.riplrc, see mine.

Create Custom Shells

Creating and starting a custom shell is as simple as:

require 'ripl'
# Define plugins, load files, etc...
Ripl.start

Ripl.start takes options to customize your shell. For example if you wanted to start on a specific binding:

Ripl.start :binding => MyClass.instance_eval{ binding }

Create Commands

If you want to invoke your custom shell with ripl, make it a ripl command. To create one, create an executable in the format ripl-<command> and make sure it’s in your shell’s $PATH. For example, the file ripl-my_gem would be invoked with ripl my_gem. Note that with your command you can take arguments and parse your options as you please. For an example command, see ripl-rails.

Credits

  • janlelis and godfat for bug fix and tweaks

  • JoshCheek for bug fixes

  • postmodern for windows fixes and no history support

Bugs/Issues

Please report them on github.

Contributing

See here

Ripl Plugins

Ripl Shells

Shells built on top of ripl:

  • nirvana: A ruby web shell complete with autocomplete

  • fresh: An interesting ruby/system hybrid shell

  • ripl-johnson: A js shell based on johnson (firefox tracemonkey)

  • ronin: An exploit development platform using ripl for its console

  • tux: A sinatra shell

  • rack-webconsole: A rack middleware that adds a web shell to any rack app

Irb Alternatives

Some other irb alternatives to check out:

  • ir: nice and light

  • irb2: yehuda katz’s partial attempt at rewriting irb

  • dietrb: mac and ruby 1.9 specific

  • pry: featureful but heavy

ripl's People

Contributors

cldwalker avatar godfat avatar grandvizierolaf avatar janlelis avatar joshcheek avatar postmodern avatar wuputah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ripl's Issues

Consider using OptionParser in Ripl::Runner

Ripl::Runner contains some pretty DIY code to parse ripl's options. Since IRB predates OptionParser, they too wrote their own custom option parsing code as one giant case/when statement. It might be better to use OptionParser for more compliant option parsing.

fixed a test failure for my ruby

I am not sure which version of ruby would produce: SyntaxError: compile error upon syntax error, but my ruby gave me this: SyntaxError: (ripl):__LINE__: syntax error. So I think it might be good enough to test against /^SyntaxError:/

godfat@c5612bc

Thank you.

BTW, could we ignore pkg ?
godfat@a6a5f4e

BTW2, I am not sure why, but running rake test or bacon -q -Ilib -I. test/*_test.rb won't run test/shell_test.rb on my computer. This might be a bacon bug. I need to sleep right now, so I'll investigate this later...

p.s. ruby -v: ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.5.0]
gem list bacon: bacon (1.1.0), bacon-bits (0.1.0), bacon-rr (0.1.0)

Failed to load readline_line_buffer

I have installed ripl as well as the suggested plugins and also ripl-irb. Whenever I run ripl from the command line I'm getting this error: Bond Error: Failed to load readline_line_buffer. Ensure that it exists and was built correctly.

I'm on ubuntu 10.10 and I use ruby 1.8.7

Any idea?
Thanks!

Scrub backtraces of internal ripl methods

Backtraces generated while inside of Ripl include ripl/runner.rb, ripl/shell.rb and any additional Ripl plugins. It would be useful if Ripl could truncate the backtrace, starting at:

(ripl):31:in `<main>'

ripl explodes on any entered command unless ~/.irb_history exists

Unless ~/.irb_history exists running any command causes ripl to explode with the following stack trace:

$HOME/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/history.rb:12:in `get_input': undefined method `<<' for nil:NilClass (NoMethodError)
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/shell.rb:60:in `loop_once'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-multi_line-0.3.0/lib/ripl/multi_line.rb:37:in `block in loop_once'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-multi_line-0.3.0/lib/ripl/multi_line.rb:36:in `catch'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-multi_line-0.3.0/lib/ripl/multi_line.rb:36:in `loop_once'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/shell.rb:49:in `block in in_loop'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/shell.rb:49:in `catch'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/shell.rb:49:in `in_loop'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/shell.rb:30:in `loop'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/runner.rb:48:in `start'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/ripl-0.6.0/lib/ripl/runner.rb:31:in `run'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/gems/tux-0.3.0/bin/tux:7:in `<top (required)>'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/bin/tux:19:in `load'
    from /home/tkahn/.rvm/gems/ruby-1.9.2-p290/bin/tux:19:in `<main>'

running touch ~/.irb_history fixes this issue.

Running Ubuntu 11.04 with RVM ruby-1.9.2-p290.

Running a python script from within Ripl closes it

If I run a python script from within Ripl, like so:

>> exec('python hello_world.py')

(say hello_world.py just contains print('hello world'))
I can see the output of the script, but it closes Ripl. Is this intended? Is there a way around this?

The same issue is present in IRB as well.

Rearrnage Ripl::Shell.create and Ripl::Shell#initialize

Would you accept patches that rewrites Ripl::Shell.create and Ripl::Shell#initialize? I feel it would be easier for me to write plugins if I can simply rewrite them. I have too much weird or random initialization in my code, and it's very hard to explain it clearly, even showing with codes. I can show you where is causing problems in ripl-rc though.

For example:

https://github.com/godfat/ripl-rc/blob/5121c889bda85ba6fe3a686c3116dd3dd49261f1/lib/ripl/rc/anchor.rb#L47
https://github.com/godfat/ripl-rc/blob/5121c889bda85ba6fe3a686c3116dd3dd49261f1/lib/ripl/rc/anchor.rb#L67
https://github.com/godfat/ripl-rc/blob/5121c889bda85ba6fe3a686c3116dd3dd49261f1/lib/ripl/rc.rb#L4-11
https://github.com/godfat/ripl-rc/blob/5121c889bda85ba6fe3a686c3116dd3dd49261f1/lib/ripl/rc.rb#L39
https://github.com/godfat/ripl-rc/blob/5121c889bda85ba6fe3a686c3116dd3dd49261f1/lib/ripl/rc/test.rb#L3-4
https://github.com/godfat/ripl-rc/blob/359b713bc7ad3967e45332c7e2a35c28288ea756/test/test_squeeze_history.rb#L7-8

It would be nice if I can use a more unified way to initialize configs and shells.

Prompts ending with newline break history editing

Given this test file:

require 'ripl'
Ripl.config.merge! prompt:"\n", result_prompt:'#=> '
Ripl.start

And this series of keystrokes:

x = 42<enter><uparrow><backspace><enter>

This is the output you see:

x = 42
#=> 42

x = 42
#=> 4

Much worse things happen if you left-arrow into the historical line and attempt to make a minor correction.

I'm guessing that somewhere Ripl is asking for prompt.length and being lied to about how many characters on the line are from the prompt. Perhaps prompt[/.+\z/].length in the same sort of spot would fix this?


For the curious, I'm using this because I want scripts that do not call puts/p or other output to be able to be copy/pasted as valid Ruby programs, so I want "no prompt", but I want a blank line before each input (or after each result) to help make it clear when the REPL is ready for input.

Need to have multi-line support as default

The utility of ripl is much reduced by lacking multi-line support out of the box. Also how robust is the multi-line hack? is rescuing from SyntaxError exceptions all that is required? irb implemented its own ruby parser.

don't write duplicated input into history

I'm using this .irbrc on my comupter, and I really like that it won't write duplicated history into the history file. Sometime I would use irb for observing some result with periodically invoking a specific command. This really helps a lot.

godfat@78ff0d0

Thanks for your consideration.

One test failure on Windows

Windows Vista
ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

Running rake resulted in one failure:

c:\Users\djberge\Repositories\ripl>rake
(in c:/Users/djberge/Repositories/ripl)
bacon -q -Ilib -I. test/*_test.rb
......F..........................
Bacon::Error: "".=~(/^ripl: Error while loading ~\/.riplrc:\nSyntaxError:/) failed
        ./test/runner_test.rb:55: riplrc - catches and prints error
        (eval):8:in `it'
        (eval):4:in `it'
        ./test/runner_test.rb:52
        C:/Ruby/lib/ruby/gems/1.8/gems/bacon-bits-0.1.0/lib/bacon/bits.rb:20:in `describe'
        ./test/runner_test.rb:42
        C:/Ruby/lib/ruby/gems/1.8/gems/bacon-bits-0.1.0/lib/bacon/bits.rb:20:in `describe'
        ./test/runner_test.rb:41
        ./test/runner_test.rb:3

33 tests, 54 assertions, 1 failures, 0 errors

Full backtrace

Is this necessary? Make a syntax error and get 30 lines of crap

full backtrace, why?

Utilizing Hirb in custom shell

Digging Ripl and the ability to create a custom shell!
This is probably fairly simple and I'm missing something...

I'd like to utilize Hirb tables and paging (with or without tables) when printing output to the screen in my custom shell inside Ripl::Shell#print_result. Could you point me in the right direction for getting this setup?

Thanks!

AT_EXIT hook like in IRB ?

I just discovered the hooks CONF[:AT_EXIT] feature in irb and I was wondering if there is a similar thing using ripl :)

My goal is to properly clean up some objects at exit

tab-completion crashes ripl.

It appears ripl's readline still uses old IRB methods when using tab-completion:

$ ripl 
>> [1,2].sel/home/hal/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/irb/completion.rb:37:in `block in <module:InputCompletor>': undefined method `conf' for IRB:Module (NoMethodError)
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/readline.rb:4:in `call'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/readline.rb:4:in `readline'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/readline.rb:4:in `get_input'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/shell.rb:35:in `loop_once'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/shell.rb:22:in `block in loop'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/shell.rb:22:in `catch'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/shell.rb:22:in `loop'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/runner.rb:45:in `start'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/lib/ripl/runner.rb:11:in `run'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/gems/ripl-0.2.1/bin/ripl:4:in `<top (required)>'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/bin/ripl:19:in `load'
from /home/hal/.rvm/gems/ruby-1.9.2-p0/bin/ripl:19:in `<main>'

How can I pump commands through Ripl?

I'm building a command line client for the Strava API, https://github.com/dblock/strava-ruby-cli.

Ripl comes in handy and I could easily throw in strava console that let me delegate anything from my client and do things like athlete.name.

I also would like to be able to just pass these commands on the command line and let Ripl evaluate them for me, without a prompt. How do I do that?

module Strava
  module Cli
    class Console < SimpleDelegator
      attr_reader :client

      def initialize(access_token)
        @client = Strava::Api::Client.new(access_token: access_token)
        super @client
      end

      def run(args)
        # HOW DO I DO THIS?
      end

      def start!
        Ripl.start(binding: binding, prompt: 'Strava> ')
      end
    end
  end
end

I can do this without ripl with just eval here, but it feels wrong.

Ill formatted .gemspec (when loading to heroku)

I just tried loading to heroku, and I get an invalid gemspec error:

WARNING:  #ArgumentError: Illformed requirement ["#Syck::DefaultKey:0x00000008c5eb50> 0.9.22"]>
...
WARNING:  Invalid .gemspec format in '/app/.bundle/gems/ruby/1.9.1/specifications/ripl-0.6.3.gemspec'

Any reason this should be popping up? I've got the latest version of ripl in my Gemfile...

Thanks

bacon-rr

Hi,

It looks like bacon-rr should be added as a development dependency to the gemspec.

Regards,

Dan

Why is ripl/completion bundled, but ripl-multi_line is not?

I find it odd that ripl/completion is bundled into ripl, and ripl has a runtime dependency on bond. I feel like either this should be moved out into it's own library, or some of the more commonly used ripl plugins (ripl-multi_line, ripl-auto_indent, ripl-color_result) should be moved into ripl (but not required by default). Personally, I would prefer the later, as those plugins makes ripl feel like a modern irb.

Cannot start a sub-shell within Ripl that has a different binding

While attempting to spawn a sub-shell within Ripl, I noticed that the :binding had no effect:

$ ripl
>> require 'ripl'
=> false
>> class Test
|    def test
|      puts 'foo'
|    end  
|  end  
=> nil
>> t = Test.new
=> #<Test:0x000000018e7dc0>
>> Ripl.start(:binding => t.instance_eval('binding'))
>> test
ArgumentError: wrong number of arguments (0 for 2..3)
    (ripl):8:in `test'
    (ripl):8:in `<main>'

Ripl.start returns the same shell on multiple invocations

I have a program that needs to jump in and out of the shell from different text files--each of which has its own separate Ruby binding environment. I could not get IRB to work right for various reasons and Pry seemed like overkill. So Ripl is the perfect fit--thanks for making it.

For my purposes I first tried Ripl.start :binding => my_binding. As hinted above, the workflow is that the user enters Ripl, does some stuff, and exits. Then without leaving the program the user enters Ripl again from a different context (i.e., different binding) to do other stuff. This jumping in and out of different bindings might happen several times.

The problem is that I quickly learned that with Ripl.start I kept finding myself in the same binding (the first one) on the second or third invocation, even if setting the :binding option. Trying Ripl.config[:binding] didn't help either. The root of the problem, I think, is this code:

def self.shell(options={})
  @shell ||= Shell.create(config.merge!(options))
end

Ripl.start ends up calling Ripl.shell. The first time through Shell.create is called and @shell is set. Any subsequent invocation just returns @shell instead of creating a new shell.

I'm not sure what the best solution should be (or else I might have a pull request for you). My workaround was to start things from a lower level by going directly to Ripl::Shell with something like this:

Ripl::Shell.create(options).loop

The options contain the binding and some other stuff. I also had to trigger the .riplrc to load. This works but the main downside is that @shell is not set so the eval("_ = Ripl.shell.result", @binding) calls don't work properly. I hacked in a setter for @shell to get around that.

Unclear how to use Ripl in a gem without modifying local files

I want to use Ripl in one of my gems. However, Ripl does not play nicely with .irbrc files that—surprise—include commands for IRB. The documented way to avoid this is to either:

  • Modify the .irbrc file manually — not an acceptable option for a gem to ask users to change their configuration, or
  • Install another gem (fine, I guess) and then create a new .riplrc file — again, unacceptable.

I assume that there's some way to use Ripl in code without a .riplrc file, but the documentation does not cover this.

How can I use Ripl from a library and get it to ignore "errors" in the .irbrc without modifying any files?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.