Skip to content

Commit

Permalink
Fix password prompts.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jul 10, 2013
1 parent 2c097a4 commit 88b9d8f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/mina/exec_helpers.rb
Expand Up @@ -26,7 +26,7 @@ def pretty_system(code)
pid_in = Sys.stream_stdin! { |chr| i.putc chr }

# __In the foreground,__ stream stdout to the output helper.
Sys.stream_stdout(o) { |str| print_str str }
Sys.stream_stdout(o) { |ch| print_char ch }

Process.waitpid pid_err
Process.kill 'TERM', pid_in
Expand Down Expand Up @@ -93,7 +93,7 @@ def stream_stdin!(&blk)
# output helper.

def stream_stdout(o, &blk)
while str = o.gets
while str = o.getc
yield str
end
end
Expand Down
30 changes: 25 additions & 5 deletions lib/mina/output_helpers.rb
Expand Up @@ -19,18 +19,38 @@ module OutputHelpers
#
# Returns nothing.
#
def print_str(str)
if str =~ /^\-+> (.*?)$/
def print_str(line)
if line =~ /^\-+> (.*?)$/
print_status $1
elsif str =~ /^! (.*?)$/
elsif line =~ /^! (.*?)$/
print_error $1
elsif str =~ /^\$ (.*?)$/
elsif line =~ /^\$ (.*?)$/
print_command $1
else
print_stdout str
print_stdout line
end
end

# ### print_char
# Prints a single character.
def print_char(ch)
$last ||= ''

if ch == "\n"
print_clear
print_str $last
$last = ''
else
print ' ' if $last == ''
print ch
$last += ch
end
end

def print_clear
print "\033[1K\r"
end

# ### print_status
# Prints a status message. (`----->`)
def print_status(msg)
Expand Down
38 changes: 38 additions & 0 deletions spec/helpers/output_helper_spec.rb
@@ -0,0 +1,38 @@
require 'spec_helper'

describe 'Output Helpers' do
before :each do
@out = Object.new
@out.send :extend, Mina::OutputHelpers

allow(@out).to receive(:print_stdout)
allow(@out).to receive(:print_status)
allow(@out).to receive(:print_error)
allow(@out).to receive(:print_command)
allow(@out).to receive(:print_clear)
end

it 'print_str to stdout' do
@out.print_str "Hello there\n"

expect(@out).to have_received(:print_stdout).with("Hello there\n")
end

it 'print_str to status' do
@out.print_str "-----> Getting password"

expect(@out).to have_received(:print_status).with("Getting password")
end

it 'print_str to status (2)' do
@out.print_str "-> Getting password"

expect(@out).to have_received(:print_status).with("Getting password")
end

it 'print_str to error' do
@out.print_str "! Something went wrong"

expect(@out).to have_received(:print_error).with("Something went wrong")
end
end
2 changes: 1 addition & 1 deletion test_env/config/deploy.rb
Expand Up @@ -63,5 +63,5 @@

task :get_password do
set :term_mode, :pretty
queue %[echo -n "Password: "; read x; echo out: $x;]
queue %[echo "-> Getting password"; echo -n "Password: "; read x; echo ""; echo out: $x;]
end

0 comments on commit 88b9d8f

Please sign in to comment.