Skip to content

Commit

Permalink
Merge pull request #105 from oNaiPs/automatically_show_output_panel
Browse files Browse the repository at this point in the history
Automatically show output panel
  • Loading branch information
bmingles authored Oct 12, 2024
2 parents 672ea96 + fba42ac commit 3d056b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Add "emeraldwalk.runonsave" configuration to user or workspace settings.
- `message` - Message to output before this command.
- `messageAfter` - Message to output after this command has finished.
- `showElapsed` - Show total elapsed time after this command.
- `autoShowOutputPanel` - Automatically shows the output panel:
- `never` - Never changes the output panel visibility (default).
- `always` - Shows output panel when the first command starts.
- `error` - Shows output panel when a command fails.

### Notes on RegEx Options

Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
"type": "boolean",
"description": "Print elapsed time for the command.",
"default": false
},
"autoShowOutputPanel": {
"description": "Automatically shows the output panel.",
"type": "string",
"default": "never",
"enum": [
"never",
"always",
"error"
]
}
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { exec } from 'child_process';
import type { ICommand, IConfig } from './model';
import type { ICommand, IConfig, IExecResult } from './model';

export function activate(context: vscode.ExtensionContext): void {
const extension = new RunOnSaveExtension(context);
Expand Down Expand Up @@ -55,13 +55,17 @@ class RunOnSaveExtension {
const startMs = performance.now();
let pendingCount = cmds.length;

const onCmdComplete = (cfg: ICommand, elapsedMs: number) => {
const onCmdComplete = (cfg: ICommand, res: IExecResult) => {
--pendingCount;
this.showOutputMessageIfDefined(cfg.messageAfter);
this.showOutputMessageIfDefined(
cfg.showElapsed && `Elapsed ms: ${elapsedMs}`,
cfg.showElapsed && `Elapsed ms: ${res.elapsedMs}`,
);

if (cfg.autoShowOutputPanel === 'error' && res.statusCode !== 0) {
this._outputChannel.show(true);
}

if (pendingCount === 0) {
this.showOutputMessageIfDefined(this._config.messageAfter);

Expand All @@ -79,8 +83,12 @@ class RunOnSaveExtension {

this.showOutputMessageIfDefined(cfg.message);

if (cfg.autoShowOutputPanel === 'always') {
this._outputChannel.show(true);
}

if (cfg.cmd == null) {
onCmdComplete(cfg, 0);
onCmdComplete(cfg, { elapsedMs: 0, statusCode: 0 });
continue;
}

Expand Down Expand Up @@ -109,7 +117,7 @@ class RunOnSaveExtension {
private _getExecPromise(
cfg: ICommand,
document: vscode.TextDocument,
): Promise<number> {
): Promise<IExecResult> {
return new Promise((resolve) => {
const startMs = performance.now();

Expand All @@ -120,10 +128,12 @@ class RunOnSaveExtension {
this.showOutputMessage(e.message);
// Don't reject since we want to be able to chain and handle
// message properties even if this errors
resolve(performance.now() - startMs);
// Returns a status code different than zero to optionally show output
// panel with error
resolve({ elapsedMs: performance.now() - startMs, statusCode: 1 });
});
child.on('exit', (_e) => {
resolve(performance.now() - startMs);
child.on('exit', (statusCode) => {
resolve({ elapsedMs: performance.now() - startMs, statusCode });
});
});
}
Expand Down Expand Up @@ -287,6 +297,7 @@ class RunOnSaveExtension {
cmd: cmdStr,
isAsync: !!cfg.isAsync,
showElapsed: cfg.showElapsed,
autoShowOutputPanel: cfg.autoShowOutputPanel,
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ export interface ICommand extends IMessageConfig {
notMatch?: string;
cmd?: string;
isAsync: boolean;
autoShowOutputPanel?: "always" | "error" | "never";
}

export interface IConfig extends IMessageConfig {
shell: string;
autoClearConsole: boolean;
commands: Array<ICommand>;
}

export interface IExecResult {
statusCode: number,
elapsedMs: number,
}

0 comments on commit 3d056b9

Please sign in to comment.