1 | /*
|
2 | Terminal Kit
|
3 |
|
4 | Copyright (c) 2009 - 2020 Cédric Ronvel
|
5 |
|
6 | The MIT License (MIT)
|
7 |
|
8 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | of this software and associated documentation files (the "Software"), to deal
|
10 | in the Software without restriction, including without limitation the rights
|
11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 | copies of the Software, and to permit persons to whom the Software is
|
13 | furnished to do so, subject to the following conditions:
|
14 |
|
15 | The above copyright notice and this permission notice shall be included in all
|
16 | copies or substantial portions of the Software.
|
17 |
|
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24 | SOFTWARE.
|
25 | */
|
26 |
|
27 | ;
|
28 |
|
29 |
|
30 |
|
31 | var Promise = require( 'seventh' ) ;
|
32 |
|
33 |
|
34 |
|
35 | /*
|
36 | yesOrNo( [yes] , [no] , callback )
|
37 | * options `Object`
|
38 | * yes `string` or `Array` contains a key code or an array of key code that will trigger the yes
|
39 | * no `string` or `Array` contains a key code or an array of key code that will trigger the no
|
40 | * echoYes `string` if defined this will be what will be outputed in case of yes
|
41 | * echoNo `string` if defined this will be what will be outputed in case of no
|
42 | * callback( error , result )
|
43 | * result: true for 'yes' or false for 'no'
|
44 | */
|
45 | module.exports = function yesOrNo( options , callback ) {
|
46 | if ( typeof options === 'function' ) { callback = options ; options = undefined ; }
|
47 |
|
48 | if ( ! options || typeof options !== 'object' ) {
|
49 | options = {
|
50 | yes: [ 'y' , 'Y' ] ,
|
51 | no: [ 'n' , 'N' ] ,
|
52 | echoYes: 'yes' ,
|
53 | echoNo: 'no'
|
54 | } ;
|
55 | }
|
56 |
|
57 | if ( typeof options.yes === 'string' ) { options.yes = [ options.yes ] ; }
|
58 | if ( ! Array.isArray( options.yes ) ) { options.yes = [ 'y' , 'Y' ] ; }
|
59 |
|
60 | if ( typeof options.no === 'string' ) { options.no = [ options.no ] ; }
|
61 | if ( ! Array.isArray( options.no ) ) { options.no = [ 'n' , 'N' ] ; }
|
62 |
|
63 | if ( ! this.grabbing ) { this.grabInput() ; }
|
64 |
|
65 | var onKey = key => {
|
66 | if ( options.yes.indexOf( key ) !== -1 ) {
|
67 | if ( options.echoYes ) { this( options.echoYes ) ; }
|
68 | this.removeListener( 'key' , onKey ) ;
|
69 |
|
70 | if ( callback ) { callback( undefined , true ) ; }
|
71 | else { controller.promise.resolve( true ) ; }
|
72 | }
|
73 | else if ( options.no.indexOf( key ) !== -1 ) {
|
74 | if ( options.echoNo ) { this( options.echoNo ) ; }
|
75 | this.removeListener( 'key' , onKey ) ;
|
76 |
|
77 | if ( callback ) { callback( undefined , false ) ; }
|
78 | else { controller.promise.resolve( false ) ; }
|
79 | }
|
80 | } ;
|
81 |
|
82 | this.on( 'key' , onKey ) ;
|
83 |
|
84 | var controller = {} ; //Object.create( NextGenEvents.prototype ) ;
|
85 |
|
86 | // Stop everything and do not even call the callback
|
87 | controller.abort = () => {
|
88 | this.removeListener( 'key' , onKey ) ;
|
89 | } ;
|
90 |
|
91 | controller.promise = new Promise() ;
|
92 |
|
93 | return controller ;
|
94 | } ;
|
95 |
|