@@ -426,6 +426,7 @@ def sections
426
426
##
427
427
def add_section ( title )
428
428
@content [ title . cap_first ] = { 'original' => "#{ title } :" , 'items' => [ ] }
429
+ @results . push ( %Q{Added section "#{ title . cap_first } "} )
429
430
end
430
431
431
432
##
@@ -451,9 +452,9 @@ def guess_section(frag,guessed=false)
451
452
if alt
452
453
raise "Did you mean `doing view #{ alt } `?"
453
454
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
457
458
add_section ( frag . cap_first )
458
459
write ( @doing_file )
459
460
return frag . cap_first
@@ -464,6 +465,57 @@ def guess_section(frag,guessed=false)
464
465
section ? section . cap_first : guessed
465
466
end
466
467
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
+
467
519
##
468
520
## @brief Attempt to match a string with an existing view
469
521
##
0 commit comments