Sha256: c6d8a74fdfea66efd745d888f98aa53d88d6efcd59b86ac922f6dfb02fd4d8e2

Contents?: true

Size: 1015 Bytes

Versions: 5

Compression:

Stored size: 1015 Bytes

Contents

require "gm/notepad/input_handlers/default_handler"
module Gm
  module Notepad
    module InputHandlers
      # Responsible for handling shell out commands
      class ShellOutHandler < DefaultHandler
        option :shell_handler, default: -> { Container.resolve(:shell_handler) }

        SHELL_OUT_HANDLER_REGEXP = /^`/.freeze
        TO_OUTPUT_REGEXP = /^\>/.freeze

        def self.handles?(input:)
          return false unless input.match(SHELL_OUT_HANDLER_REGEXP)
          true
        end

        def after_initialize!
          input.sub!(SHELL_OUT_HANDLER_REGEXP,'')
          if input.match(TO_OUTPUT_REGEXP)
            input.sub!(TO_OUTPUT_REGEXP, '')
            to_output = true
          else
            to_output = false
          end
          response = shell_handler.call(input)

          input.for_rendering(
            text: response,
            to_interactive: true,
            to_output: to_output,
            expand_line: false
          )
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gm-notepad-0.0.18 lib/gm/notepad/input_handlers/shell_out_handler.rb
gm-notepad-0.0.17 lib/gm/notepad/input_handlers/shell_out_handler.rb
gm-notepad-0.0.16 lib/gm/notepad/input_handlers/shell_out_handler.rb
gm-notepad-0.0.15 lib/gm/notepad/input_handlers/shell_out_handler.rb
gm-notepad-0.0.14 lib/gm/notepad/input_handlers/shell_out_handler.rb