Module: CheckboxFilterable
- Extended by:
- CheckboxFilterable
- Included in:
- CheckboxFilterable
- Defined in:
- lib/inquirer/prompts/checkbox_filterable.rb
Instance Method Summary collapse
- #check_no_match ⇒ Object
- #filter_choices ⇒ Object
- #prompt(opts = {}) ⇒ Object
- #render_prompt ⇒ Object
- #render_result ⇒ Object
- #update_cursor_position(cursor_position) ⇒ Object
Instance Method Details
#check_no_match ⇒ Object
262 263 264 265 266 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 262 def check_no_match return if !@choices_filtered.empty? @error_message = 'No matching choice found' end |
#filter_choices ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 248 def filter_choices @position = 0 @choices_filtered = [] @choices.each { |choice| next if !choice[:name].gsub(/\s/, '').downcase.index(@filter.gsub(/\s/, '').downcase) @choices_filtered.push(choice) } check_no_match end |
#prompt(opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 10 def prompt opts = {} # finish if there's nothing to do return opts[:default] if Array(opts[:choices]).empty? @question = opts[:message] @position = 0 @paginator = Paginator.new @choices = [] opts[:choices].each { |choice| if choice[:when].is_a?(Proc) when_parameter = opts.merge( choice: choice, ) ask_choice = choice[:when].call( when_parameter ) next if !ask_choice elsif [true, false].include? choice[:when] next if !choice[:when] end choice[:value] ||= choice[:name] if !choice[:checked] && opts[:default].is_a?(Array) choice[:checked] = opts[:default].include?( choice[:value] ) end @choices.push(choice) } question_backup = @question @question += ' ' @question += Inquirer::Style::CheckboxFilterable.selection_help @filter = '' cursor_position = 0 @choices_filtered = @choices @question = question_backup render_prompt # loop through user input IOHelper.read_char do |char| key = IOChar.char_to_key(char) if @error_message @error_message = nil end case key when 'up' if !@choices_filtered.empty? @position = (@position - 1) % @choices_filtered.length else check_no_match end when 'down' if !@choices_filtered.empty? @position = (@position + 1) % @choices_filtered.length else check_no_match end when 'space' if !@choices_filtered.empty? @choices_filtered[@position][:checked] = !@choices_filtered[@position][:checked] choice = @choices_filtered[@position] @choices.map! {|possible_choice| if choice[:name] == possible_choice[:name] possible_choice = choice end possible_choice } end when 'return' if opts[:validate] and opts[:validate].is_a?(Proc) value = @choices.reject { |choice| !choice[:checked] }.collect { |choice| choice[:value] } validation_result = opts[:validate].call( value ) if !validation_result @error_message = Inquirer::Style::CheckboxFilterable. elsif validation_result && validation_result.is_a?(String) @error_message = validation_result end end if !@error_message check_no_match end when 'backspace' index = @filter.size - cursor_position - 1 if index >= 0 # remove char at current index @filter[index] = '' end filter_choices when 'left' if cursor_position < @filter.length cursor_position += 1 end check_no_match when 'right' if cursor_position > 0 cursor_position -= 1 end check_no_match when 'escape' if @filter == '' @choices.map! { |choice| choice[:checked] = false choice } else @filter = '' end filter_choices else @filter = @filter.insert(@filter.length - cursor_position, char) filter_choices end IOHelper.clear render_prompt update_cursor_position(cursor_position) # we are done if the user hits return @error_message || key != 'return' end render_result @choices.reject { |choice| !choice[:checked] }.collect { |choice| choice[:value] } end |
#render_prompt ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 171 def render_prompt # start with the question prefix prompt = Inquirer::Style.question_prefix # render the question prompt += Inquirer::Style::CheckboxFilterable.question % @question prompt += IOChar.newline # render the list prompt += @choices_filtered.map.with_index(0) do |choice, position| choice_prompt = '' if position == @position choice_prompt += Inquirer::Style::CheckboxFilterable.selector else choice_prompt += ' ' end if choice[:checked] choice_prompt += Inquirer::Style::CheckboxFilterable.checkbox_on choice_prompt += ' ' choice_prompt += Inquirer::Style::CheckboxFilterable.checked_item % choice[:name] else choice_prompt += Inquirer::Style::CheckboxFilterable.checkbox_off choice_prompt += ' ' choice_prompt += Inquirer::Style::CheckboxFilterable.item % choice[:name] end choice_prompt end.join('') paginated_prompt = @paginator.paginate(prompt, @position) # render error message if @error_message paginated_prompt += Inquirer::Style::CheckboxFilterable. % @error_message end paginated_prompt += IOChar.newline paginated_prompt += Inquirer::Style::CheckboxFilterable.filter_prefix paginated_prompt += Inquirer::Style::CheckboxFilterable.filter % @filter IOHelper.render( paginated_prompt ) end |
#render_result ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 220 def render_result # start with the question prefix result = Inquirer::Style.question_prefix # render the question result += Inquirer::Style::CheckboxFilterable.question % @question selected_choices = @choices.reject { |choice| !choice[:checked] }.collect { |choice| choice[:short] || choice[:name] }.join(', ') if !selected_choices.empty? result += Inquirer::Style::CheckboxFilterable.response % selected_choices end result += IOChar.newline IOHelper.clear IOHelper.render( result ) end |
#update_cursor_position(cursor_position) ⇒ Object
244 245 246 |
# File 'lib/inquirer/prompts/checkbox_filterable.rb', line 244 def update_cursor_position(cursor_position) print IOChar.cursor_left * cursor_position end |