1 |
|
2 | /*
|
3 | Terminal Kit
|
4 |
|
5 | Copyright (c) 2009 - 2020 Cédric Ronvel
|
6 |
|
7 | The MIT License (MIT)
|
8 |
|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
10 | of this software and associated documentation files (the "Software"), to deal
|
11 | in the Software without restriction, including without limitation the rights
|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13 | copies of the Software, and to permit persons to whom the Software is
|
14 | furnished to do so, subject to the following conditions:
|
15 |
|
16 | The above copyright notice and this permission notice shall be included in all
|
17 | copies or substantial portions of the Software.
|
18 |
|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
25 | SOFTWARE.
|
26 | */
|
27 |
|
28 | ;
|
29 |
|
30 |
|
31 |
|
32 | /* jshint unused:false */
|
33 |
|
34 |
|
35 |
|
36 | require( '../lib/termkit.js' ).getDetectedTerminal( function( error , term ) {
|
37 |
|
38 | term.grabInput() ;
|
39 |
|
40 | function question()
|
41 | {
|
42 | var controler ;
|
43 |
|
44 | term( 'Do you like javascript? [y|n] ' ) ;
|
45 |
|
46 | //term.yesOrNo( { yes: [ 'y' , 'o' , 'ENTER' ] , no: [ 'n' ] , echoYes: 'yes' , echoNo: 'no' } , function( error , result ) {
|
47 | controler = term.yesOrNo( function( error , result ) {
|
48 |
|
49 | if ( error )
|
50 | {
|
51 | term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
|
52 | question() ;
|
53 | }
|
54 | else if ( result )
|
55 | {
|
56 | term.green( "\n'Yes' detected! Good bye!\n" ) ;
|
57 | terminate() ;
|
58 | }
|
59 | else
|
60 | {
|
61 | term.red( "\n'No' detected, are you sure?\n" ) ;
|
62 | question() ;
|
63 | }
|
64 | } ) ;
|
65 |
|
66 | term.on( 'key' , function( key ) {
|
67 | if ( key === 'ESCAPE' )
|
68 | {
|
69 | term( 'Escape.\n' ) ;
|
70 | controler.abort() ;
|
71 | setTimeout( terminate , 2000 ) ;
|
72 | }
|
73 | } ) ;
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | async function asyncQuestion()
|
79 | {
|
80 | var response ;
|
81 |
|
82 | term( 'Do you like javascript? [y|n] ' ) ;
|
83 |
|
84 | while ( ! await term.yesOrNo().promise ) {
|
85 | term.red( "\n'No' detected, are you sure? [y|n] " ) ;
|
86 | }
|
87 |
|
88 | term.green( "\n'Yes' detected! Good bye!\n" ) ;
|
89 | terminate() ;
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 | function terminate()
|
95 | {
|
96 | term.grabInput( false ) ;
|
97 | // Add a 100ms delay, so the terminal will be ready when the process effectively exit, preventing bad escape sequences drop
|
98 | setTimeout( function() { process.exit() ; } , 100 ) ;
|
99 | }
|
100 |
|
101 |
|
102 | term.bold.cyan( 'Yes or no test, hit anything on the keyboard to see how it is detected...\n' ) ;
|
103 |
|
104 | //question() ;
|
105 | asyncQuestion() ;
|
106 | } ) ;
|
107 |
|
108 |
|
109 |
|
110 |
|