Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Change: Accept value as options.cancel. Add placeholder parameters …
Browse files Browse the repository at this point in the history
…`itemsCount`, `firstItem` and `lastItem`.
  • Loading branch information
anseki committed Jan 27, 2016
1 parent 0e700b2 commit ed02067
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 anseki
Copyright (c) 2016 anseki

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1445,10 +1445,68 @@ If `true` is specified, a string like `'[1...5]'` as guide for the user is added

##### <a name="utility_methods-keyinselect-options-cancel"></a>`cancel`

*Type:* boolean
*Default:* `true`
*Type:* boolean, string or others
*Default:* `'CANCEL'`

If a value other than `false` is specified, an item to let the user tell "cancel" is added to the item list. "[0] CANCEL" (default) is displayed, and if `0` key is pressed, `-1` is returned.
You can specify a label of this item other than `'CANCEL'`. A string such as `'Go back'` (empty string `''` also), something that is converted to string such as `Date`, a string that includes [placeholder](#placeholders) such as `'Next ${itemsCount} items'` are accepted.

#### <a name="utility_methods-keyinselect-additional_placeholders"></a>Additional Placeholders

The following additional [placeholder](#placeholders) parameters are available.

##### <a name="utility_methods-keyinselect-additional_placeholders-itemscount"></a>`itemsCount`

A length of a current `items` Array.

For example:

```js
items = ['item-A', 'item-B', 'item-C', 'item-D', 'item-E'];
index = readlineSync.keyInSelect(items, null,
{cancel: 'Show more items than ${itemsCount}'});
```

```console
[1] item-A
[2] item-B
[3] item-C
[4] item-D
[5] item-E
[0] Show more items than 5
```

If `true` is specified, an item to let the user tell "cancel" is added to the item list. "[0] CANCEL" is displayed, and if `0` key is pressed, `-1` is returned.
##### <a name="utility_methods-keyinselect-additional_placeholders-firstitem"></a>`firstItem`

A first item in a current `items` Array.

For example:

```js
index = readlineSync.keyInSelect(items, 'Choose ${firstItem} or another :');
```

##### <a name="utility_methods-keyinselect-additional_placeholders-lastitem"></a>`lastItem`

A last item in a current `items` Array.

For example:

```js
items = ['January', 'February', 'March', 'April', 'May', 'June'];
index = readlineSync.keyInSelect(items, null,
{cancel: 'In after ${lastItem}'});
```

```console
[1] January
[2] February
[3] March
[4] April
[5] May
[6] June
[0] In after June
```

## <a name="placeholders"></a>Placeholders

Expand Down
2 changes: 1 addition & 1 deletion lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* readlineSync
* https://github.com/anseki/readline-sync
*
* Copyright (c) 2015 anseki
* Copyright (c) 2016 anseki
* Licensed under the MIT license.
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/read.cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* readlineSync
* https://github.com/anseki/readline-sync
*
* Copyright (c) 2015 anseki
* Copyright (c) 2016 anseki
* Licensed under the MIT license.
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/read.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# readlineSync
# https://github.com/anseki/readline-sync
#
# Copyright (c) 2015 anseki
# Copyright (c) 2016 anseki
# Licensed under the MIT license.

Param(
Expand Down
2 changes: 1 addition & 1 deletion lib/read.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# readlineSync
# https://github.com/anseki/readline-sync
#
# Copyright (c) 2015 anseki
# Copyright (c) 2016 anseki
# Licensed under the MIT license.

# Use perl for compatibility of sed/awk of GNU / POSIX, BSD. (and tr)
Expand Down
19 changes: 14 additions & 5 deletions lib/readline-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* readlineSync
* https://github.com/anseki/readline-sync
*
* Copyright (c) 2015 anseki
* Copyright (c) 2016 anseki
* Licensed under the MIT license.
*/

Expand Down Expand Up @@ -1198,8 +1198,13 @@ exports.keyInSelect = function(items, query, options) {
// -------- forced
trueValue: null,
falseValue: null,
caseSensitive: false
// limit (by items)
caseSensitive: false,
// limit (by items),
phContent: function(param) {
return param === 'itemsCount' ? items.length + '' :
param === 'firstItem' ? (items[0] + '').trim() :
param === 'lastItem' ? (items[items.length - 1] + '').trim() : null;
}
}),
// added: guide, cancel
keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n';
Expand All @@ -1210,13 +1215,17 @@ exports.keyInSelect = function(items, query, options) {
var key = String.fromCharCode(charCode);
keylist += key;
key2i[key] = i;
display += '[' + key + '] ' + item.trim() + '\n';
display += '[' + key + '] ' + (item + '').trim() + '\n';
charCode = charCode === 57 /* '9' */ ? 97 /* 'a' */ : charCode + 1;
});
if (!options || options.cancel !== false) {
keylist += '0';
key2i['0'] = -1;
display += '[' + '0' + '] CANCEL\n';
/* jshint eqnull:true */
display += '[' + '0' + '] ' +
(options && options.cancel != null && typeof options.cancel !== 'boolean' ?
(options.cancel + '').trim() : 'CANCEL') + '\n';
/* jshint eqnull:false */
}
readOptions.limit = keylist;
display += '\n';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "readline-sync",
"version": "1.2.22",
"version": "1.3.0",
"title": "readlineSync",
"description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).",
"keywords": [
Expand Down

0 comments on commit ed02067

Please sign in to comment.