From caa1b5a84fba3072dc29fd1aba3fd65b218554ee Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 17 Oct 2023 19:54:57 +0300 Subject: [PATCH] Added progressbar --- tape-playlist.lua | 58 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/tape-playlist.lua b/tape-playlist.lua index f0cfd13..32e028b 100644 --- a/tape-playlist.lua +++ b/tape-playlist.lua @@ -7,8 +7,11 @@ end local running = true +term.clear() +local screen_w, screen_h = term.getSize() local table_of_contents = {} + local function read32() local v = 0 for i = 1, 4 do @@ -24,23 +27,56 @@ local function bytes2time(b) return string.format("%dm, %ds", math.floor(s / 60), s % 60) end -term.clear() -local screen_w, screen_h = term.getSize() +local function textProgress(p, c1, c2, fmt, ...) + 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 + parallel.waitForAll( function() while running do - for i = 1, math.min(screen_h - 1, 48) do - term.setCursorPos(1, i) - term.clearLine() - local song = table_of_contents[i] - if song then - term.write(string.format("#%2d %9s %s", i, bytes2time(song.length), song.title)) + if drive.isReady() then + local pos, size = drive.getPosition(), drive.getSize() + for i = 1, math.min(screen_h - 2, 48) do + term.setCursorPos(1, i) + term.clearLine() + local song = table_of_contents[i] + if song then + local is_playing = pos >= song.offset and pos < song.ending + local s = string.format("#%2d %9s %s", i, bytes2time(song.length), song.title) + if is_playing then + local p = (pos - song.offset) / song.length + textProgress(p, colors.lime, colors.lightGray, s) + else + term.setBackgroundColor(i % 2 == 0 and colors.gray or colors.black) + term.write(s) + end + end end + term.setCursorPos(1, screen_h) + textProgress(pos / size, colors.red, colors.gray, "%8d / %8d [%s]", pos, size, drive.getState()) end - term.setCursorPos(1, screen_h) - term.clearLine() - term.write(string.format("%s - %d", drive.getState(), drive.getPosition())) os.sleep(0.1) end end,