pedrozath/coltrane

View on GitHub
bin/coltrane-interactive

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))

require 'tty-reader'
require 'paint'

Class.new do
  attr_reader :reader

  def initialize
    @reader = TTY::Reader.new(interrupt: :exit)
    shell
  end

  def quit_cmd?(cmd)
    cmd =~ /quit|exit/
  end

  def ignored_cmd?(cmd)
    cmd =~ /shell/ ||
    cmd.strip == ''
  end

  def command
    reader.read_line(Paint['coltrane ', '#FF5A00'])
  end

  def shell
    command
    .tap        { |cmd| exit if quit_cmd?(cmd) }
    .tap        { |cmd| shell and return if ignored_cmd?(cmd) }
    .yield_self { |cmd| %x{coltrane #{cmd}} }
    .yield_self { |response| puts ["\n", response, "\n"].join }
    shell
  end
end.new