From ec5e25547a401141586c87621266f9cd68c59e3c Mon Sep 17 00:00:00 2001 From: icebaker Date: Thu, 11 May 2023 19:24:50 -0300 Subject: first commit --- controllers/interfaces/cli.rb | 30 +++++++++++++++++++ controllers/interfaces/repl.rb | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 controllers/interfaces/cli.rb create mode 100644 controllers/interfaces/repl.rb (limited to 'controllers/interfaces') 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 -- cgit v1.2.3