Skip to content
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

Implement export & import #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,877 changes: 273 additions & 1,604 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@
"activationEvents": [
"onCommand:jevakallio.vscode-hacker-typer.recordMacro",
"onCommand:jevakallio.vscode-hacker-typer.playMacro",
"onCommand:jevakallio.vscode-hacker-typer.deleteMacro"
"onCommand:jevakallio.vscode-hacker-typer.removeMacro",
"onCommand:jevakallio.vscode-hacker-typer.exportMacro",
"onCommand:jevakallio.vscode-hacker-typer.importMacro"
],
"main": "./out/extension",
"contributes": {
@@ -74,6 +76,16 @@
"title": "Save Macro",
"category": "HackerTyper"
},
{
"command": "jevakallio.vscode-hacker-typer.exportMacro",
"title": "Export Macro",
"category": "HackerTyper"
},
{
"command": "jevakallio.vscode-hacker-typer.importMacro",
"title": "Import Macro",
"category": "HackerTyper"
},
{
"command": "jevakallio.vscode-hacker-typer.removeMacro",
"title": "Remove Macro",
71 changes: 70 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -45,6 +45,75 @@ export function activate(context: vscode.ExtensionContext) {
}
);

let exprt = vscode.commands.registerCommand(
"jevakallio.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.exprt(picked, location, (err) => {
if (err) {
vscode.window.showErrorMessage(`Error exporting ${picked}`);
console.log(err);
return;
}

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

});
}
);


let imprt = vscode.commands.registerCommand(
"jevakallio.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.imprt(files[i], (err) => {
if (err) {
vscode.window.showErrorMessage(`Error importing ${files[i].fsPath}`);
console.log(err);
return;
}

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

// @TODO dispose
let type = vscode.commands.registerCommand("type", replay.onType);

@@ -55,7 +124,7 @@ export function activate(context: vscode.ExtensionContext) {
replay.onBackspace
);

context.subscriptions.push(record, play, type, backspace, remove);
context.subscriptions.push(record, play, type, backspace, remove, exprt, imprt);
}

// this method is called when your extension is deactivated
34 changes: 32 additions & 2 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as vscode from "vscode";
import * as Cache from "vscode-cache";
import { Buffer } from "./buffers";
import * as bf 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";
@@ -12,7 +14,7 @@ type Metadata = {
};

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

/*
@@ -84,4 +86,32 @@ export default class Storage {
this._listings.forget(name);
this._macros.forget(name);
}

public exprt(name: string, path: vscode.Uri, callback: (err: NodeJS.ErrnoException) => void): void {
const content = JSON.stringify(this._macros.get(name));

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

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

if (err) {
callback(err);
}

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