Module: Spider::ConsoleWizard

Defined in:
lib/spiderfw/setup/spider_setup_wizard.rb

Instance Method Summary collapse

Instance Method Details

#ask_errorObject



18
19
20
21
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 18

def ask_error
    res = (ask? _("Do you want to change your settings?"), :default => true)
    !res
end

#do_ask(msg, var_name, options) ⇒ Object



23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 23

def do_ask(msg, var_name, options)
    trap('TERM', lambda{ puts _(" Exiting."); exit })
    trap('INT', lambda{ puts _(" Exiting."); exit })
    str = msg
    options ||= {}
    if options[:choices]
        str += "\n"
        if options[:allow_cancel]
            cancel_str = options[:allow_cancel] == true ? _("Cancel") : options[:allow_cancel]
            str += "0) #{cancel_str} "
        end
        options[:choices].each_with_index do |c, i|
            str += "#{i+1}) #{c} "
        end
    end
    prompt = ""
    yes = no = y = n = nil
    curr = nil
    if var_name
        add_key(var_name)
        curr = get_value(var_name)
    end
    if options[:type] == Spider::Bool
        yes = _("yes")
        no = _("no")
        y = yes[0].chr
        n = no[0].chr
        if options[:default] == true
            y = y.upcase
        elsif options[:default] == false
            n = n.upcase
        end
        prompt = "[#{y}/#{n}]: "
    elsif curr
        prompt = "[#{curr}]: "
    elsif options[:default]
        prompt = "[#{options[:default]}]: "
    else
        prompt = ">> "
    end
    
    
    good = false
    puts str
    
    while !good

        print prompt
    
        res = $stdin.gets.strip
        
        good = true
        if options[:type] == Spider::Bool
            if res == yes || res == y
                res = true
            elsif res == no || res == n
                res = false
            elsif res.blank? && options[:default]
                res = options[:default]
            else
                good = false
            end
        elsif res.to_s.empty?
            if curr
                res = curr
            elsif options[:default]
                res = options[:default]
            end
        end
        if options[:choices]
            if options[:allow_cancel] && res == "0"
                res = nil
            else
                if res =~ /\d+/
                    res = res.to_i - 1
                    unless options[:return_index]
                        res = options[:choices][res]
                        good = false unless res
                    end
                else
                    good = false unless options[:choices].include?(res)
                    if options[:return_index]
                        res = options[:choices].index(res)
                    end
                end
            end
            
        end

        if res.to_s.empty? && options[:required]
            good = false
        end
    end
    set_value(var_name, res) if var_name
    res
end

#error(msg) ⇒ Object



14
15
16
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 14

def error(msg)
    puts msg
end

#notify(msg) ⇒ Object



5
6
7
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 5

def notify(msg)
    puts msg
end

#notify_partial(msg, finish = false) ⇒ Object



9
10
11
12
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 9

def notify_partial(msg, finish=false)
    print msg
    puts if finish
end