Sha256: a161e0778813fd8bbab62855684d293511b87b67f369cb6ab504804085259fa1

Contents?: true

Size: 1.16 KB

Versions: 38

Compression:

Stored size: 1.16 KB

Contents

require "readline"

class Ppl::Command::Shell < Ppl::Application::Command

  name        "shell"
  description "Interactive mode"

  attr_writer :format

  def options(parser, options)
    parser.banner = "usage: ppl shell"
  end

  def execute(input, output)
    begin
      shell(input, output)
    rescue SystemExit, Interrupt
      terminate_gracefully(input, output)
      false
    end
  end


  private

  def shell(input, output)
    welcome_user(input, output)
    while line = read_line(input)
      break if ["exit", false].include?(line)
      process_line(line)
    end
    terminate_gracefully(input, output)
    true
  end

  def welcome_user(input, output)
    if input.stdin.tty?
      output.line("ppl #{Ppl::Version} (type \"exit\" to leave)")
    end
  end

  def read_line(input)
    prompt = determine_prompt(input.stdin)
    line   = Readline.readline(prompt, true)
  end

  def process_line(line)
    command = "#{$0} #{line}"
    Kernel.system(command)
  end

  def determine_prompt(io)
    if io.tty?
      "ppl> "
    else
      ""
    end
  end

  def terminate_gracefully(input, output)
    if input.stdin.tty?
      output.line("")
    end
  end

end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
ppl-3.0.1 lib/ppl/command/shell.rb
ppl-3.0.0 lib/ppl/command/shell.rb
ppl-2.4.1 lib/ppl/command/shell.rb
ppl-2.4.0 lib/ppl/command/shell.rb
ppl-2.3.3 lib/ppl/command/shell.rb
ppl-2.3.2 lib/ppl/command/shell.rb
ppl-2.3.1 lib/ppl/command/shell.rb
ppl-2.3.0 lib/ppl/command/shell.rb
ppl-2.2.0 lib/ppl/command/shell.rb
ppl-2.1.0 lib/ppl/command/shell.rb
ppl-2.0.0 lib/ppl/command/shell.rb
ppl-1.25.0 lib/ppl/command/shell.rb
ppl-1.24.0 lib/ppl/command/shell.rb
ppl-1.23.0 lib/ppl/command/shell.rb
ppl-1.22.2 lib/ppl/command/shell.rb
ppl-1.22.1 lib/ppl/command/shell.rb
ppl-1.22.0 lib/ppl/command/shell.rb
ppl-1.21.0 lib/ppl/command/shell.rb
ppl-1.20.0 lib/ppl/command/shell.rb
ppl-1.19.0 lib/ppl/command/shell.rb