Skip to content

Commit 544cddb

Browse files
committed
Improved y/n prompt for TTY
1 parent da6ee7c commit 544cddb

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

bin/doing

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,17 +516,19 @@ command :tag do |c|
516516
count = options[:c].to_i
517517

518518
if count == 0
519+
section_q = section == 'All' ? "" : " in section #{section}"
520+
519521
if options[:a]
520-
print "Are you sure you want to autotag all records? (y/N) "
522+
question = "Are you sure you want to autotag all records#{section_q}"
521523
elsif options[:r]
522-
print "Are you sure you want to remove #{tags.join(" and ")} from all records? (y/N) "
524+
question = "Are you sure you want to remove #{tags.join(" and ")} from all records#{section_q}"
523525
else
524-
print "Are you sure you want to add #{tags.join(" and ")} to all records? (y/N) "
526+
question = "Are you sure you want to add #{tags.join(" and ")} to all records#{section_q}"
525527
end
526528

527-
res = STDIN.gets
529+
res = wwid.yn(question,false)
528530

529-
unless res.strip =~ /^y(es)?$/i
531+
unless res
530532
raise "Cancelled"
531533
end
532534
end

lib/doing/wwid.rb

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def sections
426426
##
427427
def add_section(title)
428428
@content[title.cap_first] = {'original' => "#{title}:", 'items' => []}
429+
@results.push(%Q{Added section "#{title.cap_first}"})
429430
end
430431

431432
##
@@ -451,9 +452,9 @@ def guess_section(frag,guessed=false)
451452
if alt
452453
raise "Did you mean `doing view #{alt}`?"
453454
else
454-
print "Create a new section called #{frag.cap_first} (y/N)?"
455-
input = STDIN.gets
456-
if input =~ /^y/i
455+
res = yn("Section #{frag} not found, create it",false)
456+
457+
if res
457458
add_section(frag.cap_first)
458459
write(@doing_file)
459460
return frag.cap_first
@@ -464,6 +465,57 @@ def guess_section(frag,guessed=false)
464465
section ? section.cap_first : guessed
465466
end
466467

468+
##
469+
## @brief Ask a yes or no question in the terminal
470+
##
471+
## @param question (String) The question to ask
472+
## @param default (Bool) default response if no input
473+
##
474+
## @return (Bool) yes or no
475+
##
476+
def yn(question, default_response=false)
477+
if default_response
478+
default = 'y'
479+
else
480+
default = 'n'
481+
end
482+
# if this isn't an interactive shell, answer default
483+
unless $stdout.isatty
484+
if default.downcase == 'y'
485+
return true
486+
else
487+
return false
488+
end
489+
end
490+
# clear the buffer
491+
if ARGV.length
492+
ARGV.length.times do
493+
ARGV.shift
494+
end
495+
end
496+
system 'stty cbreak'
497+
if default
498+
if default =~ /y/i
499+
options = "#{colors['white']}[#{colors['boldgreen']}Y#{colors['white']}/#{colors['boldwhite']}n#{colors['white']}]#{colors['default']}"
500+
else
501+
options = "#{colors['white']}[#{colors['boldwhite']}y#{colors['white']}/#{colors['boldgreen']}N#{colors['white']}]#{colors['default']}"
502+
end
503+
else
504+
options = "#{colors['white']}[#{colors['boldwhite']}y#{colors['white']}/#{colors['boldwhite']}n#{colors['white']}]#{colors['default']}"
505+
end
506+
$stdout.syswrite "#{colors['boldwhite']}#{question.sub(/\?$/,'')} #{options}#{colors['boldwhite']}?#{colors['default']} "
507+
res = $stdin.sysread 1
508+
puts
509+
system 'stty cooked'
510+
511+
res.chomp!
512+
res.downcase!
513+
514+
res = default.downcase if res == ""
515+
516+
return res =~ /y/i
517+
end
518+
467519
##
468520
## @brief Attempt to match a string with an existing view
469521
##

0 commit comments

Comments
 (0)