@oclif/plugin-search 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/bin/dev +17 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +5 -0
- package/bin/run.cmd +3 -0
- package/dist/commands/search.d.ts +6 -0
- package/dist/commands/search.js +52 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/util/autocomplete.d.ts +61 -0
- package/dist/util/autocomplete.js +250 -0
- package/oclif.manifest.json +1 -0
- package/package.json +68 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
### 0.0.1 (2022-08-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add search command ([99d50e0](https://github.com/oclif/plugin-search/commit/99d50e0f1335b0166ebe4f90629ddd2583d629b4))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* improve user experience ([4430085](https://github.com/oclif/plugin-search/commit/4430085df77d335cc7cef134cc626c2a61c0a556))
|
|
16
|
+
* types ([ddc87c5](https://github.com/oclif/plugin-search/commit/ddc87c58842d73c7d20627101857da43f94c0364))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Salesforce
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
@oclif/plugin-search
|
|
2
|
+
=================
|
|
3
|
+
|
|
4
|
+
A `search` command for your oclif CLI
|
|
5
|
+
|
|
6
|
+
[](https://oclif.io)
|
|
7
|
+
[](https://npmjs.org/package/oclif-plugin-search)
|
|
8
|
+
[](https://circleci.com/gh/oclif/plugin-search/tree/main)
|
|
9
|
+
[](https://npmjs.org/package/oclif-plugin-search)
|
|
10
|
+
[](https://github.com/oclif/plugin-search/blob/main/package.json)
|
|
11
|
+
|
|
12
|
+
<!-- toc -->
|
|
13
|
+
* [Usage](#usage)
|
|
14
|
+
* [Commands](#commands)
|
|
15
|
+
<!-- tocstop -->
|
|
16
|
+
# Usage
|
|
17
|
+
<!-- usage -->
|
|
18
|
+
```sh-session
|
|
19
|
+
$ npm install -g @oclif/plugin-search
|
|
20
|
+
$ @oclif/plugin-search COMMAND
|
|
21
|
+
running command...
|
|
22
|
+
$ @oclif/plugin-search (--version)
|
|
23
|
+
@oclif/plugin-search/0.0.1 linux-x64 node-v14.20.0
|
|
24
|
+
$ @oclif/plugin-search --help [COMMAND]
|
|
25
|
+
USAGE
|
|
26
|
+
$ @oclif/plugin-search COMMAND
|
|
27
|
+
...
|
|
28
|
+
```
|
|
29
|
+
<!-- usagestop -->
|
|
30
|
+
# Commands
|
|
31
|
+
<!-- commands -->
|
|
32
|
+
* [`@oclif/plugin-search search`](#oclifplugin-search-search)
|
|
33
|
+
|
|
34
|
+
## `@oclif/plugin-search search`
|
|
35
|
+
|
|
36
|
+
Search for a command.
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
USAGE
|
|
40
|
+
$ @oclif/plugin-search search
|
|
41
|
+
|
|
42
|
+
DESCRIPTION
|
|
43
|
+
Search for a command.
|
|
44
|
+
|
|
45
|
+
Once you select a command, hit enter and it will show the help for that command.
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
_See code: [dist/commands/search.ts](https://github.com/oclif/plugin-search/blob/v0.0.1/dist/commands/search.ts)_
|
|
49
|
+
<!-- commandsstop -->
|
package/bin/dev
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const oclif = require('@oclif/core')
|
|
4
|
+
|
|
5
|
+
const path = require('path')
|
|
6
|
+
const project = path.join(__dirname, '..', 'tsconfig.json')
|
|
7
|
+
|
|
8
|
+
// In dev mode -> use ts-node and dev plugins
|
|
9
|
+
process.env.NODE_ENV = 'development'
|
|
10
|
+
|
|
11
|
+
require('ts-node').register({project})
|
|
12
|
+
|
|
13
|
+
// In dev mode, always show stack traces
|
|
14
|
+
oclif.settings.debug = true;
|
|
15
|
+
|
|
16
|
+
// Start the CLI
|
|
17
|
+
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)
|
package/bin/dev.cmd
ADDED
package/bin/run
ADDED
package/bin/run.cmd
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
* Licensed under the BSD 3-Clause license.
|
|
6
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const inquirer_1 = require("inquirer");
|
|
10
|
+
const core_1 = require("@oclif/core");
|
|
11
|
+
const autocomplete_1 = require("../util/autocomplete");
|
|
12
|
+
class Search extends core_1.Command {
|
|
13
|
+
async run() {
|
|
14
|
+
const commands = this.config.commands;
|
|
15
|
+
for (const command of commands) {
|
|
16
|
+
command.readableId = (0, core_1.toConfiguredId)(command.id, this.config);
|
|
17
|
+
}
|
|
18
|
+
(0, inquirer_1.registerPrompt)('autocomplete-search', autocomplete_1.AutocompleteSearch);
|
|
19
|
+
const { command } = await (0, inquirer_1.prompt)([
|
|
20
|
+
{
|
|
21
|
+
type: 'autocomplete-search',
|
|
22
|
+
name: 'command',
|
|
23
|
+
message: 'Search for a command',
|
|
24
|
+
emptyText: 'Nothing found!',
|
|
25
|
+
pageSize: 10,
|
|
26
|
+
validate: (val) => {
|
|
27
|
+
return val ? true : 'Type something!';
|
|
28
|
+
},
|
|
29
|
+
searchOpts: {
|
|
30
|
+
keys: ['readableId'],
|
|
31
|
+
shouldSort: true,
|
|
32
|
+
threshold: 0.25,
|
|
33
|
+
ignoreLocation: true,
|
|
34
|
+
},
|
|
35
|
+
displayKey: 'readableId',
|
|
36
|
+
options: commands,
|
|
37
|
+
footer: (currentChoices, index) => {
|
|
38
|
+
const cmd = commands.find(c => c.readableId === currentChoices.getChoice(index).value);
|
|
39
|
+
if (cmd && cmd.summary)
|
|
40
|
+
return cmd.summary;
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
]);
|
|
44
|
+
if (command === 'help') {
|
|
45
|
+
return this.config.runCommand((0, core_1.toStandardizedId)(command, this.config), []);
|
|
46
|
+
}
|
|
47
|
+
return this.config.runCommand('help', [(0, core_1.toStandardizedId)(command, this.config)]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.default = Search;
|
|
51
|
+
Search.summary = 'Search for a command.';
|
|
52
|
+
Search.description = 'Once you select a command, hit enter and it will show the help for that command.';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { run } from '@oclif/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* TODO: this functionality should eventually be moved to a separate library so that other plugins can use it.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: this code was adapted from https://github.com/mokkabonna/inquirer-autocomplete-prompt
|
|
6
|
+
*/
|
|
7
|
+
import { Interface as ReadLineInterface } from 'node:readline';
|
|
8
|
+
import Base = require('inquirer/lib/prompts/base');
|
|
9
|
+
import InquirerChoices = require('inquirer/lib/objects/choices');
|
|
10
|
+
import * as Inquirer from 'inquirer';
|
|
11
|
+
import Fuse from 'fuse.js';
|
|
12
|
+
declare type SearchOptions = Fuse.IFuseOptions<Record<string, unknown>>;
|
|
13
|
+
declare type QuestionOpts = {
|
|
14
|
+
searchText: string;
|
|
15
|
+
footer: (currentChoices: Choices, index: number, output: string) => string;
|
|
16
|
+
searchOpts: SearchOptions;
|
|
17
|
+
emptyText: string;
|
|
18
|
+
displayKey: string;
|
|
19
|
+
};
|
|
20
|
+
export declare class Choices extends InquirerChoices {
|
|
21
|
+
constructor(choices: string[], options?: {
|
|
22
|
+
separator?: string;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export declare class AutocompleteSearch extends Base {
|
|
26
|
+
rl: ReadLineInterface;
|
|
27
|
+
opt: Inquirer.prompts.PromptOptions<Inquirer.Question> & QuestionOpts & {
|
|
28
|
+
loop: boolean;
|
|
29
|
+
pageSize: number;
|
|
30
|
+
options: Array<Record<string, unknown>>;
|
|
31
|
+
};
|
|
32
|
+
private currentChoices;
|
|
33
|
+
private firstRender;
|
|
34
|
+
private selected;
|
|
35
|
+
private initialValue;
|
|
36
|
+
private paginator;
|
|
37
|
+
private done;
|
|
38
|
+
private answer;
|
|
39
|
+
private shortAnswer;
|
|
40
|
+
private answerName;
|
|
41
|
+
private searching;
|
|
42
|
+
private nbChoices;
|
|
43
|
+
private searchedOnce;
|
|
44
|
+
private lastSearchTerm;
|
|
45
|
+
private fuse;
|
|
46
|
+
constructor(question: Inquirer.Question & Partial<QuestionOpts>, rl: ReadLineInterface, answers?: Record<string, any>);
|
|
47
|
+
_run(cb: (state: unknown) => void): Promise<AutocompleteSearch>;
|
|
48
|
+
render(error?: string): void;
|
|
49
|
+
onSubmit(line: string): void;
|
|
50
|
+
onSubmitAfterValidation(line: string): void;
|
|
51
|
+
search(searchTerm?: string): void;
|
|
52
|
+
ensureSelectedInRange(): void;
|
|
53
|
+
onKeypress(e: {
|
|
54
|
+
key: {
|
|
55
|
+
name: string;
|
|
56
|
+
ctrl: boolean;
|
|
57
|
+
};
|
|
58
|
+
value: string;
|
|
59
|
+
}): void;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
* Licensed under the BSD 3-Clause license.
|
|
6
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AutocompleteSearch = exports.Choices = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const ansiEscapes = require("ansi-escapes");
|
|
12
|
+
const Base = require("inquirer/lib/prompts/base");
|
|
13
|
+
const InquirerChoices = require("inquirer/lib/objects/choices");
|
|
14
|
+
const observe = require("inquirer/lib/utils/events");
|
|
15
|
+
const utils = require("inquirer/lib/utils/readline");
|
|
16
|
+
const Paginator = require("inquirer/lib/utils/paginator");
|
|
17
|
+
const operators_1 = require("rxjs/operators");
|
|
18
|
+
const Inquirer = (0, tslib_1.__importStar)(require("inquirer"));
|
|
19
|
+
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
|
|
20
|
+
const fuse_js_1 = (0, tslib_1.__importDefault)(require("fuse.js"));
|
|
21
|
+
class Choices extends InquirerChoices {
|
|
22
|
+
constructor(choices, options = {}) {
|
|
23
|
+
// @ts-expect-error
|
|
24
|
+
super(choices, options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Choices = Choices;
|
|
28
|
+
class AutocompleteSearch extends Base {
|
|
29
|
+
constructor(question, rl, answers = {}) {
|
|
30
|
+
super(question, rl, answers);
|
|
31
|
+
this.rl = rl;
|
|
32
|
+
this.selected = 0;
|
|
33
|
+
if (!this.opt.searchOpts) {
|
|
34
|
+
this.throwParamError('searchOpts');
|
|
35
|
+
}
|
|
36
|
+
this.fuse = new fuse_js_1.default(this.opt.options, this.opt.searchOpts);
|
|
37
|
+
this.currentChoices = new Choices([], {});
|
|
38
|
+
this.firstRender = true;
|
|
39
|
+
this.initialValue = this.opt.default;
|
|
40
|
+
const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
|
|
41
|
+
this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
|
|
42
|
+
}
|
|
43
|
+
async _run(cb) {
|
|
44
|
+
this.done = cb;
|
|
45
|
+
const events = observe(this.rl);
|
|
46
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
47
|
+
const dontHaveAnswer = () => this.answer === undefined;
|
|
48
|
+
events.line.pipe((0, operators_1.takeWhile)(dontHaveAnswer)).forEach(this.onSubmit.bind(this));
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
events.keypress.pipe((0, operators_1.takeWhile)(dontHaveAnswer)).forEach(this.onKeypress.bind(this));
|
|
51
|
+
// Call once at init
|
|
52
|
+
this.search();
|
|
53
|
+
return Promise.resolve(this);
|
|
54
|
+
}
|
|
55
|
+
render(error) {
|
|
56
|
+
// Render question
|
|
57
|
+
let content = this.getQuestion();
|
|
58
|
+
let bottomContent = '';
|
|
59
|
+
if (this.firstRender) {
|
|
60
|
+
content += chalk_1.default.dim('(Use arrow keys or type to search , tab to autocomplete)');
|
|
61
|
+
}
|
|
62
|
+
// Render choices or answer depending on the state
|
|
63
|
+
if (this.status === 'answered') {
|
|
64
|
+
content += chalk_1.default.cyan(this.shortAnswer || this.answerName || this.answer);
|
|
65
|
+
}
|
|
66
|
+
else if (this.searching) {
|
|
67
|
+
content += this.rl.line;
|
|
68
|
+
bottomContent += ' ' + chalk_1.default.dim(this.opt.searchText || 'Searching...');
|
|
69
|
+
}
|
|
70
|
+
else if (this.nbChoices) {
|
|
71
|
+
const choicesStr = renderList(this.currentChoices, this.selected);
|
|
72
|
+
content += this.rl.line;
|
|
73
|
+
const indexPosition = this.selected;
|
|
74
|
+
let realIndexPosition = 0;
|
|
75
|
+
this.currentChoices.choices.every((choice, index) => {
|
|
76
|
+
if (index > indexPosition) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (choice.type === 'separator') {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const name = choice.name;
|
|
83
|
+
realIndexPosition += name ? name.split('\n').length : 0;
|
|
84
|
+
return true;
|
|
85
|
+
});
|
|
86
|
+
bottomContent += this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
|
|
87
|
+
if (this.opt.footer) {
|
|
88
|
+
const footer = this.opt.footer(this.currentChoices, this.selected, bottomContent);
|
|
89
|
+
if (footer)
|
|
90
|
+
bottomContent += `\n${new Inquirer.Separator().line}\n${footer}`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
content += this.rl.line;
|
|
95
|
+
bottomContent += ` ${chalk_1.default.yellow(this.opt.emptyText || 'No results...')}`;
|
|
96
|
+
}
|
|
97
|
+
if (error) {
|
|
98
|
+
bottomContent += `\n${chalk_1.default.red('>> ')}${error}`;
|
|
99
|
+
}
|
|
100
|
+
this.firstRender = false;
|
|
101
|
+
this.screen.render(content, bottomContent);
|
|
102
|
+
}
|
|
103
|
+
onSubmit(line) {
|
|
104
|
+
let lineOrRl = line || this.rl.line;
|
|
105
|
+
if (!lineOrRl) {
|
|
106
|
+
lineOrRl = (this.opt.default === null ? '' : this.opt.default);
|
|
107
|
+
}
|
|
108
|
+
if (typeof this.opt.validate === 'function') {
|
|
109
|
+
const checkValidationResult = (validationResult) => {
|
|
110
|
+
if (validationResult !== true) {
|
|
111
|
+
this.render(validationResult || 'Enter something, tab to autocomplete!');
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
this.onSubmitAfterValidation(lineOrRl);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const choice = this.currentChoices.getChoice(this.selected);
|
|
118
|
+
const validationResult = this.opt.validate(choice, this.answers);
|
|
119
|
+
if (typeof validationResult === 'object' && typeof validationResult.then === 'function') {
|
|
120
|
+
void validationResult.then(checkValidationResult);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
checkValidationResult(validationResult);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.onSubmitAfterValidation(lineOrRl);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
onSubmitAfterValidation(line) {
|
|
131
|
+
let choice = {};
|
|
132
|
+
if (this.nbChoices <= this.selected) {
|
|
133
|
+
this.rl.write(line);
|
|
134
|
+
this.search(line);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (this.nbChoices) {
|
|
138
|
+
choice = this.currentChoices.getChoice(this.selected);
|
|
139
|
+
this.answer = choice.value;
|
|
140
|
+
this.answerName = choice.name;
|
|
141
|
+
this.shortAnswer = choice.short;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
this.rl.write(line);
|
|
145
|
+
this.search(line);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// @ts-expect-error
|
|
149
|
+
// eslint-disable-next-line unicorn/no-array-callback-reference, unicorn/no-array-method-this-argument
|
|
150
|
+
const value = this.opt.filter(choice.value, this.answers);
|
|
151
|
+
choice.value = value;
|
|
152
|
+
this.answer = value;
|
|
153
|
+
this.status = 'answered';
|
|
154
|
+
// Rerender prompt
|
|
155
|
+
this.render();
|
|
156
|
+
this.screen.done();
|
|
157
|
+
this.done(choice.value);
|
|
158
|
+
}
|
|
159
|
+
search(searchTerm) {
|
|
160
|
+
this.selected = 0;
|
|
161
|
+
// Only render searching state after first time
|
|
162
|
+
if (this.searchedOnce) {
|
|
163
|
+
this.searching = true;
|
|
164
|
+
this.currentChoices = new Choices([], {});
|
|
165
|
+
this.render(); // Now render current searching state
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this.searchedOnce = true;
|
|
169
|
+
}
|
|
170
|
+
this.lastSearchTerm = searchTerm;
|
|
171
|
+
const choices = searchTerm ?
|
|
172
|
+
this.fuse.search(searchTerm).map(el => el.item[this.opt.displayKey]) :
|
|
173
|
+
this.opt.options.map(el => el[this.opt.displayKey]).sort();
|
|
174
|
+
this.currentChoices = new Choices(choices, {});
|
|
175
|
+
// @ts-expect-error
|
|
176
|
+
const realChoices = this.currentChoices.filter(choice => choice.type !== 'separator' && !choice.disabled);
|
|
177
|
+
this.nbChoices = realChoices.length;
|
|
178
|
+
const selectedIndex = realChoices.findIndex(
|
|
179
|
+
// @ts-expect-error
|
|
180
|
+
choice => choice === this.initialValue || choice.value === this.initialValue);
|
|
181
|
+
if (selectedIndex >= 0) {
|
|
182
|
+
this.selected = selectedIndex;
|
|
183
|
+
}
|
|
184
|
+
this.searching = false;
|
|
185
|
+
this.render();
|
|
186
|
+
}
|
|
187
|
+
ensureSelectedInRange() {
|
|
188
|
+
const selectedIndex = Math.min(this.selected, this.nbChoices); // Not above currentChoices length - 1
|
|
189
|
+
this.selected = Math.max(selectedIndex, 0); // Not below 0
|
|
190
|
+
}
|
|
191
|
+
onKeypress(e) {
|
|
192
|
+
let len;
|
|
193
|
+
const keyName = (e.key && e.key.name) || undefined;
|
|
194
|
+
if (keyName === 'tab') {
|
|
195
|
+
if (this.currentChoices.getChoice(this.selected)) {
|
|
196
|
+
this.rl.write(ansiEscapes.cursorLeft);
|
|
197
|
+
const autoCompleted = this.currentChoices.getChoice(this.selected).value;
|
|
198
|
+
this.rl.write(ansiEscapes.cursorForward(autoCompleted.length));
|
|
199
|
+
// @ts-expect-error because readonly property
|
|
200
|
+
this.rl.line = autoCompleted;
|
|
201
|
+
this.render();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (keyName === 'down' || (keyName === 'n' && e.key.ctrl)) {
|
|
205
|
+
len = this.nbChoices;
|
|
206
|
+
this.selected = this.selected < len - 1 ? this.selected + 1 : 0;
|
|
207
|
+
this.ensureSelectedInRange();
|
|
208
|
+
this.render();
|
|
209
|
+
utils.up(this.rl, 2);
|
|
210
|
+
}
|
|
211
|
+
else if (keyName === 'up' || (keyName === 'p' && e.key.ctrl)) {
|
|
212
|
+
len = this.nbChoices;
|
|
213
|
+
this.selected = this.selected > 0 ? this.selected - 1 : len - 1;
|
|
214
|
+
this.ensureSelectedInRange();
|
|
215
|
+
this.render();
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.render(); // Render input automatically
|
|
219
|
+
// Only search if input have actually changed, not because of other keypresses
|
|
220
|
+
if (this.lastSearchTerm !== this.rl.line) {
|
|
221
|
+
this.search(this.rl.line); // Trigger new search
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.AutocompleteSearch = AutocompleteSearch;
|
|
227
|
+
function renderList(choices, pointer) {
|
|
228
|
+
let output = '';
|
|
229
|
+
let separatorOffset = 0;
|
|
230
|
+
choices.forEach((choice, i) => {
|
|
231
|
+
if (choice.type === 'separator') {
|
|
232
|
+
separatorOffset += 1;
|
|
233
|
+
output += ` ${choice}\n`;
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (choice.disabled) {
|
|
237
|
+
separatorOffset += 1;
|
|
238
|
+
output += ` - ${choice.name} ( ${typeof choice.disabled === 'string' ? choice.disabled : 'Disabled'})\n`;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const isSelected = i - separatorOffset === pointer;
|
|
242
|
+
// eslint-disable-next-line no-useless-concat
|
|
243
|
+
let line = (isSelected ? '❯' + ' ' : ' ') + choice.name;
|
|
244
|
+
if (isSelected) {
|
|
245
|
+
line = chalk_1.default.cyan(line);
|
|
246
|
+
}
|
|
247
|
+
output += `${line}\n`;
|
|
248
|
+
});
|
|
249
|
+
return output.replace(/\n$/, '');
|
|
250
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.0.1","commands":{"search":{"id":"search","summary":"Search for a command.","description":"Once you select a command, hit enter and it will show the help for that command.","strict":true,"pluginName":"@oclif/plugin-search","pluginAlias":"@oclif/plugin-search","pluginType":"core","aliases":[],"flags":{},"args":[]}}}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oclif/plugin-search",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A command for searching commands",
|
|
5
|
+
"author": "Salesforce",
|
|
6
|
+
"homepage": "https://github.com/oclif/plugin-search",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"repository": "oclif/plugin-search",
|
|
10
|
+
"files": [
|
|
11
|
+
"/bin",
|
|
12
|
+
"/dist",
|
|
13
|
+
"/npm-shrinkwrap.json",
|
|
14
|
+
"/oclif.manifest.json"
|
|
15
|
+
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@oclif/core": "^1.12.1",
|
|
18
|
+
"chalk": "^4",
|
|
19
|
+
"fuse.js": "^6.6.2",
|
|
20
|
+
"inquirer": "^8.2.4",
|
|
21
|
+
"rxjs": "^7.5.6"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@oclif/plugin-help": "^5",
|
|
25
|
+
"@oclif/plugin-plugins": "^2.0.1",
|
|
26
|
+
"@oclif/test": "^2",
|
|
27
|
+
"@types/chai": "^4",
|
|
28
|
+
"@types/inquirer": "^8.2.1",
|
|
29
|
+
"@types/mocha": "^9.0.0",
|
|
30
|
+
"@types/node": "^16.9.4",
|
|
31
|
+
"chai": "^4",
|
|
32
|
+
"eslint": "^7.32.0",
|
|
33
|
+
"eslint-config-oclif": "^4",
|
|
34
|
+
"eslint-config-oclif-typescript": "^1.0.2",
|
|
35
|
+
"globby": "^11",
|
|
36
|
+
"mocha": "^9",
|
|
37
|
+
"oclif": "^3",
|
|
38
|
+
"shx": "^0.3.3",
|
|
39
|
+
"ts-node": "^10.2.1",
|
|
40
|
+
"tslib": "^2.3.1",
|
|
41
|
+
"typescript": "^4.4.3"
|
|
42
|
+
},
|
|
43
|
+
"oclif": {
|
|
44
|
+
"commands": "./dist/commands",
|
|
45
|
+
"devPlugins": [
|
|
46
|
+
"@oclif/plugin-help",
|
|
47
|
+
"@oclif/plugin-plugins"
|
|
48
|
+
],
|
|
49
|
+
"topicSeparator": " "
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "shx rm -rf dist && tsc -b",
|
|
53
|
+
"lint": "eslint . --ext .ts --config .eslintrc",
|
|
54
|
+
"postpack": "shx rm -f oclif.manifest.json",
|
|
55
|
+
"posttest": "yarn lint",
|
|
56
|
+
"prepack": "yarn build && oclif manifest && oclif readme",
|
|
57
|
+
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
|
|
58
|
+
"version": "oclif readme && git add README.md"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=14.0.0"
|
|
62
|
+
},
|
|
63
|
+
"bugs": "https://github.com/oclif/plugin-search/issues",
|
|
64
|
+
"keywords": [
|
|
65
|
+
"oclif"
|
|
66
|
+
],
|
|
67
|
+
"types": "dist/index.d.ts"
|
|
68
|
+
}
|