From 736d763b649c035b9c921cc1aa19a74452430360 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 18 Oct 2023 00:41:27 +0300 Subject: [PATCH] Added tape cloner --- tape-rip.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tape-rip.lua diff --git a/tape-rip.lua b/tape-rip.lua new file mode 100644 index 0000000..c551938 --- /dev/null +++ b/tape-rip.lua @@ -0,0 +1,84 @@ +local args = { ... } + +if #args == 0 then + print("Usage: tape-rip [-S] [source-drive] [dst-drive]") + print(" -S don't seek to the beginning of destination drive") +end + +local seekToStart = true +if args[1] == "-S" then + seekToStart= false + table.remove(args, 1) +end + +local function textProgress(p, c1, c2, fmt, ...) + p = math.min(p, 1) + local tw = term.getSize() + local str = string.format(fmt, ...) + local w1 = math.ceil(p * tw) + local w2 = tw - w1 + + local bg = term.getBackgroundColor() + + term.setBackgroundColor(c1) + term.write(str:sub(1, w1)) + local rem = w1 - #str + if rem > 0 then + term.write(string.rep(" ", rem)) + end + + term.setBackgroundColor(c2) + term.write(str:sub(w1 + 1, w1 + w2)) + + rem = math.min(tw - #str, w2) + if rem > 0 then + term.write(string.rep(" ", rem)) + end + + term.setBackgroundColor(bg) +end + +local src = peripheral.wrap(args[1]) +local dst = peripheral.wrap(args[2]) + +local failure = false +if not src then + printError("Source drive not found") + failure = true +end + +if not dst then + printError("Destination drive not found") + failure = true +end + +if src and not src.isReady() then + printError("Source drive is empty") + failure = true +end + +if dst and not dst.isReady() then + printError("Destination drive is empty") + failure = true +end + +if failure then + printError("Some errors occurred, exiting") + return +end + +src.seek(-src.getSize()) +if seekToStart then + dst.seek(-dst.getSize()) +end + +local _, y = term.getCursorPos() +while not src.isEnd() do + dst.write(src.read(6000 * 10)) + local pos, sz = src.getPosition(), src.getSize() + term.setCursorPos(1, y) + term.clearLine() + textProgress(pos / sz, colors.green, colors.gray, "%7.3f%% %8d / %8d", pos * 100 / sz, pos, sz) + os.sleep(0.01) +end +