rust/editors/code/src/status_display.ts

106 lines
2.5 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 {
2019-12-30 13:16:07 -06:00
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();
}
2019-12-30 13:16:07 -06:00
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
2019-12-30 16:30:35 -06:00
}] ${this.frame()}`;
} else {
2019-06-24 05:50:34 -05:00
this.statusBarItem!.text = `cargo ${
this.command
2019-12-30 16:30:35 -06:00
} ${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
}
2019-12-30 13:16:07 -06:00
hide() {
2019-03-31 07:51:17 -05:00
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
2019-04-13 15:13:21 -05:00
this.statusBarItem.hide();
}
2019-12-30 13:16:07 -06:00
dispose() {
2019-04-13 15:13:21 -05:00
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
this.statusBarItem.dispose();
2019-03-31 07:51:17 -05:00
}
2019-12-30 13:16:07 -06:00
handleProgressNotification(params: ProgressParams) {
const { token, value } = params;
2019-12-25 12:10:30 -06:00
if (token !== 'rustAnalyzer/cargoWatcher') {
return;
}
switch (value.kind) {
2019-12-25 12:10:30 -06:00
case 'begin':
this.show();
break;
2019-12-25 12:10:30 -06:00
case 'report':
if (value.message) {
this.packageName = value.message;
}
break;
2019-12-25 12:10:30 -06:00
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 {
2019-12-25 12:10:30 -06:00
token: string;
value: WorkDoneProgress;
}
enum WorkDoneProgressKind {
2019-12-25 12:10:30 -06:00
Begin = 'begin',
Report = 'report',
End = 'end',
}
interface WorkDoneProgress {
2019-12-25 12:10:30 -06:00
kind: WorkDoneProgressKind;
message?: string;
cancelable?: boolean;
percentage?: string;
}