Fix #38, change the placement of colon in default query

A colon in `query` doesn't mean a starting point of user input now.
It is common style that makes a colon mean a separator.
This commit is contained in:
anseki 2016-06-13 19:41:45 +09:00
parent c719316327
commit e39454cd2b
3 changed files with 84 additions and 83 deletions

View file

@ -14,20 +14,20 @@ readlineSync tries to let your script have a conversation with the user via a co
var readlineSync = require('readline-sync');
// Wait for user's response.
var userName = readlineSync.question('May I have your name? :');
var userName = readlineSync.question('May I have your name? ');
console.log('Hi ' + userName + '!');
// Handle the secret text (e.g. password).
var favFood = readlineSync.question('What is your favorite food? :', {
var favFood = readlineSync.question('What is your favorite food? ', {
hideEchoBack: true // The typed text on screen is hidden by `*` (default).
});
console.log('Oh, ' + userName + ' loves ' + favFood + '!');
```
```console
May I have your name? :CookieMonster
May I have your name? CookieMonster
Hi CookieMonster!
What is your favorite food? :****
What is your favorite food? ****
Oh, CookieMonster loves tofu!
```
@ -176,7 +176,7 @@ It can include the [placeholders](#placeholders).
For example:
```js
program = readlineSync.question('Which program starts do you want? :', {
program = readlineSync.question('Which program starts do you want? ', {
defaultInput: 'firefox'
});
```
@ -236,13 +236,13 @@ For example:
```js
readlineSync.setDefaultOptions({limit: ['green', 'yellow', 'red']});
a1 = readlineSync.question('Which color of signal? :'); // Input is limited to 3 things.
a2 = readlineSync.question('Which color of signal? :'); // It's limited yet.
a3 = readlineSync.question('What is your favorite color? :', {limit: null}); // It's unlimited temporarily.
a4 = readlineSync.question('Which color of signal? :'); // It's limited again.
a1 = readlineSync.question('Which color of signal? '); // Input is limited to 3 things.
a2 = readlineSync.question('Which color of signal? '); // It's limited yet.
a3 = readlineSync.question('What is your favorite color? ', {limit: null}); // It's unlimited temporarily.
a4 = readlineSync.question('Which color of signal? '); // It's limited again.
readlineSync.setDefaultOptions({limit: ['beef', 'chicken']});
a5 = readlineSync.question('Beef or Chicken? :'); // Input is limited to new 2 things.
a6 = readlineSync.question('And you? :'); // It's limited to 2 things yet.
a5 = readlineSync.question('Beef or Chicken? '); // Input is limited to new 2 things.
a6 = readlineSync.question('And you? '); // It's limited to 2 things yet.
```
The Object as `options` can have following properties.
@ -380,11 +380,11 @@ The [placeholders](#placeholders) like `'$<a-e>'` are replaced to an Array that
For example:
```js
direction = readlineSync.keyIn('Left or Right? :', {limit: 'lr'}); // 'l' or 'r'
direction = readlineSync.keyIn('Left or Right? ', {limit: 'lr'}); // 'l' or 'r'
```
```js
dice = readlineSync.keyIn('Roll the dice, What will the result be? :',
dice = readlineSync.keyIn('Roll the dice, What will the result be? ',
{limit: '$<1-6>'}); // range of '1' to '6'
```
@ -417,7 +417,7 @@ If the user input empty text (i.e. pressed the Enter key only), return this.
For example:
```js
lang = readlineSync.question('Which language? :', {defaultInput: 'javascript'});
lang = readlineSync.question('Which language? ', {defaultInput: 'javascript'});
```
### <a name="basic_options-truevalue_falsevalue"></a>`trueValue`, `falseValue`
@ -437,7 +437,7 @@ One of above or an Array that includes multiple things (or Array includes Array)
For example:
```js
answer = readlineSync.question('How do you like it? :', {
answer = readlineSync.question('How do you like it? ', {
trueValue: ['yes', 'yeah', 'yep'],
falseValue: ['no', 'nah', 'nope']
});
@ -527,8 +527,8 @@ readlineSync.setDefaultOptions({
print: function(display, encoding)
{ process.stdout.write(display, encoding); }
});
var name = readlineSync.question('May I have your name? :');
var loc = readlineSync.question('Hi ' + name + '! Where do you live? :');
var name = readlineSync.question('May I have your name? ');
var loc = readlineSync.question('Hi ' + name + '! Where do you live? ');
```
* Let somebody hear our conversation in real time.
@ -548,15 +548,15 @@ node conv.js >/tmp/fifo
```
```console
May I have your name? :Oz
Hi Oz! Where do you live? :Emerald City
May I have your name? Oz
Hi Oz! Where do you live? Emerald City
```
And then, another terminal shows this synchronously:
```console
May I have your name? :Oz
Hi Oz! Where do you live? :Emerald City
May I have your name? Oz
Hi Oz! Where do you live? Emerald City
```
### <a name="basic_options-history"></a>`history`
@ -1248,7 +1248,7 @@ This method works like the `window.confirm` method of web browsers. A return val
* other: `''`
The `query` is handled the same as that of the [`question`](#basic_methods-question) method.
The default value of `query` is `'Are you sure? :'`.
The default value of `query` is `'Are you sure? '`.
The keys other than `Y` and `N` are also accepted (If you want to know a user's wish explicitly, use [`keyInYNStrict`](#utility_methods-keyinynstrict) method). Therefore, if you let the user make an important decision (e.g. files are removed), check whether the return value is not *falsy*. That is, a default is "No".
@ -1317,7 +1317,7 @@ This method works like the `window.confirm` method of web browsers. A return val
* `N`: `false`
The `query` is handled the same as that of the [`question`](#basic_methods-question) method.
The default value of `query` is `'Are you sure? :'`.
The default value of `query` is `'Are you sure? '`.
A key other than `Y` and `N` is not accepted. That is, a return value has no default. Therefore, the user has to tell an own wish explicitly. If you want to know a user's wish easily, use [`keyInYN`](#utility_methods-keyinyn) method.
@ -1783,7 +1783,7 @@ TTY interfaces are different by the platforms. If the platform doesn't support t
```js
try {
answer = readlineSync.question('What is your favorite food? :');
answer = readlineSync.question('What is your favorite food? ');
} catch (e) {
console.error(e);
process.exit(1);

View file

@ -395,6 +395,7 @@ function _readlineSync(options) {
}
// other ctrl-chars
// eslint-disable-next-line no-control-regex
if (chunk) { chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ''); }
if (chunk && limit) { chunk = chunk.replace(limit, ''); }
@ -444,7 +445,7 @@ function flattenArray(array, validator) {
}
function escapePattern(pattern) {
return pattern.replace(/[\x00-\x7f]/g,
return pattern.replace(/[\x00-\x7f]/g, // eslint-disable-line no-control-regex
function(s) { return '\\x' + ('00' + s.charCodeAt().toString(16)).substr(-2); });
}
@ -1191,7 +1192,7 @@ exports.promptSimShell = function(options) {
function _keyInYN(query, options, limit) {
var res;
if (query == null) { query = 'Are you sure? :'; } // eslint-disable-line eqeqeq
if (query == null) { query = 'Are you sure? '; } // eslint-disable-line eqeqeq
if ((!options || options.guide !== false) && (query += '')) {
query = query.replace(/\s*:?\s*$/, '') + ' [y/n]: ';
}

View file

@ -1,6 +1,6 @@
{
"name": "readline-sync",
"version": "1.4.3",
"version": "1.4.4",
"title": "readlineSync",
"description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).",
"keywords": [