This repository was archived by the owner on Jul 6, 2022. It is now read-only.
forked from Jaredk3nt/HomeTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
95 lines (93 loc) · 2.45 KB
/
Copy pathcli.js
File metadata and controls
95 lines (93 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Global data
let searchUrl = ENGINES.google;
let promptSymbol = "$"; // Update to change prompt
let links = {};
let position = []; // Determines where in the link tree we are currently
let commandHistory = [];
let commandHistoryCursor = -1;
let autoCompleteHistory = [];
// IIFE for setup
(() => {
const lsLinks = readLinks();
if (lsLinks) {
links = lsLinks;
}
// Set Engine
const savedEngine = readEngine();
if (savedEngine) {
searchUrl = savedEngine;
}
// Set theme
const currentTheme = readTheme();
theme([currentTheme]);
// write initial prompt
const d = new Date();
const [date, time] = d.toLocaleString().split(" ");
textWriter(
`It's ${time.slice(0, time.length - 3)} on ${date.replace(",", "")}.`
);
writePrompt();
// Setup event listener for commands
document.addEventListener("keydown", handleKeyPresses);
focusPrompt();
})();
function handleKeyPresses(e) {
if (e.keyCode === 9) {
// Tab
e.preventDefault();
e.stopPropagation();
handleAutoComplete();
}
if (e.keyCode === 13) {
// Enter
const input = document.getElementById("prompt-input");
return runCommand(input.value);
}
if (e.keyCode === 38) {
// Up
if (commandHistoryCursor === -1 && commandHistory.length) {
commandHistoryCursor = commandHistory.length - 1;
return pushCommand(commandHistory[commandHistoryCursor]);
}
if (commandHistoryCursor > 0) {
commandHistoryCursor--;
return pushCommand(commandHistory[commandHistoryCursor]);
}
}
if (e.keyCode === 40) {
// Down
if (commandHistoryCursor === commandHistory.length - 1) {
commandHistoryCursor = -1;
return pushCommand("");
}
if (
commandHistoryCursor >= 0 &&
commandHistoryCursor < commandHistory.length
) {
commandHistoryCursor++;
return pushCommand(commandHistory[commandHistoryCursor]);
}
}
}
// User Commands
function runCommand(cmd) {
commandHistory.push(cmd);
// TODO: update parse to handle flags and quotations to better handle future commands
const parsedCmd = parseCommand(cmd);
let response;
if (COMMANDS[parsedCmd[0]]) {
if (parsedCmd.length > 1 && parsedCmd[1] === "-h") {
response = COMMANDS.help.func([parsedCmd[0]]);
} else {
response = COMMANDS[parsedCmd[0]].func(
parsedCmd.slice(1, parsedCmd.length)
);
}
} else {
textWriter("Command not found.");
}
if (!response) {
replacePrompt();
}
focusPrompt();
}