aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/status_display.ts
blob: 0f5f6ef99e64c0fd1744de1c0820cc5a99ec409f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as vscode from 'vscode';

import { WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd, Disposable } from 'vscode-languageclient';

import { Ctx } from './ctx';

const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];

export function activateStatusDisplay(ctx: Ctx) {
    const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
    ctx.pushCleanup(statusDisplay);
    const client = ctx.client;
    if (client != null) {
        ctx.pushCleanup(client.onProgress(
            WorkDoneProgress.type,
            'rustAnalyzer/cargoWatcher',
            params => statusDisplay.handleProgressNotification(params)
        ));
    }
}

class StatusDisplay implements Disposable {
    packageName?: string;

    private i: number = 0;
    private statusBarItem: vscode.StatusBarItem;
    private command: string;
    private timer?: NodeJS.Timeout;

    constructor(command: string) {
        this.statusBarItem = vscode.window.createStatusBarItem(
            vscode.StatusBarAlignment.Left,
            10,
        );
        this.command = command;
        this.statusBarItem.hide();
    }

    show() {
        this.packageName = undefined;

        this.timer =
            this.timer ||
            setInterval(() => {
                this.tick();
                this.refreshLabel();
            }, 300);

        this.statusBarItem.show();
    }

    hide() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = undefined;
        }

        this.statusBarItem.hide();
    }

    dispose() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = undefined;
        }

        this.statusBarItem.dispose();
    }

    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}`;
        }
    }

    handleProgressNotification(params: WorkDoneProgressBegin | WorkDoneProgressReport | WorkDoneProgressEnd) {
        switch (params.kind) {
            case 'begin':
                this.show();
                break;

            case 'report':
                if (params.message) {
                    this.packageName = params.message;
                    this.refreshLabel();
                }
                break;

            case 'end':
                this.hide();
                break;
        }
    }

    private tick() {
        this.i = (this.i + 1) % spinnerFrames.length;
    }
}