update command logic
This commit is contained in:
parent
e5892803a9
commit
5ea8c9d506
@ -20,8 +20,7 @@
|
|||||||
<h1>welcome to my website</h1>
|
<h1>welcome to my website</h1>
|
||||||
<h2 id="switch_theme">switch theme</h2>
|
<h2 id="switch_theme">switch theme</h2>
|
||||||
|
|
||||||
<p>press <kbd>Enter</kbd> to run command</p>
|
<p>type <span id="help">'help'</span> to list all commands</p>
|
||||||
<p>use <span id="help">'help'</span> to list all commands</p>
|
|
||||||
<span>[guest@website ~]$ </span>
|
<span>[guest@website ~]$ </span>
|
||||||
<input type="text" id="command">
|
<input type="text" id="command">
|
||||||
</div>
|
</div>
|
||||||
|
14
js/script.js
14
js/script.js
@ -5,13 +5,13 @@ function updateAge() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// process command function
|
// process command function
|
||||||
function processCommand() {
|
function processCommand(input) {
|
||||||
const [commandName, ...args] = command.value.split(' ') || [command.value]
|
const [commandName, ...args] = input.split(' ') || [input]
|
||||||
const suppliedCommand = COMMANDS.find(c => c.name === commandName)
|
const suppliedCommand = COMMANDS.find(c => c.name === commandName)
|
||||||
|
|
||||||
if (!suppliedCommand) {
|
if (!suppliedCommand) {
|
||||||
result.textContent = 'invalid command';
|
result.textContent = '';
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { arguments, output } = suppliedCommand;
|
const { arguments, output } = suppliedCommand;
|
||||||
@ -66,9 +66,7 @@ command?.addEventListener('focusout', () => {
|
|||||||
setTimeout(() => command.focus(), 0);
|
setTimeout(() => command.focus(), 0);
|
||||||
})
|
})
|
||||||
|
|
||||||
command?.addEventListener('keydown', e => {
|
command?.addEventListener('input', () => processCommand(command.value))
|
||||||
if (e.key === 'Enter') processCommand()
|
|
||||||
})
|
|
||||||
|
|
||||||
// run all tasks
|
// run all tasks
|
||||||
if (typeof age !== 'undefined') runEvery(updateAge, 10 * 1000)
|
if (typeof age !== 'undefined') runEvery(updateAge, 10 * 1000)
|
||||||
@ -82,7 +80,7 @@ if (theme === 'dark')
|
|||||||
// listen to help tooltip
|
// listen to help tooltip
|
||||||
help.addEventListener('click', () => {
|
help.addEventListener('click', () => {
|
||||||
command.value = 'help';
|
command.value = 'help';
|
||||||
processCommand();
|
processCommand('help');
|
||||||
})
|
})
|
||||||
|
|
||||||
// ignore context menu
|
// ignore context menu
|
||||||
|
Loading…
Reference in New Issue
Block a user