lib/tty/prompt/reader/mode.rb in tty-prompt-0.6.0 vs lib/tty/prompt/reader/mode.rb in tty-prompt-0.7.0
- old
+ new
@@ -1,65 +1,41 @@
# encoding: utf-8
-require 'tty/prompt/reader/mode/echo'
-require 'tty/prompt/reader/mode/raw'
-
module TTY
class Prompt
class Reader
class Mode
# Initialize a Terminal
#
# @api public
- def initialize(options = {})
- @echo = Echo.new
- @raw = Raw.new
+ def initialize(input = $stdin)
+ @input = input
end
- # Switch echo on
- #
- # @api public
- def echo_on
- @echo.on
- end
-
- # Switch echo off
- #
- # @api public
- def echo_off
- @echo.off
- end
-
# Echo given block
#
# @param [Boolean] is_on
#
# @api public
def echo(is_on = true, &block)
- @echo.echo(is_on, &block)
+ previous = @input.echo?
+ @input.echo = is_on
+ yield
+ ensure
+ @input.echo = previous
end
- # Switch raw mode on
- #
- # @api public
- def raw_on
- @raw.on
- end
-
- # Switch raw mode off
- #
- # @api public
- def raw_off
- @raw.off
- end
-
# Use raw mode in the given block
#
# @param [Boolean] is_on
#
# @api public
def raw(is_on = true, &block)
- @raw.raw(is_on, &block)
+ if is_on
+ @input.raw(&block)
+ else
+ yield
+ end
end
end # Mode
end # Reader
end # Prompt
end # TTY