]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/watch_status.ts
2404c3f164df62bdac20053b8d90c4b846158c2e
[rust.git] / editors / code / src / commands / watch_status.ts
1 import * as vscode from 'vscode';
2
3 const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
4
5 export class StatusDisplay implements vscode.Disposable {
6     public packageName?: string;
7
8     private i = 0;
9     private statusBarItem: vscode.StatusBarItem;
10     private command: string;
11     private timer?: NodeJS.Timeout;
12
13     constructor(command: string) {
14         this.statusBarItem = vscode.window.createStatusBarItem(
15             vscode.StatusBarAlignment.Left,
16             10,
17         );
18         this.command = command;
19         this.statusBarItem.hide();
20     }
21
22     public show() {
23         this.packageName = undefined;
24
25         this.timer =
26             this.timer ||
27             setInterval(() => {
28                 if (this.packageName) {
29                     this.statusBarItem!.text = `cargo ${this.command} [${
30                         this.packageName
31                     }] ${this.frame()}`;
32                 } else {
33                     this.statusBarItem!.text = `cargo ${
34                         this.command
35                     } ${this.frame()}`;
36                 }
37             }, 300);
38
39         this.statusBarItem.show();
40     }
41
42     public hide() {
43         if (this.timer) {
44             clearInterval(this.timer);
45             this.timer = undefined;
46         }
47
48         this.statusBarItem.hide();
49     }
50
51     public dispose() {
52         if (this.timer) {
53             clearInterval(this.timer);
54             this.timer = undefined;
55         }
56
57         this.statusBarItem.dispose();
58     }
59
60     public handleProgressNotification(params: ProgressParams) {
61         const { token, value } = params;
62         if (token !== "rustAnalyzer/cargoWatcher") {
63             return;
64         }
65
66         console.log("Got progress notification", token, value)
67         switch (value.kind) {
68             case "begin":
69                 this.show();
70                 break;
71
72             case "report":
73                 if (value.message) {
74                     this.packageName = value.message;
75                 }
76                 break;
77
78             case "end":
79                 this.hide();
80                 break;
81         }
82     }
83
84     private frame() {
85         return spinnerFrames[(this.i = ++this.i % spinnerFrames.length)];
86     }
87 }
88
89 // FIXME: Replace this once vscode-languageclient is updated to LSP 3.15
90 interface ProgressParams {
91     token: string
92     value: WorkDoneProgress
93 }
94
95 enum WorkDoneProgressKind {
96     Begin = "begin",
97     Report = "report",
98     End = "end"
99 }
100
101 interface WorkDoneProgress {
102     kind: WorkDoneProgressKind,
103     message?: string
104     cancelable?: boolean
105     percentage?: string
106 }