rust/editors/code/src/commands/watch_status.ts

106 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-03-31 07:51:17 -05:00
import * as vscode from 'vscode';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
2019-04-13 15:13:21 -05:00
export class StatusDisplay implements vscode.Disposable {
public packageName?: string;
2019-03-31 07:51:17 -05:00
private i = 0;
private statusBarItem: vscode.StatusBarItem;
2019-06-24 05:02:20 -05:00
private command: string;
2019-03-31 07:51:17 -05:00
private timer?: NodeJS.Timeout;
2019-06-24 05:02:20 -05:00
constructor(command: string) {
2019-03-31 08:21:14 -05:00
this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
2019-12-09 12:57:55 -06:00
10,
2019-03-31 08:21:14 -05:00
);
2019-06-24 05:02:20 -05:00
this.command = command;
2019-03-31 07:51:17 -05:00
this.statusBarItem.hide();
}
public show() {
this.packageName = undefined;
2019-03-31 08:21:14 -05:00
this.timer =
this.timer ||
setInterval(() => {
if (this.packageName) {
2019-06-24 05:02:20 -05:00
this.statusBarItem!.text = `cargo ${this.command} [${
this.packageName
}] ${this.frame()}`;
} else {
2019-06-24 05:50:34 -05:00
this.statusBarItem!.text = `cargo ${
this.command
} ${this.frame()}`;
}
2019-03-31 08:21:14 -05:00
}, 300);
2019-03-31 07:51:17 -05:00
2019-04-13 15:13:21 -05:00
this.statusBarItem.show();
2019-03-31 07:51:17 -05:00
}
public hide() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
2019-04-13 15:13:21 -05:00
this.statusBarItem.hide();
}
public dispose() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
this.statusBarItem.dispose();
2019-03-31 07:51:17 -05:00
}
public handleProgressNotification(params: ProgressParams) {
const { token, value } = params;
if (token !== "rustAnalyzer/cargoWatcher") {
return;
}
console.log("Got progress notification", token, value)
switch (value.kind) {
case "begin":
this.show();
break;
case "report":
if (value.message) {
this.packageName = value.message;
}
break;
case "end":
this.hide();
break;
}
}
2019-03-31 07:51:17 -05:00
private frame() {
2019-03-31 08:21:14 -05:00
return spinnerFrames[(this.i = ++this.i % spinnerFrames.length)];
2019-03-31 07:51:17 -05:00
}
2019-03-31 08:21:14 -05:00
}
// FIXME: Replace this once vscode-languageclient is updated to LSP 3.15
interface ProgressParams {
token: string
value: WorkDoneProgress
}
enum WorkDoneProgressKind {
Begin = "begin",
Report = "report",
End = "end"
}
interface WorkDoneProgress {
kind: WorkDoneProgressKind,
message?: string
cancelable?: boolean
percentage?: string
}