vscode-texinfo/src/utils/misc.ts

56 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-10-03 18:04:18 +00:00
/**
2020-10-24 21:45:32 +00:00
* utils/misc.ts
2020-10-03 18:04:18 +00:00
*
* @author CismonX <admin@cismon.net>
* @license MIT
*/
import * as child_process from 'child_process';
import * as vscode from 'vscode';
2020-10-24 21:45:32 +00:00
import { ExecResult } from './types';
2020-10-03 18:04:18 +00:00
/**
2020-10-22 22:40:41 +00:00
* Execute command and fetch output.
2020-10-03 18:04:18 +00:00
*
* @param path Path to the executable file.
* @param args Arguments to be passed to the command.
* @param maxBuffer Max output buffer size.
2020-10-10 17:36:05 +00:00
* @returns The output data, or `undefined` if execution fails.
2020-10-03 18:04:18 +00:00
*/
export function exec(path: string, args: string[], maxBuffer: number) {
2020-10-24 15:50:20 +00:00
return new Promise<ExecResult>(resolve => {
2020-10-03 18:04:18 +00:00
child_process.execFile(path, args, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
if (error) {
2020-10-24 15:50:20 +00:00
resolve({ error: stderr ? stderr : error.message });
2020-10-03 18:04:18 +00:00
} else {
2020-10-24 15:50:20 +00:00
resolve({ data: stdout, error: stderr });
2020-10-03 18:04:18 +00:00
}
});
});
}
/**
2020-10-24 21:45:32 +00:00
* Open a prompt with two buttons, "Confirm" and "Cancel", and wait for user action.
*
2020-10-24 21:45:32 +00:00
* @param message The message to be displayed on the prompt.
* @param confirm Text to be displayed on the "Confirm" button.
* @param error Whether the prompt is shown as an error message. Default false.
* @returns Whether the user clicked the "Confirm" button.
*/
2020-10-24 21:45:32 +00:00
export async function prompt(message: string, confirm: string, error = false) {
const func = error ? vscode.window.showErrorMessage : vscode.window.showInformationMessage;
return confirm === await func(message, confirm, 'Cancel');
}
2020-10-22 22:40:41 +00:00
2020-10-24 15:51:44 +00:00
/**
* Convert line numbers to VSCode range.
*
* @param startLine
* @param endLine Default to `startLine`.
*/
export function lineNumToRange(startLine: number, endLine = startLine) {
const startPosition = new vscode.Position(startLine, 0);
const endPosition = new vscode.Position(endLine, Number.MAX_SAFE_INTEGER);
return new vscode.Range(startPosition, endPosition);
}