diff options
author | icebaker <icebaker@proton.me> | 2023-05-11 19:24:50 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-05-11 19:24:50 -0300 |
commit | ec5e25547a401141586c87621266f9cd68c59e3c (patch) | |
tree | 547b3c7fa04c9e695785b9beeda0be5a4a77b006 /controllers/interfaces |
first commit
Diffstat (limited to 'controllers/interfaces')
-rw-r--r-- | controllers/interfaces/cli.rb | 30 | ||||
-rw-r--r-- | controllers/interfaces/repl.rb | 67 |
2 files changed, 97 insertions, 0 deletions
diff --git a/controllers/interfaces/cli.rb b/controllers/interfaces/cli.rb new file mode 100644 index 0000000..473ab7b --- /dev/null +++ b/controllers/interfaces/cli.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require_relative '../instance' + +module NanoBot + module Controllers + module Interfaces + module CLI + def self.handle! + params = { cartridge_path: ARGV[0], state: ARGV[1], command: ARGV[2] } + + bot = Instance.new(cartridge_path: params[:cartridge_path], state: params[:state]) + + case params[:command] + when 'eval' + params[:input] = ARGV[3..]&.join(' ') + params[:input] = $stdin.read.chomp if params[:input].nil? || params[:input].empty? + bot.eval(params[:input]) + when 'repl' + bot.repl + when 'debug' + bot.debug + else + raise "TODO: [#{params[:command]}]" + end + end + end + end + end +end diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb new file mode 100644 index 0000000..7b53eb2 --- /dev/null +++ b/controllers/interfaces/repl.rb @@ -0,0 +1,67 @@ +# 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) + if Logic::Helpers::Hash.fetch( + cartridge, %i[interfaces repl prefix] + ) + session.print(Logic::Helpers::Hash.fetch(cartridge, + %i[interfaces repl prefix])) + end + + session.boot(mode: 'repl') + + session.print(Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl postfix]) || "\n") + + session.flush + + prompt = build_prompt(cartridge[:interfaces][:repl][:prompt]) + + Pry.config.prompt = Pry::Prompt.new( + 'REPL', + 'REPL Prompt', + [proc { prompt }, proc { 'MISSING INPUT' }] + ) + + Pry.commands.block_command(/(.*)/, 'handler') do |line| + if Logic::Helpers::Hash.fetch( + cartridge, %i[interfaces repl prefix] + ) + session.print(Logic::Helpers::Hash.fetch( + cartridge, %i[interfaces repl prefix] + )) + end + + session.evaluate_and_print(line, mode: 'repl') + session.print(Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl postfix]) || "\n") + session.flush + end + + Pry.start + end + + def self.build_prompt(prompt) + result = '' + + prompt.each do |partial| + result += if partial[:color] + Rainbow(partial[:text]).send(partial[:color]) + else + partial[:text] + end + end + + result + end + end + end + end +end |