Class: Gm::Notepad::InputHandlers::ShellOutHandler

Inherits:
DefaultHandler show all
Defined in:
lib/gm/notepad/input_handlers/shell_out_handler.rb

Overview

Responsible for handling shell out commands

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DefaultHandler

build_if_handled, #initialize, #lines

Constructor Details

This class inherits a constructor from Gm::Notepad::InputHandlers::DefaultHandler

Class Method Details

.handles?(input:) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/gm/notepad/input_handlers/shell_out_handler.rb', line 12

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

Instance Method Details

#after_initialize!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gm/notepad/input_handlers/shell_out_handler.rb', line 17

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