rust/editors/code/src/status_display.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-03-31 07:51:17 -05:00
import * as vscode from 'vscode';
2020-02-02 15:23:01 -06:00
import { WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd, Disposable } from 'vscode-languageclient';
2020-01-15 04:47:56 -06:00
2019-12-31 10:22:43 -06:00
import { Ctx } from './ctx';
2019-03-31 07:51:17 -05:00
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
2019-12-31 10:22:43 -06:00
export function activateStatusDisplay(ctx: Ctx) {
const statusDisplay = new StatusDisplay(ctx.config.checkOnSave.command);
2019-12-31 10:22:43 -06:00
ctx.pushCleanup(statusDisplay);
2020-02-17 06:40:20 -06:00
const client = ctx.client;
if (client != null) {
ctx.pushCleanup(client.onProgress(
WorkDoneProgress.type,
'rustAnalyzer/cargoWatcher',
params => statusDisplay.handleProgressNotification(params)
2020-02-17 14:09:44 -06:00
));
2020-02-17 06:40:20 -06:00
}
2019-12-31 10:22:43 -06:00
}
class StatusDisplay implements Disposable {
2019-12-30 13:16:07 -06:00
packageName?: string;
private i: number = 0;
2019-03-31 07:51:17 -05:00
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(() => {
this.tick();
this.refreshLabel();
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
}
refreshLabel() {
if (this.packageName) {
this.statusBarItem.text = `${spinnerFrames[this.i]} cargo ${this.command} [${this.packageName}]`;
} else {
this.statusBarItem.text = `${spinnerFrames[this.i]} cargo ${this.command}`;
}
}
2020-01-15 04:47:56 -06:00
handleProgressNotification(params: WorkDoneProgressBegin | WorkDoneProgressReport | WorkDoneProgressEnd) {
switch (params.kind) {
2019-12-25 12:10:30 -06:00
case 'begin':
this.show();
break;
2019-12-25 12:10:30 -06:00
case 'report':
2020-01-15 04:47:56 -06:00
if (params.message) {
this.packageName = params.message;
this.refreshLabel();
}
break;
2019-12-25 12:10:30 -06:00
case 'end':
this.hide();
break;
}
}
private tick() {
this.i = (this.i + 1) % spinnerFrames.length;
2019-03-31 07:51:17 -05:00
}
2019-03-31 08:21:14 -05:00
}