forked from hkc/cc-stuff
1
0
Fork 0
cc-stuff/tapeget.lua

125 lines
2.7 KiB
Lua
Raw Normal View History

2023-10-03 18:19:15 +03:00
local args = { ... }
2023-10-18 01:11:38 +03:00
local seekTo = 0
2023-10-18 01:06:35 +03:00
local driveName = nil
if args[1] == "-S" then
table.remove(args, 1)
2023-10-18 01:11:38 +03:00
seekTo = tonumber(table.remove(args, 1))
2023-10-18 01:06:35 +03:00
end
if args[1] == "-D" then
table.remove(args, 1)
driveName = table.remove(args, 1)
end
local function n_to_kib(value)
return string.format("%6.1f kiB", value / 1024)
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
2023-10-18 01:06:35 +03:00
local tape
if driveName ~= nil then
tape = peripheral.wrap(driveName)
else
local drives = { peripheral.find("tape_drive") }
if #drives == 0 then
print("Drive where")
2023-10-18 00:44:24 +03:00
return
2023-10-18 01:06:35 +03:00
elseif #tape ~= 1 then
print("More than one drive found:")
for i = 1, #drives do
print(peripheral.getName(drives[i]))
end
print("Input drive name:")
io.write("> ")
tape = peripheral.wrap(read())
if not tape then
printError("wrong name")
return
end
else
tape = drives[1]
2023-10-18 00:44:24 +03:00
end
2023-10-03 18:19:15 +03:00
end
if not http then
print("no http, check config")
return
end
2023-10-17 16:03:16 +03:00
local req, err = http.get(args[1], {}, true)
2023-10-03 18:19:15 +03:00
if not req then
2023-10-17 16:03:16 +03:00
print("oopsie: "..err)
2023-10-03 18:19:15 +03:00
return
end
2023-10-03 19:33:02 +03:00
local headers = req.getResponseHeaders()
2023-10-17 20:29:15 +03:00
local length = tonumber(headers["content-length"]) or 1
2023-10-03 19:33:02 +03:00
2023-10-17 20:28:18 +03:00
if length > tape.getSize() then
printError("Tape is smaller than the file you're trying to write")
printError("Are you sure?")
io.write("Write anyways? [y/N]: ")
local r = read()
if r ~= "y" and r ~= "Y" then
return
end
end
tape.stop()
2023-10-18 01:11:38 +03:00
tape.seek(-tape.getSize())
tape.seek(seekTo)
tape.stop()
2023-10-17 20:28:18 +03:00
2023-10-03 19:46:25 +03:00
local _, y = term.getCursorPos()
2023-10-17 20:23:20 +03:00
local written = 0
2023-10-17 20:28:18 +03:00
local i = 1
2023-10-03 18:19:15 +03:00
while true do
2023-10-17 16:03:16 +03:00
local chunk = req.read(256)
2023-10-03 18:19:15 +03:00
if not chunk then
2023-10-03 18:45:22 +03:00
break
2023-10-03 18:19:15 +03:00
end
2023-10-17 20:23:20 +03:00
written = written + #chunk
2023-10-03 18:25:40 +03:00
tape.write(chunk)
2023-10-17 20:28:18 +03:00
if i % 10 == 0 then
term.setCursorPos(1, y)
term.clearLine()
textProgress(written / length, colors.green, colors.gray, "%s / %s", n_to_kib(written), n_to_kib(length))
os.sleep(0.01)
end
2023-10-03 18:19:15 +03:00
end
2023-10-17 20:28:18 +03:00
term.setCursorPos(1, y)
term.clearLine()
print("Written "..n_to_kib(written))
2023-10-03 18:19:15 +03:00
2023-10-03 18:45:22 +03:00
tape.stop()
2023-10-18 01:11:38 +03:00
tape.seek(-tape.getSize())
tape.stop()