Skip to content

Commit 4c5a38a

Browse files
committed
1 parent 718a68d commit 4c5a38a

File tree

4 files changed

+119
-4
lines changed

4 files changed

+119
-4
lines changed

package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-hacker-typer",
33
"displayName": "VSCode Hacker Typer 2",
44
"description": "In progress",
5-
"version": "0.1.1",
5+
"version": "2.0.0",
66
"license": "MIT",
77
"engines": {
88
"vscode": "^1.27.0"
@@ -18,7 +18,9 @@
1818
"activationEvents": [
1919
"onCommand:com.tiagodanin.vscode-hacker-typer.recordMacro",
2020
"onCommand:com.tiagodanin.vscode-hacker-typer.playMacro",
21-
"onCommand:com.tiagodanin.vscode-hacker-typer.deleteMacro"
21+
"onCommand:com.tiagodanin.vscode-hacker-typer.deleteMacro",
22+
"onCommand:com.tiagodanin.vscode-hacker-typer.exportMacro",
23+
"onCommand:com.tiagodanin.vscode-hacker-typer.importMacro"
2224
],
2325
"main": "./out/extension",
2426
"contributes": {
@@ -94,6 +96,16 @@
9496
"command": "com.tiagodanin.vscode-hacker-typer.exitMacro",
9597
"title": "Exit Macro",
9698
"category": "HackerTyper"
99+
},
100+
{
101+
"command": "com.tiagodanin.vscode-hacker-typer.exportMacro",
102+
"title": "Export Macro",
103+
"category": "HackerTyper"
104+
},
105+
{
106+
"command": "com.tiagodanin.vscode-hacker-typer.importMacro",
107+
"title": "Import Macro",
108+
"category": "HackerTyper"
97109
}
98110
]
99111
},

src/extension.ts

+70
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,76 @@ export function activate(context: vscode.ExtensionContext) {
5555
)
5656
context.subscriptions.push(onCommandExitMacro);
5757

58+
const onCommandExport = vscode.commands.registerCommand(
59+
"com.tiagodanin.vscode-hacker-typer.exportMacro",
60+
() => {
61+
const storage = Storage.getInstance(context);
62+
const items = storage.list();
63+
vscode.window.showQuickPick(items.map(item => item.name)).then(picked => {
64+
if (!picked) {
65+
return;
66+
}
67+
68+
const options: vscode.SaveDialogOptions = {
69+
saveLabel: 'Export',
70+
filters: {
71+
JSON: ['json']
72+
}
73+
};
74+
75+
vscode.window.showSaveDialog(options).then((location: vscode.Uri | undefined) => {
76+
if (location === undefined) { return; }
77+
78+
storage.export(picked, location, (error) => {
79+
if (error) {
80+
vscode.window.showErrorMessage(`Error exporting ${picked}`);
81+
console.log(error);
82+
return;
83+
}
84+
85+
vscode.window.showInformationMessage(`Exported "${picked}"`);
86+
});
87+
});
88+
89+
});
90+
}
91+
);
92+
context.subscriptions.push(onCommandExport);
93+
94+
const onCommandImport = vscode.commands.registerCommand(
95+
"com.tiagodanin.vscode-hacker-typer.importMacro",
96+
() => {
97+
const storage = Storage.getInstance(context);
98+
99+
const options: vscode.OpenDialogOptions = {
100+
canSelectMany: true,
101+
openLabel: 'Import',
102+
filters: {
103+
JSON: ['json']
104+
}
105+
};
106+
107+
vscode.window.showOpenDialog(options).then((files: vscode.Uri[] | undefined) => {
108+
if (files === undefined) {
109+
return;
110+
}
111+
112+
for (let i = 0; i < files.length; i++) {
113+
storage.import(files[i], (error) => {
114+
if (error) {
115+
vscode.window.showErrorMessage(`Error importing ${files[i].fsPath}`);
116+
console.log(error);
117+
return;
118+
}
119+
120+
vscode.window.showInformationMessage(`Imported "${files[i].fsPath}"`);
121+
});
122+
}
123+
});
124+
}
125+
);
126+
context.subscriptions.push(onCommandImport);
127+
58128
const onOpenFile = vscode.workspace.onDidOpenTextDocument((file) => {
59129
console.log('file', file)
60130
vscode.window.showInformationMessage(`File open: ${file}` );

src/replay.ts

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function onType({ text }: { text: string }) {
9595
() =>
9696
new Promise((resolve, reject) => {
9797
try {
98+
// @ts-ignore
9899
advanceBuffer(resolve, text);
99100
} catch (e) {
100101
console.log(e);

src/storage.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as vscode from "vscode";
22
import * as Cache from "vscode-cache";
3-
import { Buffer } from "./buffers";
3+
import * as buffers from "./buffers";
44
import { SerializedBuffer, rehydrateBuffer } from "./rehydrate";
55

6+
import * as fs from 'fs';
7+
import * as path from 'path';
8+
69
const LISTINGS = "HackerTyper:Listings";
710
const MACROS = "HackerTyper:Macros";
811

@@ -12,7 +15,7 @@ type Metadata = {
1215
};
1316

1417
export type Macro = Metadata & {
15-
buffers: Buffer[];
18+
buffers: buffers.Buffer[];
1619
};
1720

1821
export default class Storage {
@@ -63,4 +66,33 @@ export default class Storage {
6366
this._listings.forget(name);
6467
this._macros.forget(name);
6568
}
69+
70+
public export(name: string, path: vscode.Uri, callback: (error: NodeJS.ErrnoException | null | undefined) => void): void {
71+
const content = JSON.stringify(this._macros.get(name));
72+
73+
return fs.writeFile(path.fsPath, content, callback);
74+
}
75+
76+
public import(uri: vscode.Uri, callback: (error: NodeJS.ErrnoException | null | undefined) => void): void {
77+
const listings = this._listings;
78+
const macros = this._macros;
79+
fs.readFile(uri.fsPath, (error: NodeJS.ErrnoException | null | undefined, data: Buffer): void => {
80+
81+
if (error) {
82+
callback(error);
83+
}
84+
85+
const json = JSON.parse(data.toString());
86+
const name = path.basename(uri.fsPath, '.json');
87+
const operations = [
88+
listings.put(name, { name, description: `Imported macro from ${uri.fsPath}` }),
89+
macros.put(name, json)
90+
];
91+
92+
Promise.all(operations)
93+
.then(() => callback(undefined))
94+
.catch(callback);
95+
});
96+
}
6697
}
98+

0 commit comments

Comments
 (0)