commmmands
This commit is contained in:
parent
5b50df3e4f
commit
51d319a8b9
|
|
@ -3,7 +3,17 @@
|
||||||
|
|
||||||
import { scrollback } from "./dom.js"
|
import { scrollback } from "./dom.js"
|
||||||
|
|
||||||
export const commands: Record<string, () => void> = {
|
export const commands: string[] = []
|
||||||
|
|
||||||
|
export const browserCommands: Record<string, () => any> = {
|
||||||
fullscreen: () => document.body.requestFullscreen(),
|
fullscreen: () => document.body.requestFullscreen(),
|
||||||
clear: () => scrollback.innerHTML = "",
|
clear: () => scrollback.innerHTML = "",
|
||||||
|
commands: () => commands.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cacheCommands(cmds: string[]) {
|
||||||
|
commands.length = 0
|
||||||
|
commands.push(...cmds)
|
||||||
|
commands.push(...Object.keys(browserCommands))
|
||||||
|
commands.sort()
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
////
|
////
|
||||||
// The shell runs on the server and processes input, returning output.
|
// The shell runs on the server and processes input, returning output.
|
||||||
|
|
||||||
|
import type { Message, CommandResult } from "../shared/types.js"
|
||||||
import { addInput, setStatus, addOutput } from "./scrollback.js"
|
import { addInput, setStatus, addOutput } from "./scrollback.js"
|
||||||
import { send } from "./websocket.js"
|
import { send } from "./websocket.js"
|
||||||
import { randomID } from "../shared/utils.js"
|
import { randomID } from "../shared/utils.js"
|
||||||
import type { Message, CommandResult } from "../shared/types.js"
|
|
||||||
import { addToHistory } from "./history.js"
|
import { addToHistory } from "./history.js"
|
||||||
import { commands } from "./commands.js"
|
import { browserCommands, cacheCommands } from "./commands.js"
|
||||||
|
|
||||||
export function runCommand(input: string) {
|
export function runCommand(input: string) {
|
||||||
const id = randomID()
|
const id = randomID()
|
||||||
|
|
@ -16,8 +16,9 @@ export function runCommand(input: string) {
|
||||||
|
|
||||||
const [cmd = "", ...args] = input.split(" ")
|
const [cmd = "", ...args] = input.split(" ")
|
||||||
|
|
||||||
if (commands[cmd]) {
|
if (browserCommands[cmd]) {
|
||||||
commands[cmd]()
|
const result = browserCommands[cmd]()
|
||||||
|
if (result) addOutput(id, result)
|
||||||
setStatus(id, "ok")
|
setStatus(id, "ok")
|
||||||
} else {
|
} else {
|
||||||
send({ id, type: "input", data: input })
|
send({ id, type: "input", data: input })
|
||||||
|
|
@ -25,16 +26,18 @@ export function runCommand(input: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// message received from server
|
// message received from server
|
||||||
export function handleMessage(data: Message) {
|
export function handleMessage(msg: Message) {
|
||||||
if (data.type === "output") {
|
if (msg.type === "output") {
|
||||||
handleOutput(data)
|
handleOutput(msg)
|
||||||
|
} else if (msg.type === "commands") {
|
||||||
|
cacheCommands(msg.data as string[])
|
||||||
} else {
|
} else {
|
||||||
console.error("unknown message type", data)
|
console.error("unknown message type", msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleOutput(msg: Message) {
|
function handleOutput(msg: Message) {
|
||||||
const result = msg.data as CommandResult
|
const result = msg.data as CommandResult
|
||||||
setStatus(msg.id, result.status)
|
setStatus(msg.id!, result.status)
|
||||||
addOutput(msg.id, result.output)
|
addOutput(msg.id!, result.output)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ app.get("/ws", upgradeWebSocket(async c => {
|
||||||
return {
|
return {
|
||||||
onOpen(_e, ws) {
|
onOpen(_e, ws) {
|
||||||
wsConnections.push(ws)
|
wsConnections.push(ws)
|
||||||
|
ws.send(JSON.stringify({ type: "commands", data: ["hello", "echo"] }))
|
||||||
},
|
},
|
||||||
async onMessage(event, ws) {
|
async onMessage(event, ws) {
|
||||||
let data: Message | undefined
|
let data: Message | undefined
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
export type Message = {
|
export type Message = {
|
||||||
session?: string
|
session?: string
|
||||||
id: string
|
id?: string
|
||||||
type: "input" | "output"
|
type: "input" | "output" | "commands"
|
||||||
data: string | CommandResult
|
data: CommandResult | string | string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CommandResult = {
|
export type CommandResult = {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user