Class: Umu::Selector

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

Constant Summary

Constants included from Color

Color::COLORS

Class Method Summary collapse

Methods included from Template

checker, cover, hover, pointer

Class Method Details

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



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

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



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

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



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

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



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

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