Module: CLI::Mastermind::Interface

Included in:
CLI::Mastermind, Plan
Defined in:
lib/cli/mastermind/interface.rb

Instance Method Summary collapse

Instance Method Details

#ask(question, default: nil) ⇒ Object



38
39
40
# File 'lib/cli/mastermind/interface.rb', line 38

def ask(question, default: nil)
  CLI::UI.ask(question, default: default)
end

#confirm(question) ⇒ Object



42
43
44
# File 'lib/cli/mastermind/interface.rb', line 42

def confirm(question)
  CLI::UI.confirm(question)
end

#enable_uiObject



2
3
4
# File 'lib/cli/mastermind/interface.rb', line 2

def enable_ui
  CLI::UI::StdoutRouter.enable
end

#frame(title) ⇒ Object



33
34
35
36
# File 'lib/cli/mastermind/interface.rb', line 33

def frame(title)
  return yield unless ui_enabled?
  CLI::UI::Frame.open(title) { yield }
end

#select(question, options:, default: options.first, **opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cli/mastermind/interface.rb', line 46

def select(question, options:, default: options.first, **opts)
  default_value = nil
  options = case options
            when Array
              default_value = default

              o = options - [default]
              o.zip(o).to_h
            when Hash
              # Handle the "default" default.  Otherwise, we expect the
              # default to be a key in the options hash
              default = default.first if default.is_a? Array
              default_value = options[default]
              options.dup.tap { |o| o.delete(default) }
            end

  CLI::UI::Prompt.ask(question, **opts) do |handler|
    handler.option(titleize(default.to_s)) { default_value }

    options.each do |(text, value)|
      handler.option(titleize(text.to_s)) { value }
    end
  end
end

#spinner(title) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cli/mastermind/interface.rb', line 10

def spinner(title)
  return yield unless ui_enabled?

  yield_value = nil

  group = CLI::UI::SpinGroup.new
  group.add(title) do |spinner|
    catch(:success) do
      msg = catch(:fail) do
        yield_value = yield spinner
        throw :success
      end

      puts msg
      CLI::UI::Spinner::TASK_FAILED
    end
  end

  group.wait

  yield_value
end

#titleize(string) ⇒ Object



71
72
73
# File 'lib/cli/mastermind/interface.rb', line 71

def titleize(string)
  string.gsub(/[-_-]/, ' ').split(' ').map(&:capitalize).join(' ')
end

#ui_enabled?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/cli/mastermind/interface.rb', line 6

def ui_enabled?
  CLI::UI::StdoutRouter.enabled?
end