summaryrefslogtreecommitdiff
path: root/controllers/interfaces/repl.rb
blob: 9415674eed325f5f0e1e31b404e308c6ac3ff9b9 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require 'pry'
require 'rainbow'

require_relative '../../logic/helpers/hash'

module NanoBot
  module Controllers
    module Interfaces
      module REPL
        def self.start(cartridge, session)
          prefix = build_prefix(cartridge)
          postfix = build_postfix(cartridge)

          if Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction])
            session.print(prefix) unless prefix.nil?
            session.boot(mode: 'repl')
            session.print(postfix) unless postfix.nil?
            session.print("\n")
          end

          prompt = build_prompt(Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl prompt]))

          Pry.config.prompt = Pry::Prompt.new(
            'REPL',
            'REPL Prompt',
            [proc { prompt }, proc { 'MISSING INPUT' }]
          )

          Pry.commands.block_command(/(.*)/, 'handler') do |line|
            session.print(postfix) unless postfix.nil?
            session.evaluate_and_print(line, mode: 'repl')
            session.print(postfix) unless postfix.nil?
            session.print("\n")
            session.flush
          end

          Pry.start
        end

        def self.build_prefix(cartridge)
          repl = Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl])
          return "\n" if repl.nil? || !repl.key?(:prefix) # default

          repl[:prefix]
        end

        def self.build_postfix(cartridge)
          repl = Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl])
          return "\n" if repl.nil? || !repl.key?(:postfix) # default

          repl[:postfix]
        end

        def self.build_prompt(prompt)
          result = ''

          if prompt.is_a?(Array)
            prompt.each do |partial|
              result += if partial[:color]
                          Rainbow(partial[:text]).send(partial[:color])
                        else
                          partial[:text]
                        end
            end
          elsif prompt.is_a?(String)
            result = prompt
          else
            result = "🤖#{Rainbow('> ').blue}"
          end

          result
        end
      end
    end
  end
end