From ec071c3f6d24f64d3174eb71045ddde36e1a4dc4 Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Sun, 20 Dec 2015 11:27:04 +0300 Subject: [PATCH] aefef --- MineOS/Test.pkg | 3047 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 3047 insertions(+) create mode 100644 MineOS/Test.pkg diff --git a/MineOS/Test.pkg b/MineOS/Test.pkg new file mode 100644 index 00000000..ba17f237 --- /dev/null +++ b/MineOS/Test.pkg @@ -0,0 +1,3047 @@ +--@luaPackageFileSignature +--@luaPackageFileSeparator +--@address.lua +local computer = require("computer") + +io.write(computer.address()) +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@alias.lua +local shell = require("shell") + +local args = shell.parse(...) + +if #args == 0 then + for name, value in shell.aliases() do + io.write(name .. " " .. value .. "\n") + end +elseif #args == 1 then + local value = shell.getAlias(args[1]) + if value then + io.write(value) + else + io.stderr:write("no such alias") + end +else + shell.setAlias(args[1], args[2]) + io.write("alias created: " .. args[1] .. " -> " .. args[2]) +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@archive.lua +local archive = require("lib/archive") +local shell = require("shell") + +------------------------------------------------------------------------------------------------------------------------------------ + +local args = {...} + +archive.debugMode = true + +if args[1] == "pack" then + if not args[2] or not args[3] then + print(" ") + print("Использование: archive pack <имя архива> <архивируемая папка>") + print(" ") + return + end + archive.pack(args[2], args[3]) +elseif args[1] == "unpack" then + if not args[2] or not args[3] then + print(" ") + print("Использование: archive unpack <путь к архиву> <папка для сохранения файлов>") + print(" ") + return + end + archive.unpack(args[2], args[3]) +elseif args[1] == "download" then + if not args[2] or not args[3] then + print(" ") + print("Использование: archive download <папка для сохранения файлов>") + print(" ") + return + end + print(" ") + print("Загрузка файла по ссылке \"" .. args[2] .. "\"") + shell.execute("wget " .. args[2] .. "TempFile.pkg -fq") + archive.unpack("TempFile.pkg", args[3]) + shell.execute("rm TempFile.pkg") +else + print(" ") + print("Использование: archive ...") + print(" ") + return +end + +archive.debugMode = false + +------------------------------------------------------------------------------------------------------------------------------------ +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@cat.lua +local shell = require("shell") + +local args = shell.parse(...) +if #args == 0 then + repeat + local read = io.read("*L") + if read then + io.write(read) + end + until not read +else + for i = 1, #args do + local file, reason = io.open(shell.resolve(args[i])) + if not file then + io.stderr:write(tostring(reason) .. "\n") + os.exit(false) + end + repeat + local line = file:read("*L") + if line then + io.write(line) + end + until not line + file:close() + end +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@cd.lua +local shell = require("shell") + +local args = shell.parse(...) +if #args == 0 then + io.write("Usage: cd ") +else + local result, reason = shell.setWorkingDirectory(shell.resolve(args[1])) + if not result then + io.stderr:write(reason) + end +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@clear.lua +local ecs = require("ECSAPI") +ecs.prepareToExit() +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@components.lua +local component = require("component") +local shell = require("shell") +local text = require("text") + +local args, options = shell.parse(...) +local count = tonumber(options.limit) or math.huge + +local components = {} +local padTo = 1 + +if #args == 0 then -- get all components if no filters given. + args[1] = "" +end +for _, filter in ipairs(args) do + for address, name in component.list(filter) do + if name:len() > padTo then + padTo = name:len() + 2 + end + components[address] = name + end +end + +padTo = padTo + 8 - padTo % 8 +for address, name in pairs(components) do + io.write(text.padRight(name, padTo) .. address .. '\n') + + if options.l then + local proxy = component.proxy(address) + local padTo = 1 + local methods = {} + for name, member in pairs(proxy) do + if type(member) == "table" or type(member) == "function" then + if name:len() > padTo then + padTo = name:len() + 2 + end + table.insert(methods, name) + end + end + table.sort(methods) + padTo = padTo + 8 - padTo % 8 + + for _, name in ipairs(methods) do + local doc = tostring(proxy[name]) + io.write(" " .. text.padRight(name, padTo) .. doc .. '\n') + end + end + + count = count - 1 + if count <= 0 then + break + end +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@cp.lua +local fs = require("filesystem") +local shell = require("shell") + +local args, options = shell.parse(...) +if #args < 2 then + io.write("Usage: cp [-inrv] \n") + io.write(" -i: prompt before overwrite (overrides -n option).\n") + io.write(" -n: do not overwrite an existing file.\n") + io.write(" -r: copy directories recursively.\n") + io.write(" -u: copy only when the SOURCE file differs from the destination\n") + io.write(" file or when the destination file is missing.\n") + io.write(" -v: verbose output.\n") + io.write(" -x: stay on original source file system.") + return +end + +local from = {} +for i = 1, #args - 1 do + table.insert(from, shell.resolve(args[i])) +end +local to = shell.resolve(args[#args]) + +local function status(from, to) + if options.v then + io.write(from .. " -> " .. to .. "\n") + end + os.sleep(0) -- allow interrupting +end + +local result, reason + +local function prompt(message) + io.write(message .. " [Y/n]\n") + local result = io.read() + return result and (result == "" or result:sub(1, 1):lower() == "y") +end + +local function areEqual(path1, path2) + local f1 = io.open(path1, "rb") + if not f1 then + return nil, "could not open `" .. path1 .. "' for update test" + end + local f2 = io.open(path2, "rb") + if not f2 then + f1:close() + return nil, "could not open `" .. path2 .. "' for update test" + end + local result = true + local chunkSize = 4 * 1024 + repeat + local s1, s2 = f1:read(chunkSize), f2:read(chunkSize) + if s1 ~= s2 then + result = false + break + end + until not s1 or not s2 + f1:close() + f2:close() + return result +end + +local function isMount(path) + path = fs.canonical(path) + for _, mountPath in fs.mounts() do + if path == fs.canonical(mountPath) then + return true + end + end +end + +local function recurse(fromPath, toPath) + status(fromPath, toPath) + if fs.isDirectory(fromPath) then + if not options.r then + io.write("omitting directory `" .. fromPath .. "'\n") + return true + end + if fs.canonical(fromPath) == fs.canonical(fs.path(toPath)) then + return nil, "cannot copy a directory, `" .. fromPath .. "', into itself, `" .. toPath .. "'\n" + end + if fs.exists(toPath) and not fs.isDirectory(toPath) then + -- my real cp always does this, even with -f, -n or -i. + return nil, "cannot overwrite non-directory `" .. toPath .. "' with directory `" .. fromPath .. "'" + end + if options.x and isMount(fromPath) then + return true + end + fs.makeDirectory(toPath) + for file in fs.list(fromPath) do + local result, reason = recurse(fs.concat(fromPath, file), fs.concat(toPath, file)) + if not result then + return nil, reason + end + end + return true + else + if fs.exists(toPath) then + if fs.canonical(fromPath) == fs.canonical(toPath) then + return nil, "`" .. fromPath .. "' and `" .. toPath .. "' are the same file" + end + if fs.isDirectory(toPath) then + if options.i then + if not prompt("overwrite `" .. toPath .. "'?") then + return true + end + elseif options.n then + return true + else -- yes, even for -f + return nil, "cannot overwrite directory `" .. toPath .. "' with non-directory" + end + else + if options.u then + if areEqual(fromPath, toPath) then + return true + end + end + if options.i then + if not prompt("overwrite `" .. toPath .. "'?") then + return true + end + elseif options.n then + return true + end + -- else: default to overwriting + end + fs.remove(toPath) + end + return fs.copy(fromPath, toPath) + end +end +for _, fromPath in ipairs(from) do + local toPath = to + if fs.isDirectory(toPath) then + toPath = fs.concat(toPath, fs.name(fromPath)) + end + result, reason = recurse(fromPath, toPath) + if not result then + error(reason, 0) + end +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@date.lua +io.write(os.date("%F %T")) +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@df.lua +local fs = require("filesystem") +local shell = require("shell") +local text = require("text") + +local args, options = shell.parse(...) + +local function formatSize(size) + if not options.h then + return tostring(size) + end + local sizes = {"", "K", "M", "G"} + local unit = 1 + local power = options.si and 1000 or 1024 + while size > power and unit < #sizes do + unit = unit + 1 + size = size / power + end + return math.floor(size * 10) / 10 .. sizes[unit] +end + +local mounts = {} +if #args == 0 then + for proxy, path in fs.mounts() do + mounts[path] = proxy + end +else + for i = 1, #args do + local proxy, path = fs.get(args[i]) + if not proxy then + io.stderr:write(args[i], ": no such file or directory\n") + else + mounts[path] = proxy + end + end +end + +local result = {{"Filesystem", "Used", "Available", "Use%", "Mounted on"}} +for path, proxy in pairs(mounts) do + local label = proxy.getLabel() or proxy.address + local used, total = proxy.spaceUsed(), proxy.spaceTotal() + local available, percent + if total == math.huge then + used = used or "N/A" + available = "unlimited" + percent = "0%" + else + available = total - used + percent = used / total + if percent ~= percent then -- NaN + available = "N/A" + percent = "N/A" + else + percent = math.ceil(percent * 100) .. "%" + end + end + table.insert(result, {label, formatSize(used), formatSize(available), tostring(percent), path}) +end + +local m = {} +for _, row in ipairs(result) do + for col, value in ipairs(row) do + m[col] = math.max(m[col] or 1, value:len()) + end +end + +for _, row in ipairs(result) do + for col, value in ipairs(row) do + io.write(text.padRight(value, m[col] + 2)) + end + io.write("\n") +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@dmesg.lua +local event = require "event" +local component = require "component" +local keyboard = require "keyboard" + +local args = {...} + +local interactive = io.output() == io.stdout +local color, isPal, evt +if interactive then + color, isPal = component.gpu.getForeground() +end +io.write("Press 'Ctrl-C' to exit\n") +pcall(function() + repeat + if #args > 0 then + evt = table.pack(event.pullMultiple("interrupted", table.unpack(args))) + else + evt = table.pack(event.pull()) + end + if interactive then component.gpu.setForeground(0xCC2200) end + io.write("[" .. os.date("%T") .. "] ") + if interactive then component.gpu.setForeground(0x44CC00) end + io.write(tostring(evt[1]) .. string.rep(" ", math.max(10 - #tostring(evt[1]), 0) + 1)) + if interactive then component.gpu.setForeground(0xB0B00F) end + io.write(tostring(evt[2]) .. string.rep(" ", 37 - #tostring(evt[2]))) + if interactive then component.gpu.setForeground(0xFFFFFF) end + if evt.n > 2 then + for i = 3, evt.n do + io.write(" " .. tostring(evt[i])) + end + end + + io.write("\n") + until evt[1] == "interrupted" +end) +if interactive then + component.gpu.setForeground(color, isPal) +end + +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@echo.lua +local args = table.pack(...) +for i = 1, #args do + if i > 1 then + io.write(" ") + end + io.write(args[i]) +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@edit.lua +local component = require("component") +local event = require("event") +local fs = require("filesystem") +local keyboard = require("keyboard") +local shell = require("shell") +local term = require("term") +local text = require("text") +local unicode = require("unicode") + +if not term.isAvailable() then + return +end + +local args, options = shell.parse(...) +if #args == 0 then + io.write("Usage: edit ") + return +end + +local filename = shell.resolve(args[1]) + +local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly() + +if not fs.exists(filename) then + if fs.isDirectory(filename) then + io.stderr:write("file is a directory") + return + elseif readonly then + io.stderr:write("file system is read only") + return + end +end + +local function loadConfig() + -- Try to load user settings. + local env = {} + local config = loadfile("/etc/edit.cfg", nil, env) + if config then + pcall(config) + end + -- Fill in defaults. + env.keybinds = env.keybinds or { + left = {{"left"}}, + right = {{"right"}}, + up = {{"up"}}, + down = {{"down"}}, + home = {{"home"}}, + eol = {{"end"}}, + pageUp = {{"pageUp"}}, + pageDown = {{"pageDown"}}, + + backspace = {{"back"}}, + delete = {{"delete"}}, + deleteLine = {{"control", "delete"}, {"shift", "delete"}}, + newline = {{"enter"}}, + + save = {{"control", "s"}}, + close = {{"control", "w"}}, + find = {{"control", "f"}}, + findnext = {{"control", "g"}, {"control", "n"}, {"f3"}} + } + -- Generate config file if it didn't exist. + if not config then + local root = fs.get("/") + if root and not root.isReadOnly() then + fs.makeDirectory("/etc") + local f = io.open("/etc/edit.cfg", "w") + if f then + local serialization = require("serialization") + for k, v in pairs(env) do + f:write(k.."="..tostring(serialization.serialize(v, math.huge)).."\n") + end + f:close() + end + end + end + return env +end + +term.clear() +term.setCursorBlink(true) + +local running = true +local buffer = {} +local scrollX, scrollY = 0, 0 +local config = loadConfig() + +local getKeyBindHandler -- forward declaration for refind() + +local function helpStatusText() + local function prettifyKeybind(label, command) + local keybind = type(config.keybinds) == "table" and config.keybinds[command] + if type(keybind) ~= "table" or type(keybind[1]) ~= "table" then return "" end + local alt, control, shift, key + for _, value in ipairs(keybind[1]) do + if value == "alt" then alt = true + elseif value == "control" then control = true + elseif value == "shift" then shift = true + else key = value end + end + if not key then return "" end + return label .. ": [" .. + (control and "Ctrl+" or "") .. + (alt and "Alt+" or "") .. + (shift and "Shift+" or "") .. + unicode.upper(key) .. + "] " + end + return prettifyKeybind("Save", "save") .. + prettifyKeybind("Close", "close") .. + prettifyKeybind("Find", "find") +end + +------------------------------------------------------------------------------- + +local function setStatus(value) + local w, h = component.gpu.getResolution() + component.gpu.set(1, h, text.padRight(unicode.sub(value, 1, w - 10), w - 10)) +end + +local function getSize() + local w, h = component.gpu.getResolution() + return w, h - 1 +end + +local function getCursor() + local cx, cy = term.getCursor() + return cx + scrollX, cy + scrollY +end + +local function line() + local cbx, cby = getCursor() + return buffer[cby] +end + +local function setCursor(nbx, nby) + local w, h = getSize() + nby = math.max(1, math.min(#buffer, nby)) + + local ncy = nby - scrollY + if ncy > h then + term.setCursorBlink(false) + local sy = nby - h + local dy = math.abs(scrollY - sy) + scrollY = sy + component.gpu.copy(1, 1 + dy, w, h - dy, 0, -dy) + for by = nby - (dy - 1), nby do + local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w) + component.gpu.set(1, by - scrollY, str) + end + elseif ncy < 1 then + term.setCursorBlink(false) + local sy = nby - 1 + local dy = math.abs(scrollY - sy) + scrollY = sy + component.gpu.copy(1, 1, w, h - dy, 0, dy) + for by = nby, nby + (dy - 1) do + local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w) + component.gpu.set(1, by - scrollY, str) + end + end + term.setCursor(term.getCursor(), nby - scrollY) + + nbx = math.max(1, math.min(unicode.len(line()) + 1, nbx)) + local ncx = nbx - scrollX + if ncx > w then + term.setCursorBlink(false) + local sx = nbx - w + local dx = math.abs(scrollX - sx) + scrollX = sx + component.gpu.copy(1 + dx, 1, w - dx, h, -dx, 0) + for by = 1 + scrollY, math.min(h + scrollY, #buffer) do + local str = unicode.sub(buffer[by], nbx - (dx - 1), nbx) + str = text.padRight(str, dx) + component.gpu.set(1 + (w - dx), by - scrollY, str) + end + elseif ncx < 1 then + term.setCursorBlink(false) + local sx = nbx - 1 + local dx = math.abs(scrollX - sx) + scrollX = sx + component.gpu.copy(1, 1, w - dx, h, dx, 0) + for by = 1 + scrollY, math.min(h + scrollY, #buffer) do + local str = unicode.sub(buffer[by], nbx, nbx + dx) + --str = text.padRight(str, dx) + component.gpu.set(1, by - scrollY, str) + end + end + term.setCursor(nbx - scrollX, nby - scrollY) + + component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10)) +end + +local function highlight(bx, by, length, enabled) + local w, h = getSize() + local cx, cy = bx - scrollX, by - scrollY + cx = math.max(1, math.min(w, cx)) + cy = math.max(1, math.min(h, cy)) + length = math.max(1, math.min(w - cx, length)) + + local fg, fgp = component.gpu.getForeground() + local bg, bgp = component.gpu.getBackground() + if enabled then + component.gpu.setForeground(bg, bgp) + component.gpu.setBackground(fg, fgp) + end + local value = "" + for x = cx, cx + length - 1 do + value = value .. component.gpu.get(x, cy) + end + component.gpu.set(cx, cy, value) + if enabled then + component.gpu.setForeground(fg, fgp) + component.gpu.setBackground(bg, bgp) + end +end + +local function home() + local cbx, cby = getCursor() + setCursor(1, cby) +end + +local function ende() + local cbx, cby = getCursor() + setCursor(unicode.len(line()) + 1, cby) +end + +local function left() + local cbx, cby = getCursor() + if cbx > 1 then + setCursor(cbx - 1, cby) + return true -- for backspace + elseif cby > 1 then + setCursor(cbx, cby - 1) + ende() + return true -- again, for backspace + end +end + +local function right(n) + n = n or 1 + local cbx, cby = getCursor() + local be = unicode.len(line()) + 1 + if cbx < be then + setCursor(cbx + n, cby) + elseif cby < #buffer then + setCursor(1, cby + 1) + end +end + +local function up(n) + n = n or 1 + local cbx, cby = getCursor() + if cby > 1 then + setCursor(cbx, cby - n) + if getCursor() > unicode.len(line()) then + ende() + end + end +end + +local function down(n) + n = n or 1 + local cbx, cby = getCursor() + if cby < #buffer then + setCursor(cbx, cby + n) + if getCursor() > unicode.len(line()) then + ende() + end + end +end + +local function delete(fullRow) + local cx, cy = term.getCursor() + local cbx, cby = getCursor() + local w, h = getSize() + local function deleteRow(row) + local content = table.remove(buffer, row) + local rcy = cy + (row - cby) + if rcy <= h then + component.gpu.copy(1, rcy + 1, w, h - rcy, 0, -1) + component.gpu.set(1, h, text.padRight(buffer[row + (h - rcy)], w)) + end + return content + end + if fullRow then + term.setCursorBlink(false) + if #buffer > 1 then + deleteRow(cby) + else + buffer[cby] = "" + component.gpu.fill(1, cy, w, 1, " ") + end + setCursor(1, cby) + elseif cbx <= unicode.len(line()) then + term.setCursorBlink(false) + buffer[cby] = unicode.sub(line(), 1, cbx - 1) .. + unicode.sub(line(), cbx + 1) + component.gpu.copy(cx + 1, cy, w - cx, 1, -1, 0) + local br = cbx + (w - cx) + local char = unicode.sub(line(), br, br) + if not char or unicode.len(char) == 0 then + char = " " + end + component.gpu.set(w, cy, char) + elseif cby < #buffer then + term.setCursorBlink(false) + local append = deleteRow(cby + 1) + buffer[cby] = buffer[cby] .. append + component.gpu.set(cx, cy, append) + else + return + end + setStatus(helpStatusText()) +end + +local function insert(value) + if not value or unicode.len(value) < 1 then + return + end + term.setCursorBlink(false) + local cx, cy = term.getCursor() + local cbx, cby = getCursor() + local w, h = getSize() + buffer[cby] = unicode.sub(line(), 1, cbx - 1) .. + value .. + unicode.sub(line(), cbx) + local len = unicode.len(value) + local n = w - (cx - 1) - len + if n > 0 then + component.gpu.copy(cx, cy, n, 1, len, 0) + end + component.gpu.set(cx, cy, value) + right(len) + setStatus(helpStatusText()) +end + +local function enter() + term.setCursorBlink(false) + local cx, cy = term.getCursor() + local cbx, cby = getCursor() + local w, h = getSize() + table.insert(buffer, cby + 1, unicode.sub(buffer[cby], cbx)) + buffer[cby] = unicode.sub(buffer[cby], 1, cbx - 1) + component.gpu.fill(cx, cy, w - (cx - 1), 1, " ") + if cy < h then + if cy < h - 1 then + component.gpu.copy(1, cy + 1, w, h - (cy + 1), 0, 1) + end + component.gpu.set(1, cy + 1, text.padRight(buffer[cby + 1], w)) + end + setCursor(1, cby + 1) + setStatus(helpStatusText()) +end + +local findText = "" + +local function find() + local w, h = getSize() + local cx, cy = term.getCursor() + local cbx, cby = getCursor() + local ibx, iby = cbx, cby + while running do + if unicode.len(findText) > 0 then + local sx, sy + for syo = 1, #buffer do -- iterate lines with wraparound + sy = (iby + syo - 1 + #buffer - 1) % #buffer + 1 + sx = string.find(buffer[sy], findText, syo == 1 and ibx or 1, true) + if sx and (sx >= ibx or syo > 1) then + break + end + end + if not sx then -- special case for single matches + sy = iby + sx = string.find(buffer[sy], findText, nil, true) + end + if sx then + cbx, cby = sx, sy + setCursor(cbx, cby) + highlight(cbx, cby, unicode.len(findText), true) + end + end + term.setCursor(7 + unicode.len(findText), h + 1) + setStatus("Find: " .. findText) + + local _, _, char, code = event.pull("key_down") + local handler, name = getKeyBindHandler(code) + highlight(cbx, cby, unicode.len(findText), false) + if name == "newline" then + break + elseif name == "close" then + handler() + elseif name == "backspace" then + findText = unicode.sub(findText, 1, -2) + elseif name == "find" or name == "findnext" then + ibx = cbx + 1 + iby = cby + elseif not keyboard.isControl(char) then + findText = findText .. unicode.char(char) + end + end + setCursor(cbx, cby) + setStatus(helpStatusText()) +end + +------------------------------------------------------------------------------- + +local keyBindHandlers = { + left = left, + right = right, + up = up, + down = down, + home = home, + eol = ende, + pageUp = function() + local w, h = getSize() + up(h - 1) + end, + pageDown = function() + local w, h = getSize() + down(h - 1) + end, + + backspace = function() + if not readonly and left() then + delete() + end + end, + delete = function() + if not readonly then + delete() + end + end, + deleteLine = function() + if not readonly then + delete(true) + end + end, + newline = function() + if not readonly then + enter() + end + end, + + save = function() + if readonly then return end + local new = not fs.exists(filename) + local backup + if not new then + backup = filename .. "~" + for i = 1, math.huge do + if not fs.exists(backup) then + break + end + backup = filename .. "~" .. i + end + fs.copy(filename, backup) + end + local f, reason = io.open(filename, "w") + if f then + local chars, firstLine = 0, true + for _, line in ipairs(buffer) do + if not firstLine then + line = "\n" .. line + end + firstLine = false + f:write(line) + chars = chars + unicode.len(line) + end + f:close() + local format + if new then + format = [["%s" [New] %dL,%dC written]] + else + format = [["%s" %dL,%dC written]] + end + setStatus(string.format(format, fs.name(filename), #buffer, chars)) + else + setStatus(reason) + end + if not new then + fs.remove(backup) + end + end, + close = function() + -- TODO ask to save if changed + running = false + end, + find = function() + findText = "" + find() + end, + findnext = find +} + +getKeyBindHandler = function(code) + if type(config.keybinds) ~= "table" then return end + -- Look for matches, prefer more 'precise' keybinds, e.g. prefer + -- ctrl+del over del. + local result, resultName, resultWeight = nil, nil, 0 + for command, keybinds in pairs(config.keybinds) do + if type(keybinds) == "table" and keyBindHandlers[command] then + for _, keybind in ipairs(keybinds) do + if type(keybind) == "table" then + local alt, control, shift, key + for _, value in ipairs(keybind) do + if value == "alt" then alt = true + elseif value == "control" then control = true + elseif value == "shift" then shift = true + else key = value end + end + if (not alt or keyboard.isAltDown()) and + (not control or keyboard.isControlDown()) and + (not shift or keyboard.isShiftDown()) and + code == keyboard.keys[key] and + #keybind > resultWeight + then + resultWeight = #keybind + resultName = command + result = keyBindHandlers[command] + end + end + end + end + end + return result, resultName +end + +------------------------------------------------------------------------------- + +local function onKeyDown(char, code) + local handler = getKeyBindHandler(code) + if handler then + handler() + elseif readonly and code == keyboard.keys.q then + running = false + elseif not readonly then + if not keyboard.isControl(char) then + insert(unicode.char(char)) + elseif unicode.char(char) == "\t" then + insert(" ") + end + end +end + +local function onClipboard(value) + value = value:gsub("\r\n", "\n") + local cbx, cby = getCursor() + local start = 1 + local l = value:find("\n", 1, true) + if l then + repeat + local line = string.sub(value, start, l - 1) + line = text.detab(line, 2) + insert(line) + enter() + start = l + 1 + l = value:find("\n", start, true) + until not l + end + insert(string.sub(value, start)) +end + +local function onClick(x, y) + setCursor(x + scrollX, y + scrollY) +end + +local function onScroll(direction) + local cbx, cby = getCursor() + setCursor(cbx, cby - direction * 12) +end + +------------------------------------------------------------------------------- + +do + local f = io.open(filename) + if f then + local w, h = getSize() + local chars = 0 + for line in f:lines() do + if line:sub(-1) == "\r" then + line = line:sub(1, -2) + end + table.insert(buffer, line) + chars = chars + unicode.len(line) + if #buffer <= h then + component.gpu.set(1, #buffer, line) + end + end + f:close() + if #buffer == 0 then + table.insert(buffer, "") + end + local format + if readonly then + format = [["%s" [readonly] %dL,%dC]] + else + format = [["%s" %dL,%dC]] + end + setStatus(string.format(format, fs.name(filename), #buffer, chars)) + else + table.insert(buffer, "") + setStatus(string.format([["%s" [New File] ]], fs.name(filename))) + end + setCursor(1, 1) +end + +while running do + local event, address, arg1, arg2, arg3 = event.pull() + if type(address) == "string" and component.isPrimary(address) then + local blink = true + if event == "key_down" then + onKeyDown(arg1, arg2) + elseif event == "clipboard" and not readonly then + onClipboard(arg1) + elseif event == "touch" or event == "drag" then + onClick(arg1, arg2) + elseif event == "scroll" then + onScroll(arg3) + else + blink = false + end + if blink then + term.setCursorBlink(true) + term.setCursorBlink(true) -- force toggle to caret + end + end +end + +term.clear() +term.setCursorBlink(false) +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@event.lua +local event = require "event" + +while true do + local cyka = {event.pull()} + print("Ивент: "..cyka[1]) + for i=2,#cyka do + print("Аргумент "..(i).." = "..cyka[i]) + end + print(" ") +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@flash.lua +local component = require("component") +local shell = require("shell") +local fs = require("filesystem") + +local args, options = shell.parse(...) + +if #args < 1 and not options.l then + io.write("Usage: flash [-qlr] [] [label]\n") + io.write(" q: quiet mode, don't ask questions.\n") + io.write(" l: print current contents of installed EEPROM.\n") + io.write(" r: save the current contents of installed EEPROM to file.") + return +end + +local function printRom() + local eeprom = component.eeprom + io.write(eeprom.get()) +end + +local function readRom() + local eeprom = component.eeprom + fileName = shell.resolve(args[1]) + if not options.q then + if fs.exists(fileName) then + io.write("Are you sure you want to overwrite " .. fileName .. "?\n") + io.write("Type `y` to confirm.\n") + repeat + local response = io.read() + until response and response:lower():sub(1, 1) == "y" + end + io.write("Reading EEPROM " .. eeprom.address .. ".\n" ) + end + local bios = eeprom.get() + local file = assert(io.open(fileName, "wb")) + file:write(bios) + file:close() + if not options.q then + io.write("All done!\nThe label is '" .. eeprom.getLabel() .. "'.\n") + end +end + +local function writeRom() + local file = assert(io.open(args[1], "rb")) + local bios = file:read("*a") + file:close() + + if not options.q then + io.write("Insert the EEPROM you would like to flash.\n") + io.write("When ready to write, type `y` to confirm.\n") + repeat + local response = io.read() + until response and response:lower():sub(1, 1) == "y" + io.write("Beginning to flash EEPROM.\n") + end + + local eeprom = component.eeprom + + if not options.q then + io.write("Flashing EEPROM " .. eeprom.address .. ".\n") + io.write("Please do NOT power down or restart your computer during this operation!\n") + end + + eeprom.set(bios) + + local label = args[2] + if not options.q and not label then + io.write("Enter new label for this EEPROM. Leave input blank to leave the label unchanged.\n") + label = io.read() + end + if label and #label > 0 then + eeprom.setLabel(label) + if not options.q then + io.write("Set label to '" .. eeprom.getLabel() .. "'.\n") + end + end + + if not options.q then + io.write("All done! You can remove the EEPROM and re-insert the previous one now.\n") + end +end + +if options.l then + printRom() +elseif options.r then + readRom() +else + writeRom() +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@hostname.lua +local args = {...} +if args[1] then + local file, reason = io.open("/etc/hostname", "w") + if not file then + io.stderr:write(reason .. "\n") + else + file:write(args[1]) + file:close() + os.setenv("HOSTNAME", args[1]) + os.setenv("PS1", "$HOSTNAME:$PWD# ") + end +else + local file = io.open("/etc/hostname") + if file then + io.write(file:read("*l"), "\n") + file:close() + else + io.stderr:write("Hostname not set\n") + end +end +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@install.lua +local component = require("component") +local computer = require("computer") +local event = require("event") +local filesystem = require("filesystem") +local unicode = require("unicode") +local shell = require("shell") + +local args, options = shell.parse(...) + +local fromAddress = options.from and component.get(options.from) or filesystem.get(os.getenv("_")).address +local candidates = {} +for address in component.list("filesystem", true) do + local dev = component.proxy(address) + if not dev.isReadOnly() and dev.address ~= computer.tmpAddress() and dev.address ~= fromAddress then + table.insert(candidates, dev) + end +end + +if #candidates == 0 then + io.write("No writable disks found, aborting.\n") + os.exit() +end + +for i = 1, #candidates do + local label = candidates[i].getLabel() + if label then + label = label .. " (" .. candidates[i].address:sub(1, 8) .. "...)" + else + label = candidates[i].address + end + io.write(i .. ") " .. label .. "\n") +end + +io.write("To select the device to install to, please enter a number between 1 and " .. #candidates .. ".\n") +io.write("Press 'q' to cancel the installation.\n") +local choice +while not choice do + result = io.read() + if result:sub(1, 1):lower() == "q" then + os.exit() + end + local number = tonumber(result) + if number and number > 0 and number <= #candidates then + choice = candidates[number] + else + io.write("Invalid input, please try again.\n") + end +end + +local function findMount(address) + for fs, path in filesystem.mounts() do + if fs.address == component.get(address) then + return path + end + end +end + +local name = options.name or "OpenOS" +io.write("Installing " .. name .." to device " .. (choice.getLabel() or choice.address) .. "\n") +os.sleep(0.25) +local cpPath = filesystem.concat(findMount(filesystem.get(os.getenv("_")).address), "bin/cp") +local cpOptions = "-vrx" .. (options.u and "ui " or "") +local cpSource = filesystem.concat(findMount(fromAddress), options.fromDir or "/", "*") +local cpDest = findMount(choice.address) .. "/" +local result, reason = os.execute(cpPath .. " " .. cpOptions .. " " .. cpSource .. " " .. cpDest) +if not result then + error(reason, 0) +end +if not options.nolabelset then pcall(choice.setLabel, name) end + +if not options.noreboot then + io.write("All done! " .. ((not options.noboot) and "Set as boot device and r" or "R") .. "eboot now? [Y/n]\n") + local result = io.read() + if not result or result == "" or result:sub(1, 1):lower() == "y" then + if not options.noboot then computer.setBootAddress(choice.address)end + io.write("\nRebooting now!\n") + computer.shutdown(true) + end +end +io.write("Returning to shell.\n") +--@luaPackageFileEnd +--@luaPackageFileSeparator +--@label.lua +local fs = require("filesystem") +local shell = require("shell") + +local args, options = shell.parse(...) +if #args < 1 then + io.write("Usage: label [-a] [