Class: Capistrano::Harrow::UI::TTY

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/harrow/ui/tty.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {input: $stdin, output: $stdout, timeout: 30}) ⇒ TTY

Returns a new instance of TTY.



5
6
7
8
9
# File 'lib/capistrano/harrow/ui/tty.rb', line 5

def initialize(params={input: $stdin, output: $stdout, timeout: 30})
  @in = params.fetch(:input, $stdin)
  @out = params.fetch(:output, $stdout)
  @timeout = params.fetch(:timeout, 60)
end

Instance Method Details

#prompt(prompt_str, answers = ['yes', 'no']) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/capistrano/harrow/ui/tty.rb', line 24

def prompt(prompt_str, answers=['yes', 'no'])
  answers = Array(answers)

  @out.write prompt_str

  default_answer = answers.first
  hints = answers_hint(answers)

  unless hints.empty?
    @out.write " "
    @out.write hints
  end

  @out.write ": "

  unless IO.select([@in], [], [], @timeout)
    raise TimeoutError.new
  else
    answer = @in.gets.chop
  end

  return default_answer if answer.empty?

  answer
end

#prompt_password(prompt_str) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/capistrano/harrow/ui/tty.rb', line 15

def prompt_password(prompt_str)
  `stty -echo 2>/dev/null`
  password =  prompt(prompt_str, [])
  show "\n"
  password
ensure
  `stty echo  2>/dev/null`
end

#show(text) ⇒ Object



11
12
13
# File 'lib/capistrano/harrow/ui/tty.rb', line 11

def show(text)
  @out.puts text
end