File tree Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Expand file tree Collapse file tree 5 files changed +146
-0
lines changed Original file line number Diff line number Diff line change 1+ /***
2+ * The goal of this file is to know about the readline functions;
3+ *
4+ *
5+ * How to run this example:
6+ * 1. node readline-1.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var readline = require ( 'readline' ) ;
11+
12+ var rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout
15+ } ) ;
16+
17+ rl . question ( "What do you think of Node.js ? " , function ( answer ) {
18+ console . log ( "Thank you for your valuable feedback." , answer ) ;
19+
20+ rl . close ( ) ;
21+ } ) ;
Original file line number Diff line number Diff line change 1+ /***
2+ * The goal of this file is to know about the readline functions;
3+ *
4+ *
5+ * How to run this example:
6+ * 1. node readline-2.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var readline = require ( 'readline' ) ;
11+
12+ var rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout ,
15+ completer : completer
16+ } ) ;
17+
18+ rl . question ( "What do you think of Node.js ? " , function ( answer ) {
19+ console . log ( "Thank you for your valuable feedback." , answer ) ;
20+
21+ rl . close ( ) ;
22+ } ) ;
23+
24+ function completer ( line ) {
25+ var completions = 'excellent good bad' . split ( ' ' ) ;
26+ var hits = completions . filter ( function ( c ) {
27+ return c . indexOf ( line ) == 0 ;
28+ } ) ;
29+
30+ return [ hits . length ? hits : completions , line ] ;
31+ }
Original file line number Diff line number Diff line change 1+ /***
2+ * The goal of this file is to know about the readline functions;
3+ *
4+ *
5+ * How to run this example:
6+ * 1. node readline-3.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var readline = require ( 'readline' ) ;
11+
12+ var rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout
15+ } ) ;
16+
17+ rl . on ( 'line' , function ( cmd ) {
18+ console . log ( 'You just typed: ' + cmd ) ;
19+ } ) ;
20+
21+ rl . on ( 'pause' , function ( ) {
22+ console . log ( 'Readline paused' ) ;
23+ } ) ;
24+
25+ rl . on ( 'resume' , function ( ) {
26+ console . log ( 'Readline resumed' ) ;
27+ } ) ;
28+
29+ rl . on ( 'close' , function ( ) {
30+ console . log ( 'Readline closed' ) ;
31+ } ) ;
32+
33+ rl . on ( 'SIGINT' , function ( ) {
34+ rl . question ( 'Are you sure you want to exit? ' , function ( answer ) {
35+ if ( answer . match ( / ^ y ( e s ) ? $ / i) ) {
36+ rl . pause ( ) ;
37+ }
38+ } ) ;
39+ } ) ;
Original file line number Diff line number Diff line change 1+ /***
2+ * The goal of this file is to know about the readline functions;
3+ *
4+ *
5+ * How to run this example:
6+ * 1. node readline-4.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var readline = require ( 'readline' ) ;
11+
12+ var rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout
15+ } ) ;
16+
17+ rl . setPrompt ( '>> ' ) ;
18+ rl . prompt ( ) ;
19+
20+ rl . on ( 'line' , function ( line ) {
21+
22+ switch ( line . trim ( ) ) {
23+ case 'hi' :
24+ console . log ( 'world!' ) ;
25+ break ;
26+
27+ default :
28+ console . log ( 'Say what? I might have heard `' + line . trim ( ) + '`' ) ;
29+ break ;
30+ }
31+ rl . prompt ( ) ;
32+ } ) . on ( 'close' , function ( ) {
33+ console . log ( 'Have a great day!' ) ;
34+ process . exit ( 0 ) ;
35+ } ) ;
Original file line number Diff line number Diff line change 1+ /***
2+ * The goal of this file is to know about the readline functions;
3+ *
4+ *
5+ * How to run this example:
6+ * 1. node readline-5.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var readline = require ( 'readline' ) ;
11+
12+ var rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout
15+ } ) ;
16+
17+ readline . cursorTo ( process . stdin , 20 , 20 ) ;
18+ readline . clearLine ( process . stdin , 0 ) ;
19+
20+ rl . close ( ) ;
You can’t perform that action at this time.
0 commit comments