Slight improvements

This commit is contained in:
2025-08-06 14:01:45 -05:00
parent 7f0aa63843
commit 7877651ab0
3 changed files with 57 additions and 39 deletions
+7
View File
@@ -20,6 +20,13 @@
"command": "speed-code.cancelSpeedRun", "command": "speed-code.cancelSpeedRun",
"title": "Cancel Speed Run" "title": "Cancel Speed Run"
} }
],
"keybindings": [
{
"command": "speed-code.submitSpeedRun",
"key": "shift+enter",
"when": "editorTextFocus"
}
] ]
}, },
"scripts": { "scripts": {
+31 -37
View File
@@ -3,6 +3,7 @@ import os from 'os';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import testCode from './tester'; import testCode from './tester';
import * as utils from './utils';
let resetTimer = () => {} let resetTimer = () => {}
@@ -29,59 +30,52 @@ export function activate(context: vscode.ExtensionContext) {
statusBarItem.text = "No active Speedrun"; statusBarItem.text = "No active Speedrun";
statusBarItem.show(); statusBarItem.show();
let timer: undefined | NodeJS.Timeout; let run: undefined | { path: string, elapsed: number, timer: NodeJS.Timeout };
resetTimer = () => { resetTimer = () => {
if (timer) { if (run) {
clearInterval(timer); clearInterval(run.timer);
run = undefined;
} }
statusBarItem.text = "No active Speedrun"; statusBarItem.text = "No active Speedrun";
}; };
resetTimer(); resetTimer();
context.subscriptions.push(vscode.commands.registerCommand('speed-code.submitSpeedRun', () => {
if (run) {
const [isValid, res] = testCode(fs.readFileSync(run.path).toString(), [
{ input: ["racecar"], expected: true },
{ input: ["hello"], expected: true },
{ input: ["madam"], expected: true },
]);
vscode.window.showInformationMessage('Time: ' + run.elapsed);
if (isValid) {
resetTimer();
}
}
}));
context.subscriptions.push(vscode.commands.registerCommand('speed-code.startSpeedRun', () => { context.subscriptions.push(vscode.commands.registerCommand('speed-code.startSpeedRun', () => {
const runFilePath = path.join(speedCodePath, 'isPalindrome.js'); const runFilePath = path.join(speedCodePath, 'isPalindrome.js');
fs.writeFileSync(runFilePath, ''); fs.writeFileSync(runFilePath, '');
openAndFocusFile(runFilePath); openAndFocusFile(runFilePath);
setTimeout(() => {
const [isValid, res] = testCode(fs.readFileSync(runFilePath).toString(), [{
input: ["racecar"],
expected: true
},{
input: ["hello"],
expected: false
},{
input: ["madam"],
expected: true
},]);
vscode.window.showInformationMessage('Speedrun info: ' + JSON.stringify(res) + 'Summary: ' + isValid);
}, 2000);
const startTime = Date.now(); const startTime = Date.now();
timer = setInterval(() => { run = {
path: runFilePath,
elapsed: 0,
timer: setInterval(() => {
if (!run) return;
const currentTime = Date.now(); const currentTime = Date.now();
const timeSeconds = (currentTime - startTime) / 1000; run.elapsed = (currentTime - startTime) / 1000;
if (timeSeconds > 86400) { statusBarItem.text = utils.format(run.elapsed);
const days = Math.floor(timeSeconds / 86400);
const hours = Math.floor(timeSeconds / 3600);
const minutes = Math.floor(timeSeconds / 60);
statusBarItem.text = `${days}:${hours - (days * 24)}:${minutes - (hours * 60)}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else if (timeSeconds >= 3600) {
const hours = Math.floor(timeSeconds / 3600);
const minutes = Math.floor(timeSeconds / 60);
statusBarItem.text = `${hours}:${minutes - (hours * 60)}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else if (timeSeconds >= 60) {
const minutes = Math.floor(timeSeconds / 60);
statusBarItem.text = `${minutes}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else {
statusBarItem.text = `${timeSeconds}`;
}
statusBarItem.color = '#fff'; statusBarItem.color = '#fff';
}, 10); }, 10),
};
vscode.window.showInformationMessage('Speedrun Started!'); vscode.window.showInformationMessage('Speedrun Started!');
}), statusBarItem); }), statusBarItem);
+17
View File
@@ -0,0 +1,17 @@
export function format(timeSeconds: number): string {
if (timeSeconds > 86400) {
const days = Math.floor(timeSeconds / 86400);
const hours = Math.floor(timeSeconds / 3600);
const minutes = Math.floor(timeSeconds / 60);
return `${days}:${hours - (days * 24)}:${minutes - (hours * 60)}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else if (timeSeconds >= 3600) {
const hours = Math.floor(timeSeconds / 3600);
const minutes = Math.floor(timeSeconds / 60);
return `${hours}:${minutes - (hours * 60)}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else if (timeSeconds >= 60) {
const minutes = Math.floor(timeSeconds / 60);
return `${minutes}:${(timeSeconds - (minutes * 60)).toFixed(3)}`;
} else {
return `${timeSeconds}`;
}
}