Add a rot13 command

This commit is contained in:
nyanotech 2022-03-23 10:59:14 +00:00
parent cd85661f8d
commit 03b31c0a00
Signed by: nyanotech
GPG Key ID: D2D0A9E8F160472B
2 changed files with 20 additions and 0 deletions

View File

@ -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"

View File

@ -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!');