-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (72 loc) · 2.19 KB
/
index.js
File metadata and controls
94 lines (72 loc) · 2.19 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
(async terminal => {
const prompt = document.querySelector('.prompt')
const { CommandHistory } = await import('./lib/command-history.js')
const commandHistory = new CommandHistory()
const { elementBuilder } = await import('./lib/element.js')
const scrollback = elementBuilder('div').class('scrollback').make()
terminal.prepend(scrollback)
const { buildCommands } = await import('./lib/commands.js')
const commands = await buildCommands({terminal, scrollback, prompt})
const print = (options, ...args) => {
const { classList } = options || {}
if (typeof options !== 'object') {
args.unshift(options)
}
scrollback.appendChild(elementBuilder('p').contents(args.join(' ')).class(...(classList || [])).make())
}
const center = (...args) => {
print({ classList: ['center'] }, ...args)
}
const { messages } = await import('./data/system-messages.js')
center(messages.headerMessage)
print(messages.trespasserMessage)
print(messages.hack)
function scrollToBottom() {
terminal.scrollTop = terminal.scrollHeight
}
async function handleEntry(value) {
const args = value.split(' ')
const command = args.shift()
print(`>${value}`)
if (command && commands[command]) {
try {
print(await commands[command](...args))
} catch (e) {
debugger
}
} else {
print(`No command "${command}" found. Try "help" for a list of options.`)
}
print('\n')
scrollToBottom()
}
async function handleKey(event) {
const { key, code } = event
const textEntryKeys = '.-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 '
const handlers = {
async Enter(input) {
commandHistory.insert(input)
handleEntry(input)
return null
},
async Backspace(input) {
return input.slice(0, -1)
},
async ArrowUp() {
return commandHistory.prev()
},
async ArrowDown() {
return commandHistory.next()
}
}
if (textEntryKeys.includes(key)) {
prompt.innerHTML += key
} else if (handlers[key]) {
event.preventDefault()
event.stopImmediatePropagation()
const { innerHTML } = prompt
prompt.innerHTML = await handlers[key](innerHTML)
}
}
document.addEventListener('keydown', handleKey)
})(document.querySelector('.terminal'))