Class: CommandMonkey

Inherits:
Object
  • Object
show all
Defined in:
lib/command-monkey.rb

Overview

This library runs an interactive program, such as pacmd or irb, and provides a method to execute commands in the program and return the replies.

Instance Method Summary collapse

Constructor Details

#initialize(program, prompt) ⇒ CommandMonkey

program: the interactive program to execute prompt: regex detecting the interactive program’s prompt



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/command-monkey.rb', line 10

def initialize(program, prompt)
  @prompt = prompt

  # run the program in a separate thread so that this library does not block
  # the client program
  @operator = Fiber.new do |command|
    PTY.spawn program do |output, input, pid|
      @output = output

      get_reply

      loop do
        input.puts command
        command = Fiber.yield get_reply(command)
      end
    end
  end
end

Instance Method Details

#enter(command) ⇒ Object

Send a command to the pacmd session, and return the output



30
31
32
# File 'lib/command-monkey.rb', line 30

def enter(command)
  @operator.resume(command)
end

#filter_output(text) ⇒ Object

Captured output from the program is filtered through this method before being returned



36
37
38
# File 'lib/command-monkey.rb', line 36

def filter_output(text)
  text.strip
end

#strip_command_pattern(command) ⇒ Object

Return the pattern which needs to be removed, based on the command text



41
42
43
# File 'lib/command-monkey.rb', line 41

def strip_command_pattern(command)
  /\A\s+#{Regexp.quote command}/
end