module CTF::IOWithEcho

Attributes

echo[RW]
echo_color[RW]
echo_input[RW]
echo_output[RW]

Public Instance Methods

expect(pattern, timeout = 9999999) click to toggle source

TODO: Don't ignore timeout

# File lib/ctf/io_with_echo.rb, line 45
def expect(pattern, timeout = 9999999)
  result = nil
  loop do
    if pattern.is_a?(Regexp)
      break if result && result.match(pattern)
    else
      break if result && result.end_with?(pattern)
    end
    data = getc
    break unless data
    if result
      result << data.chr
    else
      result = data.chr
    end
  end
  return result unless result
  if pattern.is_a?(Regexp)
    [result] + (result.match(pattern) || [nil]).to_a[1..-1]
  else
    [result]
  end
end
getc() click to toggle source
Calls superclass method
# File lib/ctf/io_with_echo.rb, line 40
def getc
  super.tap {|result| echo_input_print(result || '') }
end
gets() click to toggle source
Calls superclass method
# File lib/ctf/io_with_echo.rb, line 36
def gets
  super.tap {|result| echo_input_print(result || '') }
end
interactive!(input = STDIN, output = STDOUT) click to toggle source
Calls superclass method
# File lib/ctf/io_with_echo.rb, line 31
def interactive!(input = STDIN, output = STDOUT)
  @echo = false
  super input, output
end
read(length = nil, outbuf = nil) click to toggle source
Calls superclass method
# File lib/ctf/io_with_echo.rb, line 27
def read(length = nil, outbuf = nil)
  super(length, outbuf).tap {|result| echo_input_print(result || '') }
end
write(str) click to toggle source
Calls superclass method
# File lib/ctf/io_with_echo.rb, line 22
def write(str)
  echo_output_print str
  super str
end

Private Instance Methods

echo_input_print(str) click to toggle source
# File lib/ctf/io_with_echo.rb, line 81
def echo_input_print(str)
  if echo
    if echo_color
      echo_input.print HighLine.color(str, :green)
    else
      echo_input.print str
    end
    echo_input.flush
  end
end
echo_output_print(str) click to toggle source
# File lib/ctf/io_with_echo.rb, line 70
def echo_output_print(str)
  if echo
    if echo_color
      echo_output.print HighLine.color(str, :yellow)
    else
      echo_output.print str
    end
    echo_output.flush
  end
end