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 pathutils.js
More file actions
149 lines (135 loc) · 3.56 KB
/
Copy pathutils.js
File metadata and controls
149 lines (135 loc) · 3.56 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function safeParse(input) {
try {
return JSON.parse(input) || {};
} catch {
return {};
}
}
function locationType(val) {
if (typeof val === "string") return types.LINK;
return types.DIR;
}
function joinWriter(command, writer) {
return (input) => {
writer(command(input));
};
}
function focusPrompt() {
const input = document.getElementById("prompt-input");
input.focus();
input.setSelectionRange(input.value.length, input.value.length);
}
function getCursor(pos) {
let cursor = links;
pos.forEach((p) => {
// TODO: handle bad positions
cursor = cursor[p];
});
return cursor;
}
function getCurrentCursor() {
return getCursor(position);
}
function pushCommand(cmd) {
const prompt = document.getElementById("prompt-input");
prompt.value = cmd;
focusPrompt();
}
// Returns link url if link or cursor if directory
// Throw error if bad path
function locatePath(path) {
let cursor = locateParentPath(path);
if (path.length) {
const final = path[path.length - 1];
if (!cursor[final]) {
throw `no such link or directory: ${final}`;
}
return cursor[final];
}
return cursor;
}
function locateParentPath(fullPath) {
const path = fullPath.slice(0, fullPath.length - 1);
let cursor = getCurrentCursor();
const newPosition = [...position];
for (let i = 0; i < path.length; i++) {
const m = path[i];
if (m === "..") {
newPosition.pop();
cursor = getCursor(newPosition);
} else {
if (!cursor[m]) {
throw `no such link or directory: ${m}`;
}
if (locationType(cursor[m]) === types.LINK) {
throw `not a directory: ${m}`;
}
newPosition.push(m);
cursor = getCursor(newPosition);
}
}
return cursor;
}
// Write out functions
function writePrompt() {
const terminal = document.getElementById("terminal-content");
const prompt = document.createElement("div");
prompt.classList.add("prompt");
prompt.innerHTML = `
<p class='prompt-title'>~${position.length ? "/" : ""}${position.join(
"/"
)}<span class='prompt-cursor'>${promptSymbol}</span></p>
<input id='prompt-input' type='text' />
`;
terminal.appendChild(prompt);
}
function replacePrompt() {
const oldPrompt = document.getElementById("prompt-input");
const value = oldPrompt.value;
const promptText = document.createElement("p");
promptText.innerText = value;
promptText.classList.add("prompt-text");
oldPrompt.replaceWith(promptText);
writePrompt();
}
// Parse command input by keeping strings in "" together as an single item
function parseCommand(input) {
const re = /"([^"]+)"|([^\s]+)/g;
const parsedCmd = [];
let temp;
while ((temp = re.exec(input)) !== null) {
const val = temp[1] || temp[2]; // Get the correct capture group
parsedCmd.push(val);
}
return parsedCmd;
}
// Parse command array to extract flags
function extractFlags(command, flagMap = {}) {
const finalCommand = [];
const flags = {};
for (let i = 0; i < command.length; i++) {
const arg = command[i];
const isFlag = /^(-|--)(\w+)/.exec(arg);
if (isFlag) {
const flag = isFlag[2];
// If flag marked boolean, don't capture input
// TODO: throw error if not found in map?
if (flagMap[flag] !== "boolean") {
flags[flag] = command[i + 1];
i++;
} else {
flags[flag] = true;
}
} else {
finalCommand.push(arg);
}
}
return { command: finalCommand, flags };
}
function formaturl(url) {
let finalUrl = url;
if (!/^http|https:\/\//.test(finalUrl)) {
finalUrl = "https://" + finalUrl;
}
return finalUrl;
}