From 5ea8c9d506ea5c639cf5f9d810f814a43480f9af Mon Sep 17 00:00:00 2001 From: Franek Date: Fri, 24 May 2024 09:57:50 +0200 Subject: [PATCH] update command logic --- index.html | 3 +-- js/script.js | 14 ++++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index e8fe99a..0347269 100644 --- a/index.html +++ b/index.html @@ -20,8 +20,7 @@

welcome to my website

switch theme

-

press Enter to run command

-

use 'help' to list all commands

+

type 'help' to list all commands

[guest@website ~]$ diff --git a/js/script.js b/js/script.js index ba9eaf0..248511d 100644 --- a/js/script.js +++ b/js/script.js @@ -5,13 +5,13 @@ function updateAge() { } // process command function -function processCommand() { - const [commandName, ...args] = command.value.split(' ') || [command.value] +function processCommand(input) { + const [commandName, ...args] = input.split(' ') || [input] const suppliedCommand = COMMANDS.find(c => c.name === commandName) if (!suppliedCommand) { - result.textContent = 'invalid command'; - return + result.textContent = ''; + return; } const { arguments, output } = suppliedCommand; @@ -66,9 +66,7 @@ command?.addEventListener('focusout', () => { setTimeout(() => command.focus(), 0); }) -command?.addEventListener('keydown', e => { - if (e.key === 'Enter') processCommand() -}) +command?.addEventListener('input', () => processCommand(command.value)) // run all tasks if (typeof age !== 'undefined') runEvery(updateAge, 10 * 1000) @@ -82,7 +80,7 @@ if (theme === 'dark') // listen to help tooltip help.addEventListener('click', () => { command.value = 'help'; - processCommand(); + processCommand('help'); }) // ignore context menu