chore: claude code refactor (7.5/10)
This commit is contained in:
47
ui.ts
47
ui.ts
@@ -1,6 +1,13 @@
|
||||
// UI utilities and game interface
|
||||
|
||||
import { colors } from "@cliffy/ansi/colors";
|
||||
|
||||
// UI Constants
|
||||
const WAIT_TIMES = {
|
||||
DEFAULT: 2000, // 2 seconds for default modes
|
||||
WORDS: 6000, // 6 seconds for words mode
|
||||
PHRASES: 8000, // 8 seconds for phrases mode
|
||||
} as const;
|
||||
import { Confirm } from "@cliffy/prompt/confirm";
|
||||
import { Input } from "@cliffy/prompt/input";
|
||||
import { Select } from "@cliffy/prompt/select";
|
||||
@@ -100,10 +107,10 @@ export async function showTranslator(): Promise<void> {
|
||||
await new Promise((resolve) => {
|
||||
const buf = new Uint8Array(1);
|
||||
Deno.stdin.setRaw(true);
|
||||
Deno.stdin.read(buf).then(() => {
|
||||
Deno.stdin.setRaw(false);
|
||||
resolve(undefined);
|
||||
});
|
||||
Deno.stdin.read(buf)
|
||||
.then(() => resolve(undefined))
|
||||
.catch(() => resolve(undefined))
|
||||
.finally(() => Deno.stdin.setRaw(false));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -248,23 +255,35 @@ export async function playRound(
|
||||
);
|
||||
console.log(colors.dim(" Use / for word spaces\n"));
|
||||
|
||||
// Create a promise that rejects after the time limit
|
||||
const timeoutPromise = new Promise<string>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error("Time's up!"));
|
||||
}, timeLimit * 1000);
|
||||
});
|
||||
|
||||
// Get user input with timeout
|
||||
let userInput = "";
|
||||
let timeoutId: number | undefined;
|
||||
|
||||
try {
|
||||
// Create a promise that rejects after the time limit
|
||||
const timeoutPromise = new Promise<string>((_, reject) => {
|
||||
timeoutId = setTimeout(() => {
|
||||
reject(new Error("Time's up!"));
|
||||
}, timeLimit * 1000);
|
||||
});
|
||||
|
||||
const inputPromise = Input.prompt({
|
||||
message: "Your answer:",
|
||||
minLength: 0,
|
||||
});
|
||||
|
||||
userInput = await Promise.race([inputPromise, timeoutPromise]);
|
||||
|
||||
// Clear timeout if input was received first
|
||||
if (timeoutId !== undefined) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
} catch (error) {
|
||||
// Clear timeout on error
|
||||
if (timeoutId !== undefined) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
if (error instanceof Error && error.message === "Time's up!") {
|
||||
console.log(colors.red("\n⏰ Time's up!"));
|
||||
userInput = ""; // Empty input for timeout
|
||||
@@ -299,11 +318,11 @@ export async function playRound(
|
||||
}
|
||||
|
||||
// Longer wait time for words and phrases
|
||||
let waitTime = 2000; // Default: 2 seconds
|
||||
let waitTime: number = WAIT_TIMES.DEFAULT;
|
||||
if (session.config.mode === "words") {
|
||||
waitTime = 6000; // 6 seconds for words
|
||||
waitTime = WAIT_TIMES.WORDS;
|
||||
} else if (session.config.mode === "phrases") {
|
||||
waitTime = 8000; // 8 seconds for phrases
|
||||
waitTime = WAIT_TIMES.PHRASES;
|
||||
}
|
||||
|
||||
// Wait for user to continue
|
||||
|
||||
Reference in New Issue
Block a user