Skip to content

Commit

Permalink
Merge pull request #124 from augustocdias/prompt_string
Browse files Browse the repository at this point in the history
Add support for `command:shellCommand.promptString` (#123)
  • Loading branch information
MarcelRobitaille authored Dec 16, 2024
2 parents 6108dfb + 4476e9e commit 0b7b936
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [1.14.0] 2024-11-09

- Add support for `command:shellCommand.promptString` (#123)

## [1.13.1] 2024-11-09

- Add support for warning if the command outputs on stderr (warnOnStderr)
Expand Down
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ Usage example:
}
```

## Available Commands

The extension makes two commands available:
1. `shellCommand.execute`: Runs a system command and shows a dropdown with the returned options. See [The command `shellCommand.execute`](#the-command-shellcommandexecute)
1. `shellCommand.promptString`: This is a reimplementation of the built-in `promptString`, but this version can be used in the command of `shellCommand.execute`. See [The command `shellCommand.promptString`](#the-command-shellcommandpromptstring)

## The command `shellCommand.execute`

### Field Separator

By default the extension returns the exact string value that was produced by the shell command and then shown and selected in 'Quick Pick' dialog. However, sometimes it is useful to show more descriptive information than the internal string value that is returned. This can be done by specifying a `fieldSeparator` and making the shell command return lines containing multiple fields separated by that value. Supported fields are:

```
Expand Down Expand Up @@ -82,7 +92,7 @@ VSCode renders it like this:

![Process Picker](https://github.com/augustocdias/vscode-shell-command/raw/master/process-picker.png)

Arguments for the extension:
### Arguments for `shellCommand.execute`

* `command`: the system command to be executed (must be in PATH). If given as an array, the elements are joined by spaces.
* `commandArgs`: if provided, `command` is interpreted as the binary to run and `commandArgs` are the arguments. This is useful if the binary you want to run has spaces (like `C:\Program Files\*`). This translates to `child_process.execFileSync(command, commandArgs)`.
Expand Down Expand Up @@ -114,6 +124,8 @@ As of today, the extension supports variable substitution for:

For a complete vscode variables documentation please refer to [vscode variables](https://code.visualstudio.com/docs/editor/variables-reference).

## Examples of the command `shellCommand.execute`

Dependent Input Variables Usage example:

```json
Expand Down Expand Up @@ -175,6 +187,39 @@ Example with `commandArgs`:
}
```


## The command `shellCommand.promptString`

The only reason to use `shellCommand.promptString` over `promptString` is that the former works inside of a `shellCommand.execute` command.

Here is an example of a `launch.json` configuration where the user is prompted to select a program to debug based on the output of `findProgram.sh`.
If some additional search is desired, the arguments of `findProgram.sh` can include `shellCommand.promptString`.
Note that it's not possible to put `${input:searchExpression}` where `searchExpression` is an input with the built-in `promptString` type. See [Limitations](#Limitations).
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Test",
"request": "launch",
"program": "${input:pickProgram}"
}
],
"inputs": [
{
"id": "pickProgram",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "findProgram.sh --search ${command:shellCommand.promptString}"
}
}
]
}
```

## Limitations

There are a few limitations to be aware of:

* in the main task command the input variables should appear (left to right) in order of dependence
Expand Down
23 changes: 20 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { ShellCommandException } from './util/exceptions';
// This is the type use by the vscode API
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function activate(this: any, context: vscode.ExtensionContext) {
const command = 'shellCommand.execute';
const userInputContext = new UserInputContext();
const callback = (args: { [key: string]: unknown }) => {

const handleExecute = (args: { [key: string]: unknown }) => {
try {
const handler = new CommandHandler(args, userInputContext, context, subprocess);
return handler.handle();
Expand All @@ -23,5 +23,22 @@ export function activate(this: any, context: vscode.ExtensionContext) {
}
};

context.subscriptions.push(vscode.commands.registerCommand(command, callback, this));
context.subscriptions.push(vscode.commands.registerCommand(
'shellCommand.execute',
handleExecute,
this,
));

// Reimplementation of promptString that can be used from inputs.
const handlePromptString = async () => {
const inputValue = await vscode.window.showInputBox();

return inputValue || '';
};

context.subscriptions.push(vscode.commands.registerCommand(
'shellCommand.promptString',
handlePromptString,
this,
));
}

0 comments on commit 0b7b936

Please sign in to comment.