diff options
author | icebaker <icebaker@proton.me> | 2023-05-13 10:45:32 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-05-13 10:45:32 -0300 |
commit | d83ec5192442a9bd8d914c5a4856917b9653583c (patch) | |
tree | ca97e1618dd937594dc1da0a13584a46b87613eb /controllers/instance.rb | |
parent | 805e5073117bc717f46da0fa7f8ab628b4fa2a7d (diff) |
improving streams
Diffstat (limited to 'controllers/instance.rb')
-rw-r--r-- | controllers/instance.rb | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/controllers/instance.rb b/controllers/instance.rb index f3cbd74..243e266 100644 --- a/controllers/instance.rb +++ b/controllers/instance.rb @@ -12,12 +12,14 @@ require_relative './session' module NanoBot module Controllers class Instance - def initialize(cartridge_path:, state: nil) + def initialize(cartridge_path:, stream:, state: nil) + @stream = stream + load_cartridge!(cartridge_path) provider = Components::Provider.new(@cartridge[:provider]) - @session = Session.new(provider:, cartridge: @cartridge, state:) + @session = Session.new(provider:, cartridge: @cartridge, state:, stream: @stream) end def debug @@ -26,18 +28,42 @@ module NanoBot def eval(input) Interfaces::Eval.evaluate(input, @cartridge, @session) + + return unless @stream.is_a?(StringIO) + + @stream.flush + result = @stream.string.clone + @stream.truncate(0) + @stream.rewind + result end def repl + if @stream.is_a?(StringIO) + @stream.flush + @stream = $stdout + @session.stream = @stream + end Interfaces::REPL.start(@cartridge, @session) end private def load_cartridge!(path) + elected_path = if path.strip == '-' + File.expand_path('../static/cartridges/default.yml', __dir__) + else + Components::Storage.cartridge_path(path) + end + + if elected_path.nil? + @stream.write("Cartridge file not found: \"#{path}\"\n") + raise StandardError, "Cartridge file not found: \"#{path}\"" + end + @cartridge = Logic::Helpers::Hash.symbolize_keys( YAML.safe_load( - File.read(Components::Storage.cartridge_path(path)), + File.read(elected_path), permitted_classes: [Symbol] ) ) |