Sha256: c813a63a8926480a474262ab31313c32681051f139c7b5e7075300b547dd1bdb

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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)
        puts 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", nil
          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

1 entries across 1 versions & 1 rubygems

Version Path
ruboty-1.2.2 lib/ruboty/adapters/shell.rb