diff options
author | icebaker <icebaker@proton.me> | 2023-12-16 21:09:29 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-12-16 21:09:29 -0300 |
commit | 55a717a548375e7fed0b02fbd9d69aae167d98ec (patch) | |
tree | 1f9b121ca28db627d079ad488adca41bc2ddaaa3 /controllers/interfaces | |
parent | 3cfd5ee507ead55322cebe514b383ffc50c41728 (diff) |
improving repl experience
Diffstat (limited to 'controllers/interfaces')
-rw-r--r-- | controllers/interfaces/repl.rb | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb index fd16ea6..bf581a7 100644 --- a/controllers/interfaces/repl.rb +++ b/controllers/interfaces/repl.rb @@ -10,6 +10,21 @@ module NanoBot module Controllers module Interfaces module REPL + COMMANDS_TO_BE_REMOVED = [ + 'help', 'cd', 'find-method', 'ls', 'pry-backtrace', 'raise-up', 'reset', 'watch', + 'whereami', 'wtf?', '!', 'amend-line', 'edit', 'hist', 'show-input', 'ri', 'show-doc', + 'show-source', 'stat', 'import-set', 'play', '!!!', '!!@', '$', '?', '@', 'file-mode', + 'history', 'quit', 'quit-program', 'reload-method', 'show-method', 'cat', + 'change-inspector', 'change-prompt', 'clear-screen', 'fix-indent', 'list-inspectors', + 'save-file', 'shell-mode', 'pry-version', 'reload-code', 'toggle-color', '!pry', + 'disable-pry', 'jump-to', 'nesting', 'switch-to', + 'pry-theme' + ].freeze + + COMMANDS_TO_KEEP = [ + '/whereami[!?]+/', '.<shell command>', 'exit', 'exit-all', 'exit-program' + ].freeze + def self.boot(cartridge, session, prefix = nil, suffix = nil, as: 'repl') return unless Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction]) @@ -31,13 +46,7 @@ module NanoBot prompt = self.prompt(cartridge) - Pry.config.prompt = Pry::Prompt.new( - 'REPL', - 'REPL Prompt', - [proc { prompt }, proc { 'MISSING INPUT' }] - ) - - Pry.commands.block_command(/(.*)/, 'handler') do |line| + handler = proc do |line| session.print(prefix) unless prefix.nil? session.evaluate_and_print(line, mode: 'repl') session.print(suffix) unless suffix.nil? @@ -45,7 +54,43 @@ module NanoBot session.flush end - Pry.start + pry_prompt = Pry::Prompt.new( + 'REPL', + 'REPL Prompt', + [proc { prompt }, proc { 'MISSING INPUT' }] + ) + + pry_instance = Pry.new({ prompt: pry_prompt }) + + pry_instance.config.correct_indent = false + + pry_instance.config.completer = Struct.new(:initialize, :call) do + def initialize(...); end + def call(...); end + end + + first_whereami = true + + pry_instance.config.commands.block_command(/whereami --quiet(.*)/, '/whereami[!?]+/') do |line| + unless first_whereami + handler.call(line.nil? ? 'whereami --quiet' : "whereami --quiet#{line}") + end + first_whereami = false + end + + pry_instance.config.commands.block_command(/\.(.*)/, '.<shell command>') do |line| + handler.call(line.nil? ? '.' : ".#{line}") + end + + COMMANDS_TO_BE_REMOVED.each do |command| + pry_instance.config.commands.block_command(command, 'handler') do |line| + handler.call(line.nil? ? command : "#{command} #{line}") + end + end + + pry_instance.commands.block_command(/(.*)/, 'handler', &handler) + + Pry::REPL.new(pry_instance).start end def self.prompt(cartridge) |