Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoDanin committed Sep 18, 2022
1 parent 718a68d commit 4c5a38a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-hacker-typer",
"displayName": "VSCode Hacker Typer 2",
"description": "In progress",
"version": "0.1.1",
"version": "2.0.0",
"license": "MIT",
"engines": {
"vscode": "^1.27.0"
Expand All @@ -18,7 +18,9 @@
"activationEvents": [
"onCommand:com.tiagodanin.vscode-hacker-typer.recordMacro",
"onCommand:com.tiagodanin.vscode-hacker-typer.playMacro",
"onCommand:com.tiagodanin.vscode-hacker-typer.deleteMacro"
"onCommand:com.tiagodanin.vscode-hacker-typer.deleteMacro",
"onCommand:com.tiagodanin.vscode-hacker-typer.exportMacro",
"onCommand:com.tiagodanin.vscode-hacker-typer.importMacro"
],
"main": "./out/extension",
"contributes": {
Expand Down Expand Up @@ -94,6 +96,16 @@
"command": "com.tiagodanin.vscode-hacker-typer.exitMacro",
"title": "Exit Macro",
"category": "HackerTyper"
},
{
"command": "com.tiagodanin.vscode-hacker-typer.exportMacro",
"title": "Export Macro",
"category": "HackerTyper"
},
{
"command": "com.tiagodanin.vscode-hacker-typer.importMacro",
"title": "Import Macro",
"category": "HackerTyper"
}
]
},
Expand Down
70 changes: 70 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,76 @@ export function activate(context: vscode.ExtensionContext) {
)
context.subscriptions.push(onCommandExitMacro);

const onCommandExport = vscode.commands.registerCommand(
"com.tiagodanin.vscode-hacker-typer.exportMacro",
() => {
const storage = Storage.getInstance(context);
const items = storage.list();
vscode.window.showQuickPick(items.map(item => item.name)).then(picked => {
if (!picked) {
return;
}

const options: vscode.SaveDialogOptions = {
saveLabel: 'Export',
filters: {
JSON: ['json']
}
};

vscode.window.showSaveDialog(options).then((location: vscode.Uri | undefined) => {
if (location === undefined) { return; }

storage.export(picked, location, (error) => {
if (error) {
vscode.window.showErrorMessage(`Error exporting ${picked}`);
console.log(error);
return;
}

vscode.window.showInformationMessage(`Exported "${picked}"`);
});
});

});
}
);
context.subscriptions.push(onCommandExport);

const onCommandImport = vscode.commands.registerCommand(
"com.tiagodanin.vscode-hacker-typer.importMacro",
() => {
const storage = Storage.getInstance(context);

const options: vscode.OpenDialogOptions = {
canSelectMany: true,
openLabel: 'Import',
filters: {
JSON: ['json']
}
};

vscode.window.showOpenDialog(options).then((files: vscode.Uri[] | undefined) => {
if (files === undefined) {
return;
}

for (let i = 0; i < files.length; i++) {
storage.import(files[i], (error) => {
if (error) {
vscode.window.showErrorMessage(`Error importing ${files[i].fsPath}`);
console.log(error);
return;
}

vscode.window.showInformationMessage(`Imported "${files[i].fsPath}"`);
});
}
});
}
);
context.subscriptions.push(onCommandImport);

const onOpenFile = vscode.workspace.onDidOpenTextDocument((file) => {
console.log('file', file)
vscode.window.showInformationMessage(`File open: ${file}` );
Expand Down
1 change: 1 addition & 0 deletions src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function onType({ text }: { text: string }) {
() =>
new Promise((resolve, reject) => {
try {
// @ts-ignore
advanceBuffer(resolve, text);
} catch (e) {
console.log(e);
Expand Down
36 changes: 34 additions & 2 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as vscode from "vscode";
import * as Cache from "vscode-cache";
import { Buffer } from "./buffers";
import * as buffers from "./buffers";
import { SerializedBuffer, rehydrateBuffer } from "./rehydrate";

import * as fs from 'fs';
import * as path from 'path';

const LISTINGS = "HackerTyper:Listings";
const MACROS = "HackerTyper:Macros";

Expand All @@ -12,7 +15,7 @@ type Metadata = {
};

export type Macro = Metadata & {
buffers: Buffer[];
buffers: buffers.Buffer[];
};

export default class Storage {
Expand Down Expand Up @@ -63,4 +66,33 @@ export default class Storage {
this._listings.forget(name);
this._macros.forget(name);
}

public export(name: string, path: vscode.Uri, callback: (error: NodeJS.ErrnoException | null | undefined) => void): void {
const content = JSON.stringify(this._macros.get(name));

return fs.writeFile(path.fsPath, content, callback);
}

public import(uri: vscode.Uri, callback: (error: NodeJS.ErrnoException | null | undefined) => void): void {
const listings = this._listings;
const macros = this._macros;
fs.readFile(uri.fsPath, (error: NodeJS.ErrnoException | null | undefined, data: Buffer): void => {

if (error) {
callback(error);
}

const json = JSON.parse(data.toString());
const name = path.basename(uri.fsPath, '.json');
const operations = [
listings.put(name, { name, description: `Imported macro from ${uri.fsPath}` }),
macros.put(name, json)
];

Promise.all(operations)
.then(() => callback(undefined))
.catch(callback);
});
}
}

0 comments on commit 4c5a38a

Please sign in to comment.