Class: Umu::Selector

Inherits:
Object
  • Object
show all
Extended by:
Template
Defined in:
lib/umu/core/selector.rb

Overview

Selector is a module for selecting items.

Constant Summary

Constants included from Color

Color::COLORS

Class Method Summary collapse

Methods included from Template

checker, command, cover, hover, logo, pointer, show_command

Class Method Details

.checkbox(opts = [], content = '') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/umu/core/selector.rb', line 33

def self.checkbox(opts = [], content = '')
  puts content
  options = opts.map(&:to_s)

  options.each_with_index do |x, idx|
    print pointer(idx.zero?)
    puts "( ) #{x}"
  end

  checkbox_interactive_menu(options)
end

.checkbox_interactive_menu(options) ⇒ Object



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
119
# File 'lib/umu/core/selector.rb', line 77

def checkbox_interactive_menu(options)
  return [] if options.empty?

  index = 0
  ary = []
  while (key = $stdin.getch) != "\C-c"
    if key == "\e"
      second_key = $stdin.getch

      if second_key == '['
        key = $stdin.getch
        case key
        when 'A', 'D'
          index -= 1 if index != 0
        when 'B', 'C'
          index += 1 if index < (options.size - 1)
        else
          "esc: [#{key}"
        end
        puts "\e[#{options.size + 1}A"
        options.each_with_index do |x, idx|
          print pointer(idx == index)
          puts checker(ary.include?(idx), x)
        end
      else
        key = "esc: #{second_key}"
      end
    end
    if key == ' '
      ary.include?(index) ? ary.delete(index) : (ary << index)
      puts "\e[#{options.size + 1}A"
      options.each_with_index do |x, idx|
        print pointer(idx == index)
        puts checker(ary.include?(idx), x)
      end
    end
    next unless key == "\r"

    check_items = ary.sort.map { |x| options[x] }
    cover(options.size)
    return check_items
  end
end

.radio(opts = [], content = '') ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/umu/core/selector.rb', line 10

def self.radio(opts = [], content = '')
  puts content
  options = opts.map(&:to_s)

  options.each_with_index do |x, idx|
    print pointer(idx.zero?)
    puts hover(idx.zero?, x)
  end
  radio_interactive_menu(options)
end

.radio_interactive_menu(options) ⇒ 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
70
71
72
73
74
75
# File 'lib/umu/core/selector.rb', line 46

def radio_interactive_menu(options)
  index = 0
  while (key = $stdin.getch) != "\C-c"
    if key == "\e"
      second_key = $stdin.getch
      if second_key == '['
        key = $stdin.getch
        case key
        when 'A', 'D'
          index -= 1 if index != 0
        when 'B', 'C'
          index += 1 if index < (options.size - 1)
        else
          "esc: [#{key}"
        end
        puts "\e[#{options.size + 1}A"
        options.each_with_index do |x, idx|
          print pointer(idx == index)
          puts hover(idx == index, x)
        end
      else
        key = "esc: #{second_key}"
      end
    end
    next unless key == "\r"

    cover(options.size)
    return options[index]
  end
end

.single_choice(content = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/umu/core/selector.rb', line 21

def self.single_choice(content = '')
  choice = radio(%w[Yes No], content)
  case choice
  when 'Yes'
    true
  when 'No'
    false
  else
    false
  end
end