From 58b63b4cde1538beccd306a29423bb64c8d201df Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 17 Oct 2023 20:19:45 +0300 Subject: [PATCH] Improved tapeget and added some old mess --- mess/progress-text.lua | 43 ++++++++++++++++++++++++++++++++++++ tapeget.lua | 49 +++++++++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 mess/progress-text.lua diff --git a/mess/progress-text.lua b/mess/progress-text.lua new file mode 100644 index 0000000..300eac3 --- /dev/null +++ b/mess/progress-text.lua @@ -0,0 +1,43 @@ +term.clear() +term.setCursorPos(1, 1) +term.setTextColor(colors.white) + +local tw, th = term.getSize() +print(string.format("Terminal: %dx%d", tw, th)) +local function printScale(c1, c2, p, fmt, ...) + 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) + print() +end + + +term.setBackgroundColor(colors.black) +local t = 0 +while true do + for i = 1, th do + local p = 0.5 + 0.5 * math.sin((i + t) * math.pi / 25) + term.setCursorPos(1, i) + printScale(colors.red, colors.gray, p, "%7.3f%%", p * 100) + end + os.sleep(0.05) + t = t + 1 +end diff --git a/tapeget.lua b/tapeget.lua index a85a5e7..87a1754 100644 --- a/tapeget.lua +++ b/tapeget.lua @@ -1,5 +1,36 @@ local args = { ... } +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 + local tape = peripheral.find("tape_drive") if not tape then print("tape where") @@ -22,27 +53,19 @@ if not req then end local headers = req.getResponseHeaders() -local length = headers["content-length"] +local length = headers["content-length"] or 1 -local function n_to_kib(value) - return string.format("%6.1f kiB", value / 1024) -end - -local written = 1 local _, y = term.getCursorPos() while true do local chunk = req.read(256) if not chunk then - print("EOF") break end tape.write(chunk) - written = written + 1 - if (written % 8192) == 0 then - os.sleep(0.01) - term.setCursorPos(1, y) - term.write(n_to_kib(written) .. " / " .. n_to_kib(length) .. string.format(" %7.3f%%", 100 * written / length)) - end + term.setCursorPos(1, y) + term.clearLine() + textProcess(written / length, colors.green, colors.gray, "%8d / %8d", n_to_kib(written), n_to_kib(length)) + os.sleep(0.01) end tape.stop()