feat: added translation, updated readme

This commit is contained in:
2025-11-21 16:53:11 +01:00
parent 19f0ccb126
commit ea431c9401
3 changed files with 90 additions and 0 deletions

57
ui.ts
View File

@@ -42,6 +42,7 @@ export async function showMainMenu(): Promise<string> {
{ name: "Start New Game", value: "play" },
{ name: "View Statistics", value: "stats" },
{ name: "Morse Code Reference", value: "reference" },
{ name: "Translator", value: "translator" },
{ name: "Reset Statistics", value: "reset" },
{ name: "Exit", value: "exit" },
],
@@ -50,6 +51,62 @@ export async function showMainMenu(): Promise<string> {
return action;
}
/**
* Show translator utility
*/
export async function showTranslator(): Promise<void> {
clearScreen();
console.log(colors.bold.cyan("\n=== Morse Code Translator ===\n"));
const direction = await Select.prompt({
message: "Select translation direction:",
options: [
{ name: "Text → Morse Code", value: "toMorse" },
{ name: "Morse Code → Text", value: "toText" },
],
});
if (direction === "toMorse") {
const text = await Input.prompt({
message: "Enter text to translate:",
validate: (value) => {
if (!value.trim()) {
return "Please enter some text";
}
return true;
},
});
const morse = textToMorse(text.toUpperCase());
console.log(colors.bold.green("\nTranslation:"));
console.log(colors.yellow(morse));
} else {
const morse = await Input.prompt({
message: "Enter Morse code to translate (use dots and dashes):",
validate: (value) => {
if (!value.trim()) {
return "Please enter some Morse code";
}
return true;
},
});
const text = morseToText(morse);
console.log(colors.bold.green("\nTranslation:"));
console.log(colors.yellow(text));
}
console.log(colors.gray("\nPress Enter to continue..."));
await new Promise((resolve) => {
const buf = new Uint8Array(1);
Deno.stdin.setRaw(true);
Deno.stdin.read(buf).then(() => {
Deno.stdin.setRaw(false);
resolve(undefined);
});
});
}
/**
* Select game mode
*/