Skip to content

Commit

Permalink
Implement+use helper for friendly status bar progressions
Browse files Browse the repository at this point in the history
  • Loading branch information
WasabiFan committed Aug 26, 2017
1 parent 89c36d8 commit 81bed87
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 29 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"commands": [
{
"command": "ev3devBrowser.openSshTerminal",
"title": "Open SSH Terminal",
"category": "ev3dev"
"title": "Open SSH Terminal"
},
{
"command": "ev3devBrowser.captureScreenshot",
Expand Down
47 changes: 23 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import * as dnode from 'dnode'
import * as fs from 'fs'
import * as net from 'net'
import * as dnode from 'dnode';
import * as fs from 'fs';
import * as net from 'net';

import * as path from 'path'
import * as ssh2 from 'ssh2'
import * as ssh2Streams from 'ssh2-streams'
import * as temp from 'temp'
import * as path from 'path';
import * as ssh2 from 'ssh2';
import * as ssh2Streams from 'ssh2-streams';
import * as temp from 'temp';

import * as vscode from 'vscode'
import * as vscode from 'vscode';

import * as dnssd from './dnssd'
import { sanitizedDateString, getSharedTempDir, verifyFileHeader } from './utils'
import * as dnssd from './dnssd';
import {
sanitizedDateString,
getSharedTempDir,
verifyFileHeader,
StatusBarProgressionMessage
} from './utils';

const S_IXUSR = parseInt('00100', 8);

Expand Down Expand Up @@ -536,13 +541,11 @@ class Device extends vscode.TreeItem {
}

async captureScreenshot() {
const statusBarMessage = vscode.window.createStatusBarItem();
statusBarMessage.text = "Attempting to capture screenshot...";
statusBarMessage.show();
const statusBarMessage = new StatusBarProgressionMessage("Attempting to capture screenshot...");

const handleCaptureError = (e) => {
vscode.window.showErrorMessage("Error capturing screenshot: " + (e.message || e));
statusBarMessage.dispose();
const handleCaptureError = e => {
vscode.window.showErrorMessage("Error capturing screenshot: " + (e.message || e));
statusBarMessage.finish();
}

try {
Expand All @@ -564,21 +567,17 @@ class Device extends vscode.TreeItem {

writeStream.on('error', (e: Error) => {
vscode.window.showErrorMessage("Error saving screenshot: " + e.message);
statusBarMessage.dispose();
statusBarMessage.finish();
});

writeStream.on('finish', async () => {
const pngHeader = [ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A ];
if(await verifyFileHeader(screenshotFile, pngHeader)) {
statusBarMessage.text = `Screenshot "${screenshotBaseName}" successfully captured`;
if (await verifyFileHeader(screenshotFile, pngHeader)) {
statusBarMessage.finish(`Screenshot "${screenshotBaseName}" successfully captured`);
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(screenshotFile));

// Remove message after giving time to read it
setTimeout(() => statusBarMessage.dispose(), 5000);
}
else {
handleCaptureError("The captured screenshot was not in the expected format."
+ " This may be caused by running an old version of 'fbcat' on the remote device; try updating the fbcat package.");
handleCaptureError("The screenshot was not in the correct format. You may need to upgrade to fbcat 0.5.0.");
}
});
}
Expand Down
60 changes: 57 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as temp from 'temp'
import * as fs from 'fs'
import { isArray } from 'util'
import * as vscode from 'vscode';
import * as temp from 'temp';
import * as fs from 'fs';
import { isArray } from 'util';

export function sanitizedDateString(date?: Date) {
const d = date || new Date();
Expand Down Expand Up @@ -54,4 +55,57 @@ export async function verifyFileHeader(filePath: string, expectedHeader: Buffer
const bufferExpectedHeader = isArray(expectedHeader) ? new Buffer(<number[]>expectedHeader) : <Buffer>expectedHeader;
const header = await openAndRead(filePath, 0, bufferExpectedHeader.length, offset);
return header.compare(bufferExpectedHeader) == 0;
}

export class StatusBarProgressionMessage {
private statusBarItem: vscode.StatusBarItem;

constructor(initialMessage?: string) {
this.statusBarItem = vscode.window.createStatusBarItem();
if (initialMessage) {
this.statusBarItem.text = initialMessage;
this.statusBarItem.show();
}
}

/**
* Updates the displayed message.
* @param newMessage The new message to display
*/
public update(newMessage: string) {
if (!this.statusBarItem) {
return;
}

this.statusBarItem.text = newMessage;
this.statusBarItem.show();
}

/**
* Marks the progression as being finished. If a message is specified, it is
* shown temporarily before the item disappears.
*
* Note that a message should always be provided if an external message
* indicating a failure won't be presented to the user.
* @param finalMessage The last message to show for a short period
* @param delay The amount of time the final message should be shown
*/
public finish(finalMessage?: string, delay: number = 5000) {
if (!this.statusBarItem) {
return;
}

if (finalMessage) {
this.update(finalMessage);
setTimeout(() => this.dispose(), delay);
}
else {
this.dispose();
}
}

private dispose() {
this.statusBarItem.dispose();
this.statusBarItem = null;
}
}

0 comments on commit 81bed87

Please sign in to comment.