-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add screenshot command #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
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(); | ||
const pad = (num: number) => ("00" + num).slice(-2); | ||
|
||
return `${d.getFullYear()}-${pad(d.getMonth())}-${pad(d.getDay())}-${pad(d.getHours())}-${pad(d.getMinutes())}-${pad(d.getSeconds())}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nodejs doesn't have a built-in date formating function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not one for formatting it in a file-name-friendly way AFAIK. I've seen a fair number of custom packages to format dates which makes me think that there isn't a builtin for it. |
||
} | ||
|
||
const tempDirs: { [sharedKey: string]: string } = {}; | ||
export function getSharedTempDir(sharedKey: string): Promise<string> { | ||
if (tempDirs[sharedKey]) { | ||
return Promise.resolve(tempDirs[sharedKey]); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
temp.track(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for style points, we could call It is possible to unload and reload the extension without the process actually exiting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Earlier I confirmed that it cleared the temp file properly when closing the window or reloading the session/switching folders.
The main VSCode process doesn't exit, but the extension host does. Extensions are run in an independent sandboxed process which is restarted when you reload the window. |
||
temp.mkdir(sharedKey, (err, dirPath) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
tempDirs[sharedKey] = dirPath; | ||
resolve(dirPath); | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
export function openAndRead(path: string, offset: number, length: number, position: number): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
fs.open(path, 'r', (err, fd) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
const buffer = new Buffer(length); | ||
fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
resolve(buffer); | ||
}); | ||
}); | ||
}) | ||
} | ||
|
||
export async function verifyFileHeader(filePath: string, expectedHeader: Buffer | number[], offset?: number): Promise<boolean> { | ||
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running from the command palette is actually broken here because there is no parameter passed as
d
inBut some of the other commands are currently broken for the same reason, so we can leave this as is and fix them all later (I have a plan...).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just looked at this again and this command should not be showing up in the command palette. Not sure what's up. Perhaps a vscode bug.