|
| 1 | +## Readline |
| 2 | + |
| 3 | +To use this module, do `require('readline')`. Readline allows reading of a |
| 4 | +stream (such as STDIN) on a line-by-line basis. |
| 5 | + |
| 6 | +Note that once you've invoked this module, your node program will not |
| 7 | +terminate until you've closed the interface, and the STDIN stream. Here's how |
| 8 | +to allow your program to gracefully terminate: |
| 9 | + |
| 10 | + var rl = require('readline'); |
| 11 | + |
| 12 | + var i = rl.createInterface(process.sdtin, process.stdout, null); |
| 13 | + i.question("What do you think of node.js?", function(answer) { |
| 14 | + // TODO: Log the answer in a database |
| 15 | + console.log("Thank you for your valuable feedback."); |
| 16 | + |
| 17 | + // These two lines together allow the program to terminate. Without |
| 18 | + // them, it would run forever. |
| 19 | + i.close(); |
| 20 | + process.stdin.destroy(); |
| 21 | + }); |
| 22 | + |
| 23 | +### rl.createInterface(input, output, completer) |
| 24 | + |
| 25 | +Takes two streams and creates a readline interface. The `completer` function |
| 26 | +is used for autocompletion. When given a substring, it returns `[[substr1, |
| 27 | +substr2, ...], originalsubstring]`. |
| 28 | + |
| 29 | +`createInterface` is commonly used with `process.stdin` and |
| 30 | +`process.stdout` in order to accept user input: |
| 31 | + |
| 32 | + var readline = require('readline'), |
| 33 | + rl = readline.createInterface(process.stdin, process.stdout); |
| 34 | + |
| 35 | +### rl.setPrompt(prompt, length) |
| 36 | + |
| 37 | +Sets the prompt, for example when you run `node` on the command line, you see |
| 38 | +`> `, which is node's prompt. |
| 39 | + |
| 40 | +### rl.prompt() |
| 41 | + |
| 42 | +Readies readline for input from the user, putting the current `setPrompt` |
| 43 | +options on a new line, giving the user a new spot to write. |
| 44 | + |
| 45 | +<!-- ### rl.getColumns() Not available? --> |
| 46 | + |
| 47 | +### rl.question(query, callback) |
| 48 | + |
| 49 | +Prepends the prompt with `query` and invokes `callback` with the user's |
| 50 | +response. Displays the query to the user, and then invokes `callback` with the |
| 51 | +user's response after it has been typed. |
| 52 | + |
| 53 | +Example usage: |
| 54 | + |
| 55 | + interface.question('What is your favorite food?', function(answer) { |
| 56 | + console.log('Oh, so your favorite food is ' + answer); |
| 57 | + }); |
| 58 | + |
| 59 | +### rl.close() |
| 60 | + |
| 61 | + Closes tty. |
| 62 | + |
| 63 | +### rl.pause() |
| 64 | + |
| 65 | + Pauses tty. |
| 66 | + |
| 67 | +### rl.resume() |
| 68 | + |
| 69 | + Resumes tty. |
| 70 | + |
| 71 | +### rl.write() |
| 72 | + |
| 73 | + Writes to tty. |
| 74 | + |
| 75 | +### Event: 'line' |
| 76 | + |
| 77 | +`function (line) {}` |
| 78 | + |
| 79 | +Emitted whenever the `in` stream receives a `\n`, usually received when the |
| 80 | +user hits enter, or return. This is a good hook to listen for user input. |
| 81 | + |
| 82 | +Example of listening for `line`: |
| 83 | + |
| 84 | + rl.on('line', function (cmd) { |
| 85 | + console.log('You just typed: '+cmd); |
| 86 | + }); |
| 87 | + |
| 88 | +### Event: 'close' |
| 89 | + |
| 90 | +`function () {}` |
| 91 | + |
| 92 | +Emitted whenever the `in` stream receives a `^C` or `^D`, respectively known |
| 93 | +as `SIGINT` and `EOT`. This is a good way to know the user is finished using |
| 94 | +your program. |
| 95 | + |
| 96 | +Example of listening for `close`, and exiting the program afterward: |
| 97 | + |
| 98 | + rl.on('close', function() { |
| 99 | + console.log('goodbye!'); |
| 100 | + process.exit(0); |
| 101 | + }); |
| 102 | + |
| 103 | +Here's an example of how to use all these together to craft a tiny command |
| 104 | +line interface: |
| 105 | + |
| 106 | + var readline = require('readline'), |
| 107 | + rl = readline.createInterface(process.stdin, process.stdout), |
| 108 | + prefix = 'OHAI> '; |
| 109 | + |
| 110 | + rl.on('line', function(line) { |
| 111 | + switch(line.trim()) { |
| 112 | + case 'hello': |
| 113 | + console.log('world!'); |
| 114 | + break; |
| 115 | + default: |
| 116 | + console.log('Say what? I might have heard `' + line.trim() + '`'); |
| 117 | + break; |
| 118 | + } |
| 119 | + rl.setPrompt(prefix, prefix.length); |
| 120 | + rl.prompt(); |
| 121 | + }).on('close', function() { |
| 122 | + console.log('Have a great day!'); |
| 123 | + process.exit(0); |
| 124 | + }); |
| 125 | + console.log(prefix + 'Good to see you. Try typing stuff.'); |
| 126 | + rl.setPrompt(prefix, prefix.length); |
| 127 | + rl.prompt(); |
| 128 | + |
| 129 | + |
| 130 | +Take a look at this slightly more complicated |
| 131 | +[example](https://gist.github.com/901104), and |
| 132 | +[http-console](http://github.com/cloudhead/http-console) for a real-life use |
| 133 | +case. |
0 commit comments