Project

shelley

0.0
No commit activity in last 3 years
No release in over 3 years
Shelley allows to convert your classes and method into a shell supporting nested commands, autocomplete and history.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

Shelley

A gem for converting your classes and method into a shell supporting nested commands, autocomplete and history.

Installation

Add this line to your application's Gemfile:

gem 'shelley'

And then execute:

$ bundle

Or install it yourself as:

$ gem install shelley

Usage

A simple shell

class Calculator
  def initialize
    @value = 0
  end

  def add(n)
    @value += n.to_i
  end

  def subtract(n)
    @value -= n.to_i
  end

  def multiply_by(n)
    @value *= n.to_i
  end

  def divide_by(n)
    @value /= n.to_i
  end

  def result
    puts @value
  end
end

command_registry = Shelley::CommandRegistry.new
command_registry.add_command(Calculator.new)
shell = Shelley::InteractiveShell.new(command_registry)
shell.start

Sample output

> add 3
> result
3
> add 5
> subtract 2
> result
6
> divide_by 3
> result
2
> multiply_by 10
> result
20
> divide_by 0
divided by 0
>

A shell with subcommands

class Timer
  def initialize
    @started_at = nil
    @elapsed = 0
  end

  def start
    return unless @started_at.nil?
    @started_at = Time.now
  end

  def stop
    return if @started_at.nil?
    @elapsed += Time.now - @started_at
    @started_at = nil
  end

  def elapsed
    elapsed = @elapsed
    elapsed += (Time.now - @started_at) unless @started_at.nil?
    puts elapsed
  end
end

command_registry = Shelley::CommandRegistry.new
command_registry.add_command(Calculator.new, 'calc')
command_registry.add_command(Timer.new, 'timer')
shell = Shelley::InteractiveShell.new(command_registry)
shell.start

Sample output

> calc add 1
> calc add 3
> timer start
> calc subtract 2
> calc result
2
> timer elapsed
10.156443773
> timer stop
> timer elapsed
12.239438967
> timer elapsed
12.239438967
> calc result
2

TODO

  • Support for adding single methods
  • Automatic argument convertion
  • Argument autocomplete
  • Refactor InteractiveShell and CommandRegistry in order to not expose the internal state of CommandRegistry