Module: Planter::Prompt
- Included in:
- Planter
- Defined in:
- lib/planter/prompt.rb
Overview
Individual question
Defined Under Namespace
Classes: Question
Class Method Summary collapse
-
.choice(choices, prompt = 'Make a selection', default_response: nil) ⇒ String
Choose from an array of multiple choices.
- .file_what?(entry) ⇒ Boolean
-
.yn(question, default_response: false) ⇒ Boolean
Ask a yes or no question in the terminal.
Class Method Details
.choice(choices, prompt = 'Make a selection', default_response: nil) ⇒ String
Choose from an array of multiple choices. Letter surrounded in parenthesis becomes character for response. Only one letter should be specified and must be unique.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/planter/prompt.rb', line 243 def self.choice(choices, prompt = 'Make a selection', default_response: nil) $stdin.reopen('/dev/tty') default = default_response.is_a?(String) ? default_response.downcase : nil # if this isn't an interactive shell, answer default return default unless $stdout.isatty # If --defaults is set, return default return default if Planter.accept_defaults || ENV['PLANTER_DEBUG'] # clear the buffer if ARGV&.length ARGV.length.times do ARGV.shift end end system 'stty cbreak' vertical = choices.join(' ').length + 4 > TTY::Screen.cols desc = choices.map { |c| c.highlight_character(default: default) } abbr = choices.abbr_choices(default: default) = if vertical "{x}#{desc.join("\n")}\n{by}#{prompt}{x} #{abbr}{bw}? " else "{by}#{prompt}{bw}?\n#{desc.join(', ')}{x} #{abbr}:{x} " end $stdout.syswrite .x res = $stdin.sysread 1 puts system 'stty cooked' res.chomp! res.downcase! res.empty? ? default : res end |
.file_what?(entry) ⇒ Boolean
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/planter/prompt.rb', line 283 def self.file_what?(entry) = %w[(o)vewrite (m)erge] << '(c)opy' unless File.exist?(entry.target) << '(i)gnore' opt = Prompt.choice(, "What do you want to do with #{File.basename(entry.target)}", default_response: 'i') case opt when /^m/ :merge when /^o/ :overwrite when /^c/ :copy else :ignore end end |
.yn(question, default_response: false) ⇒ Boolean
Ask a yes or no question in the terminal
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/planter/prompt.rb', line 310 def self.yn(question, default_response: false) $stdin.reopen('/dev/tty') default = if default_response.is_a?(String) default_response =~ /y/i ? true : false else default_response end # if PLANTER_DEBUG is set, answer default return true if ENV['PLANTER_DEBUG'] # if this isn't an interactive shell, answer default return default unless $stdout.isatty # If --defaults is set, return default return default if Planter.accept_defaults # clear the buffer if ARGV&.length ARGV.length.times do ARGV.shift end end system 'stty cbreak' = if default.nil? '{w}[{bw}y{w}/{bw}n{w}]' else "{w}[#{default ? '{bg}Y{w}/{bw}n' : '{bw}y{w}/{bg}N'}{w}]" end $stdout.syswrite "{bw}#{question.sub(/\?$/, '')} #{}{bw}? {x}".x res = $stdin.sysread 1 puts system 'stty cooked' res.chomp! res.downcase! return default if res.empty? res =~ /y/i ? true : false end |