From 03b31c0a002605980cb22e6ad1606b0077ea6902 Mon Sep 17 00:00:00 2001 From: nyanotech Date: Wed, 23 Mar 2022 10:59:14 +0000 Subject: [PATCH] Add a rot13 command --- package.json | 10 ++++++++++ src/extension.ts | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/package.json b/package.json index 18e2eb8..19684e2 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "activationEvents": [ "onCommand:recombobulator.base64", + "onCommand:recombobulator.rot13", "onCommand:recombobulator.unbase64" ], "contributes": { @@ -27,6 +28,11 @@ "category": "Recombobulator", "title": "base64 encode" }, + { + "command": "recombobulator.rot13", + "category": "Recombobulator", + "title": "rot13" + }, { "command": "recombobulator.unbase64", "category": "Recombobulator", @@ -39,6 +45,10 @@ "command": "recombobulator.base64", "when": "editorHasSelection" }, + { + "command": "recombobulator.rot13", + "when": "editorHasSelection" + }, { "command": "recombobulator.unbase64", "when": "editorHasSelection" diff --git a/src/extension.ts b/src/extension.ts index 2888513..c9e43f9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -8,6 +8,12 @@ function unbase64(a: string) { return Buffer.from(a, 'base64').toString("utf-8"); // TODO - handle different character encodings } +function rot13(a: string) { + const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const rotatedAlphabet = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; + return a.replace(/[a-zA-Z]/g, letter => rotatedAlphabet[alphabet.indexOf(letter)]); +} + function applyEdit(transform: Function) { const editor = vscode.window.activeTextEditor; @@ -31,6 +37,10 @@ export function activate(context: vscode.ExtensionContext) { applyEdit(base64); vscode.window.showInformationMessage('base64-encoded the selection!'); })); + context.subscriptions.push(vscode.commands.registerCommand('recombobulator.rot13', () => { + applyEdit(rot13); + vscode.window.showInformationMessage('rot13\'d the selection!'); + })); context.subscriptions.push(vscode.commands.registerCommand('recombobulator.unbase64', () => { applyEdit(unbase64); vscode.window.showInformationMessage('base64-decoded the selection!');