Skip to content

Commit

Permalink
Add spinner to status bar while running cargo task
Browse files Browse the repository at this point in the history
  • Loading branch information
W4RH4WK authored and KalitaAlexey committed Dec 17, 2016
1 parent 5d26fa0 commit 0158137
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
"dependencies": {
"tmp": "0.0.28",
"tree-kill": "^1.0.0",
"find-up": "^1.1.2"
"find-up": "^1.1.2",
"elegant-spinner": "^1.0.1"
}
}
34 changes: 34 additions & 0 deletions src/services/commandService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import * as path from 'path';
import kill = require('tree-kill');
import PathService from './pathService';

import elegantSpinner = require('elegant-spinner');
const spinner = elegantSpinner();

const errorRegex = /^(.*):(\d+):(\d+):\s+(\d+):(\d+)\s+(warning|error|note|help):\s+(.*)$/;

interface RustError {
Expand Down Expand Up @@ -221,6 +224,8 @@ export class CommandService {
private static diagnostics: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection('rust');
private static channel: ChannelWrapper = new ChannelWrapper(vscode.window.createOutputChannel('Cargo'));
private static currentTask: CargoTask;
private static statusBarItem: vscode.StatusBarItem;
private static spinnerUpdate: any;
public static errorFormat: ErrorFormat;

public static checkCommand(target: CheckTarget): vscode.Disposable {
Expand Down Expand Up @@ -493,6 +498,33 @@ export class CommandService {
return true;
}

private static showSpinner(): void {
if (this.statusBarItem == null) {
this.statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
this.statusBarItem.text = spinner();
this.statusBarItem.tooltip = 'Running Cargo Task';
}

this.statusBarItem.show();

if (this.spinnerUpdate == null) {
this.spinnerUpdate = setInterval(() => {
this.statusBarItem.text = spinner();
}, 50);
}
}

private static hideSpinner(): void {
if (this.spinnerUpdate != null) {
clearInterval(this.spinnerUpdate);
this.spinnerUpdate = null;
}

if (this.statusBarItem != null) {
this.statusBarItem.hide();
}
}

private static runCargo(args: string[], force = false): void {
if (force && this.currentTask) {
this.channel.setOwner(null);
Expand All @@ -517,8 +549,10 @@ export class CommandService {

PathService.cwd().then((value: string | Error) => {
if (typeof value === 'string') {
this.showSpinner();
const cwd = value;
this.currentTask.execute(args, cwd, this.channel).then(result => {
this.hideSpinner();
this.parseDiagnostics(cwd, result.output);
if (!result.success) {
const failedResult = <CargoTaskExecuteFailedResult>result;
Expand Down
4 changes: 4 additions & 0 deletions typings/elegant-spinner/elegant-spinner.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'elegant-spinner' {
function spinner(): () => string;
export = spinner;
}

0 comments on commit 0158137

Please sign in to comment.