Module: Postview::CLI::Command

Included in:
CreateCommand, ServerCommand
Defined in:
lib/postview/cli.rb

Overview

:nodoc: all

Instance Method Summary collapse

Instance Method Details

#hidden_prompt(label) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/postview/cli.rb', line 44

def hidden_prompt label
  while true
    system "stty -echo"
    printf "%1$s %2$s: ", " "*2, "#{label}"
    value = $stdin.readline.chomp.strip
    system "stty echo"
    puts
    return value unless value.nil? || value.to_s.empty?
  end
end

#init(process) {|Hash.new| ... } ⇒ Object

Yields:

  • (Hash.new)


66
67
68
69
# File 'lib/postview/cli.rb', line 66

def init process, &block
  printf "%1$s %2$s ...\n", ">"*2, process
  yield Hash.new
end

#prompt(label, default = nil, regexp = nil) ⇒ Object

Prompt for values.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/postview/cli.rb', line 26

def prompt label, default = nil, regexp = nil
  while true
    message = if default
                [ "%1$s %2$s [%3$s]: ", " "*2, label, "#{default}" ]
              else
                [ "%1$s %2$s: ", " "*2, "#{label}" ]
              end
    printf *message
    value = $stdin.readline.chomp.strip
    value = default if value.empty?
    if regexp && !value.match(regexp)
      printf "!! Invalid value, please try again."
      value = nil
    end
    return value unless value.nil? || value.to_s.empty?
  end
end

#start(process, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/postview/cli.rb', line 55

def start process, &block
  printf "%1$s %2$s ", ">"*2, process
  if block_given?
    result = yield
    printf " %s\n", (result ? "done" : "fail")
  end
rescue Exception => error
  printf " error\n"
  abort format("%1$s %2$s \n", ">"*2, error)
end

#step(&block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/postview/cli.rb', line 71

def step &block
  if block_given?
    result = yield
    printf "%s", (result ? "." : "!")
    result
  end
end