1
0
Fork 0
cc-stuff/tape-rip.lua

92 lines
2.0 KiB
Lua
Raw Normal View History

2023-10-18 00:41:27 +03:00
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()
2023-10-18 00:54:19 +03:00
local i = 0
2023-10-18 00:41:27 +03:00
while not src.isEnd() do
2023-10-18 00:56:03 +03:00
dst.write(src.read(6000 * 30))
2023-10-18 00:41:27 +03:00
local pos, sz = src.getPosition(), src.getSize()
term.setCursorPos(1, y)
term.clearLine()
2023-10-18 00:55:17 +03:00
if (i % 256) == 0 then
2023-10-18 00:54:19 +03:00
textProgress(pos / sz, colors.green, colors.gray, "%7.3f%% %8d / %8d", pos * 100 / sz, pos, sz)
os.sleep(0.01)
end
i = i + 1
2023-10-18 00:41:27 +03:00
end
2023-10-18 00:54:19 +03:00
local pos, sz = src.getPosition(), src.getSize()
textProgress(pos / sz, colors.green, colors.gray, "%7.3f%% %8d / %8d", pos * 100 / sz, pos, sz)
print()