Module: Mina::OutputHelpers

Defined in:
lib/mina/output_helpers.rb

Instance Method Summary collapse

Instance Method Details

#color(str, c) ⇒ Object

### color Colorizes a string. Returns the string ‘str` with the color `c`.



87
88
89
# File 'lib/mina/output_helpers.rb', line 87

def color(str, c)
  ENV['NO_COLOR'] ? str : "\033[#{c}m#{str}\033[0m"
end

### print_char Prints a single character.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mina/output_helpers.rb', line 36

def print_char(ch)
  $last ||= ''

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


50
51
52
# File 'lib/mina/output_helpers.rb', line 50

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

### print_command Prints a command.



74
75
76
# File 'lib/mina/output_helpers.rb', line 74

def print_command(msg)
  puts "       #{color("$", 32)} #{color(msg, 32)}"
end

### print_error Prints an error message (header).



62
63
64
# File 'lib/mina/output_helpers.rb', line 62

def print_error(msg)
  puts " #{color("!", 33)}     #{color(msg, 31)}"
end

### print_status Prints a status message. (‘—–>`)



56
57
58
# File 'lib/mina/output_helpers.rb', line 56

def print_status(msg)
  puts color(("-----> " + msg), 32)
end

### print_stderr Prints an error message (body), or prints stderr output.



68
69
70
# File 'lib/mina/output_helpers.rb', line 68

def print_stderr(msg)
  puts "       #{color(msg, 31)}"
end

### print_stdout Prints a normal message.



80
81
82
# File 'lib/mina/output_helpers.rb', line 80

def print_stdout(msg)
  puts "       #{msg}"
end

### print_str Prints a string by delegating it to the proper output helper.

It takes an input with text and prints them nicely. The text block can have statuses (prefixed with ‘—–> `), errors (prefixed with `! `), commands (prefixed with `$ `) or anything else. Depending on the type of the message, they will be delegated to the proper print_* helper.

-----> Unlocking
$ unlock foo
Unlocked.
! ERROR: Failed

Returns nothing.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mina/output_helpers.rb', line 22

def print_str(line)
  if line =~ /^\-+> (.*?)$/
    print_status $1
  elsif line =~ /^! (.*?)$/
    print_error $1
  elsif line =~ /^\$ (.*?)$/
    print_command $1
  else
    print_stdout line
  end
end

#puts(msg) ⇒ Object



91
92
93
# File 'lib/mina/output_helpers.rb', line 91

def puts(msg)
  %[echo "#{msg}"]
end