This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from AtkinsSJ/command-name-completion
Add completers for command names and options
- Loading branch information
Showing
4 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2024 Puter Technologies Inc. | ||
* | ||
* This file is part of Phoenix Shell. | ||
* | ||
* Phoenix Shell is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
export class CommandCompleter { | ||
async getCompletions (ctx, inputState) { | ||
const { builtins } = ctx.registries; | ||
const query = inputState.input; | ||
|
||
if ( query === '' ) { | ||
return []; | ||
} | ||
|
||
const completions = []; | ||
|
||
// TODO: Match executable names as well as builtins | ||
for ( const commandName of Object.keys(builtins) ) { | ||
if ( commandName.startsWith(query) ) { | ||
completions.push(commandName.slice(query.length)); | ||
} | ||
} | ||
|
||
return completions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (C) 2024 Puter Technologies Inc. | ||
* | ||
* This file is part of Phoenix Shell. | ||
* | ||
* Phoenix Shell is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
import { DEFAULT_OPTIONS } from '../coreutils/coreutil_lib/help.js'; | ||
|
||
export class OptionCompleter { | ||
async getCompletions (ctx, inputState) { | ||
const { builtins } = ctx.registries; | ||
const query = inputState.input; | ||
|
||
if ( query === '' ) { | ||
return []; | ||
} | ||
|
||
// TODO: Query the command through the providers system. | ||
// Or, we could include the command in the context that's given to completers? | ||
const command = builtins[inputState.tokens[0]]; | ||
if ( ! command ) { | ||
return []; | ||
} | ||
|
||
const completions = []; | ||
|
||
const processOptions = (options) => { | ||
for ( const optionName of Object.keys(options) ) { | ||
const prefixedOptionName = `--${optionName}`; | ||
if ( prefixedOptionName.startsWith(query) ) { | ||
completions.push(prefixedOptionName.slice(query.length)); | ||
} | ||
} | ||
}; | ||
|
||
// TODO: Only check these for builtins! | ||
processOptions(DEFAULT_OPTIONS); | ||
|
||
if ( command.args?.options ) { | ||
processOptions(command.args.options); | ||
} | ||
|
||
return completions; | ||
} | ||
} |