1 line
No EOL
6.2 KiB
Text
1 line
No EOL
6.2 KiB
Text
{"protected":false,"mode":"f","metadata":{"name":"miss","author":"Luna Magdalena :3","version":[1],"dependencies":[],"runner":[],"paths":{"miss.js":"%B/miss.js"}},"content":"{\"miss.js\":{\"protected\":true,\"metadata\":{},\"mode\":\"f\",\"content\":\" // check if there is a config file\\nlet config = {}\\n\\nif (fs.getNode(fs.resolve(itty.env.dir_conf || \\\"/conf/\\\") + \\\"miss.conf\\\")) {\\n try {\\n const configFile = io.open(fs.resolve(itty.env.dir_conf || \\\"/conf/\\\") + \\\"miss.conf\\\")\\n config = configFile.read()\\n config = JSON.parse(config)\\n } catch (e) {\\n io.error([1, \\\"Error parsing config\\\"])\\n config = {}\\n }\\n}\\n\\nlet history = []\\nlet historyIndex = 0\\n\\nfunction onInput(key) {\\n switch (key) {\\n case \\\"%^\\\":\\n historyIndex = Math.max(historyIndex - 1, 0)\\n if (history[historyIndex]) {\\n program.read.input = history[historyIndex]\\n program.read.cursor = program.read.input.length\\n }\\n break\\n case \\\"%v\\\":\\n historyIndex = Math.min(historyIndex + 1, history.length)\\n if (history[historyIndex]) {\\n program.read.input = history[historyIndex]\\n program.read.cursor = program.read.input.length\\n } else if (historyIndex === history.length) {\\n program.read.input = \\\"\\\"\\n program.read.cursor = 0\\n }\\n break\\n }\\n}\\n\\nlet running = false\\n// this runs on every new line\\nasync function line() {\\n // promt! %D gets replaced by the active dir\\n const prompt = config.prompt || [[\\\"%D\\\", 0xc000], [\\\" > \\\", 0xd000]]\\n\\n for (let p of prompt)\\n p[0] = p[0].replace(/%D/g, program.currentDir)\\n\\n // parse arguments\\n let command = await io.read(prompt)\\n if (command !== \\\"\\\" && command !== history[history.length - 1]) {\\n history.push(command)\\n historyIndex = history.length\\n }\\n command = command.replace(/%%/g, \\\"%\\\")\\n let args = command.match(/\\\"([^\\\"%]*(%.[^\\\"%]*)*)\\\"|'([^'%]*(%.[^'%]*)*)'|\\\\S+/g)\\n\\n if (args) {\\n for (let i=0; i < args.length; i++) {\\n args[i] = args[i].replace(/^'(.*)'$|^\\\"(.*)\\\"$/, \\\"$1$2\\\")\\n args[i] = args[i].replace(/%(['\\\"])/g, \\\"$1\\\")\\n }\\n\\n // split into multiple commands\\n const commands = []\\n let redir\\n let c = []\\n for (let a of args) {\\n if (a !== \\\"|\\\") {\\n c.push(a)\\n } else {\\n commands.push(c.slice())\\n c = []\\n }\\n }\\n commands.push(c)\\n\\n let redirIndex = commands[commands.length - 1].indexOf(\\\">\\\")\\n if (redirIndex !== -1) {\\n redir = commands[commands.length - 1][redirIndex + 1]\\n commands[commands.length - 1] = commands[commands.length - 1].slice(0, redirIndex)\\n }\\n\\n running = true\\n let output = \\\"\\\"\\n for (let c = 0; c < commands.length; c++) {\\n const hidden = (c < commands.length - 1 || redir ? true : false)\\n output = await run(commands[c], output, hidden)\\n }\\n \\n if (redir) {\\n let file\\n try {\\n file = io.open(redir, \\\"a\\\")\\n } catch (e) {\\n display.print(\\\"Cannot open file\\\", 0x9000) \\n }\\n if (file) {\\n file.write(output)\\n try {\\n file.writeOut()\\n } catch (e) {\\n display.print(\\\"Cannot open file\\\", 0x9000) \\n }\\n }\\n }\\n\\n running = false\\n \\n if (itty.cursorX !== 0)\\n itty.cursorY++\\n\\n line()\\n } else {\\n line()\\n }\\n}\\n\\n// attempt to run autorun functions!\\nif (program.uid === 0) {\\n for (let a of [\\\"/autorun\\\", \\\"/autorun.js\\\", \\\"/.autorun\\\", \\\"/.autorun.js\\\"]) {\\n try {\\n exec.runProgram(a, [], \\\"\\\", false)\\n } catch {}\\n }\\n}\\n\\nline()\\n\\nasync function run(arg, input, hidden) {\\n const program = arg[0]\\n if (arg.length > 1)\\n arg.splice(0, 1)\\n else\\n arg = []\\n let file = null\\n\\n switch (program) {\\n // built in \\\"programs\\\"\\n case \\\"cd\\\":\\n try {\\n fs.changeDir(arg[0] || itty.env.home)\\n } catch (e) {\\n display.print(\\\"Not a directory\\\")\\n }\\n break\\n case \\\"exit\\\":\\n quit()\\n display.print(\\\"Cannot quit init process\\\", 0xf000)\\n return\\n break\\n // otherwise, call\\n default:\\n let output\\n try {\\n output = await exec.runProgram(program, arg, input, hidden)\\n } catch (e) {\\n if (e[0] !== 0) {\\n if (e[0] === -1 && e[1] === \\\"Cannot find program file\\\") {\\n display.print(`${program}: command not found.`, 0x2000)\\n return\\n } else\\n display.print(\\\"Error running program\\\", 0x9000)\\n }\\n }\\n\\n return output\\n }\\n}\\n\\nfunction onReturn() {\\n if (running)\\n return\\n\\n program.visible = true\\n program.focused = true\\n historyIndex = history.length\\n if (itty.cursorX !== 0)\\n itty.cursorY++\\n onError()\\n}\\n\\nfunction onError() {\\n if (program.visible) {\\n for (let e of io.getErrors()) {\\n display.write(`[${e[0]}] `, 0x9010)\\n display.print(e[1] || \\\"\\\")\\n }\\n }\\n}\\n\\nfunction onResize() {\\n if (program.focused) {\\n program.read.line = 0\\n display.clear()\\n }\\n}\"}}"} |