setPrompt() and setEncoding() return current value.

This commit is contained in:
anseki 2014-07-12 08:56:21 +09:00
parent cabbc1ffea
commit 8d9aca03f8
3 changed files with 15 additions and 5 deletions

View file

@ -22,10 +22,16 @@ npm install readline-sync
### setPrompt ### setPrompt
```js ```js
readlineSync.setPrompt(prompt) currentValue = readlineSync.setPrompt([prompt])
``` ```
Sets the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt. Sets the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt.
This is converted to string (i.e. `toString` method is called) before it is displayed every time.
For example: like bash `[foo-directory]# `
```js
readlineSync.setPrompt({toString: function() {return '[' + require('path').basename(process.cwd()) + ']# '; }})
```
### prompt ### prompt
@ -39,16 +45,17 @@ If `{noEchoBack: true}` is specified to `options`, echo back is avoided. It is u
### question ### question
```js ```js
line = readlineSync.question(query[, options]) line = readlineSync.question([query[, options]])
``` ```
Displays the `query` to the user, and then returns the user's response after it has been typed. Displays the `query` to the user, and then returns the user's response after it has been typed.
`query` is converted to string (i.e. `toString` method is called) before it is displayed every time.
If `{noEchoBack: true}` is specified to `options`, echo back is avoided. It is used to hide the password which is typed by user on screen. *See [Note](#note) for security.* If `{noEchoBack: true}` is specified to `options`, echo back is avoided. It is used to hide the password which is typed by user on screen. *See [Note](#note) for security.*
### setEncoding ### setEncoding
```js ```js
readlineSync.setEncoding(encoding) currentValue = readlineSync.setEncoding([encoding])
``` ```
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'. Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
@ -100,6 +107,7 @@ try {
+ If `options.noEchoBack` is used, the text that input by user is saved to temporary file (e.g. `/tmp/readline-sync.stdout`). This file is removed immediately after reading is done, but you have to be careful about it because this text is *plain*. Removing the file might fail, or the file might be peeped before it is removed. + If `options.noEchoBack` is used, the text that input by user is saved to temporary file (e.g. `/tmp/readline-sync.stdout`). This file is removed immediately after reading is done, but you have to be careful about it because this text is *plain*. Removing the file might fail, or the file might be peeped before it is removed.
## Release History ## Release History
* 2014-07-12 v0.4.2 `setPrompt()` and `setEncoding()` return current value.
* 2014-07-12 v0.4.1 `setPrompt()` and `question()` accept the value which is not string too (e.g. number, Date, Object, etc.). * 2014-07-12 v0.4.1 `setPrompt()` and `question()` accept the value which is not string too (e.g. number, Date, Object, etc.).
* 2014-07-12 v0.4.0 Add `options.noEchoBack`. * 2014-07-12 v0.4.0 Add `options.noEchoBack`.
* 2014-07-12 v0.3.0 Add `setPrint()`. * 2014-07-12 v0.3.0 Add `setPrint()`.

View file

@ -139,12 +139,14 @@ exports.setPrompt = function(newPrompt) {
/* jshint eqnull:true */ /* jshint eqnull:true */
promptText = newPrompt != null ? newPrompt : ''; promptText = newPrompt != null ? newPrompt : '';
/* jshint eqnull:false */ /* jshint eqnull:false */
return promptText;
}; };
exports.setEncoding = function(newEncoding) { exports.setEncoding = function(newEncoding) {
if (typeof newEncoding === 'string') { if (typeof newEncoding === 'string') {
encoding = newEncoding; encoding = newEncoding;
} }
return encoding;
}; };
exports.prompt = function(options) { exports.prompt = function(options) {

View file

@ -1,7 +1,7 @@
{ {
"name": "readline-sync", "name": "readline-sync",
"description": "Synchronous Readline", "description": "Synchronous Readline",
"version": "0.4.1", "version": "0.4.2",
"homepage": "https://github.com/anseki/readline-sync", "homepage": "https://github.com/anseki/readline-sync",
"author": { "author": {
"name": "anseki" "name": "anseki"