Sha256: 6b48e66f64688018d82c7d24fb0651de772a42d16acb19589d13d1567338f627

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

require "readline"

module Ruboty
  module Adapters
    class Shell < Base
      PROMPT = "> "

      SOURCE = "shell"

      USAGE = "Type `exit` or `quit` to end the session."

      attr_accessor :stopped

      def initialize(*args)
        super
        remember
      end

      def run
        explain
        listen
      end

      def say(message)
        Ruboty.logger.info(message[:body])
      end

      private

      def explain
        Ruboty.logger.info(USAGE)
      end

      def read
        Readline.readline(PROMPT, true).tap do |line|
          history_file.puts(line)
        end
      end

      def listen
        step until stopped?
      rescue Interrupt
        stop
      end

      def step
        case body = read
        when "exit", "quit"
          stop
        else
          robot.receive(body: body, source: SOURCE)
        end
      end

      def stopped?
        !!stopped
      end

      def stop
        self.stopped = true
      end

      def remember
        if history_pathname.exist?
          history_pathname.each_line do |line|
            Readline::HISTORY << line.rstrip
          end
        end
      end

      def history_pathname
        @history_pathname ||= Pathname.new("~/.ruboty_history").expand_path
      end

      def history_file
        @history_file ||= history_pathname.open("a").tap do |file|
          file.sync = true
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ruboty-1.1.7 lib/ruboty/adapters/shell.rb
ruboty-1.1.6 lib/ruboty/adapters/shell.rb
ruboty-1.1.5 lib/ruboty/adapters/shell.rb
ruboty-1.1.4 lib/ruboty/adapters/shell.rb
ruboty-1.1.3 lib/ruboty/adapters/shell.rb
ruboty-1.1.2 lib/ruboty/adapters/shell.rb
ruboty-1.1.1 lib/ruboty/adapters/shell.rb
ruboty-1.1.0 lib/ruboty/adapters/shell.rb
ruboty-1.0.4 lib/ruboty/adapters/shell.rb
ruboty-1.0.3 lib/ruboty/adapters/shell.rb
ruboty-1.0.2 lib/ruboty/adapters/shell.rb
ruboty-1.0.1 lib/ruboty/adapters/shell.rb
ruboty-1.0.0 lib/ruboty/adapters/shell.rb