diff options
| author | ruki <[email protected]> | 2024-03-18 23:44:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-03-18 23:44:06 +0800 |
| commit | ca16db30058d8a512d73a7bee28e57f72de4c338 (patch) | |
| tree | 37f034b25bf468297daa0ec4bc8dd5490f380c5e | |
| parent | ef76c91d0517361173246f6ee62b3b6c3ba1e29e (diff) | |
improve progress
| -rw-r--r-- | tests/modules/scheduler/runjobs.lua | 16 | ||||
| -rw-r--r-- | xmake/actions/build/build.lua | 2 | ||||
| -rw-r--r-- | xmake/actions/build/build_files.lua | 2 | ||||
| -rw-r--r-- | xmake/actions/test/main.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/async/runjobs.lua | 30 | ||||
| -rw-r--r-- | xmake/modules/utils/progress.lua | 18 |
6 files changed, 45 insertions, 27 deletions
diff --git a/tests/modules/scheduler/runjobs.lua b/tests/modules/scheduler/runjobs.lua index d84f23152..4e2a98cc0 100644 --- a/tests/modules/scheduler/runjobs.lua +++ b/tests/modules/scheduler/runjobs.lua @@ -2,12 +2,12 @@ import("core.base.scheduler") import("private.async.jobpool") import("async.runjobs") -function _jobfunc(index, total) +function _jobfunc(index, total, opt) print("%s: run job (%d/%d)", scheduler.co_running(), index, total) local dt = os.mclock() os.sleep(1000) dt = os.mclock() - dt - print("%s: run job (%d/%d) end, dt: %d ms", scheduler.co_running(), index, total, dt) + print("%s: run job (%d/%d) end, progress: %s, dt: %d ms", scheduler.co_running(), index, total, opt.progress, dt) end function main() @@ -22,16 +22,16 @@ function main() -- test jobs print("==================================== test jobs ====================================") local jobs = jobpool.new() - local root = jobs:addjob("job/root", function (idx, total) - _jobfunc(idx, total) + local root = jobs:addjob("job/root", function (index, total, opt) + _jobfunc(index, total, opt) end) for i = 1, 3 do - local job = jobs:addjob("job/" .. i, function (idx, total) - _jobfunc(idx, total) + local job = jobs:addjob("job/" .. i, function (index, total, opt) + _jobfunc(index, total, opt) end, {rootjob = root}) for j = 1, 50 do - jobs:addjob("job/" .. i .. "/" .. j, function (idx, total) - _jobfunc(idx, total) + jobs:addjob("job/" .. i .. "/" .. j, function (index, total, opt) + _jobfunc(index, total, opt) end, {rootjob = job}) end end diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua index 1db197cd6..fbbb9fdbc 100644 --- a/xmake/actions/build/build.lua +++ b/xmake/actions/build/build.lua @@ -299,7 +299,7 @@ function main(targetnames, group_pattern) if errors and progress.showing_without_scroll() then print("") end - end, comax = option.get("jobs") or 1, curdir = curdir, count_as_index = true, distcc = distcc}) + end, comax = option.get("jobs") or 1, curdir = curdir, distcc = distcc}) os.cd(curdir) end end diff --git a/xmake/actions/build/build_files.lua b/xmake/actions/build/build_files.lua index 909e4e54b..57b586173 100644 --- a/xmake/actions/build/build_files.lua +++ b/xmake/actions/build/build_files.lua @@ -202,7 +202,7 @@ function main(targetname, group_pattern, sourcefiles) local batchjobs = _get_batchjobs(targetname, group_pattern, filepatterns) if batchjobs and batchjobs:size() > 0 then local curdir = os.curdir() - runjobs("build_files", batchjobs, {comax = option.get("jobs") or 1, curdir = curdir, count_as_index = true}) + runjobs("build_files", batchjobs, {comax = option.get("jobs") or 1, curdir = curdir}) os.cd(curdir) else wprint("%s not found!", sourcefiles) diff --git a/xmake/actions/test/main.lua b/xmake/actions/test/main.lua index ccc69e897..852f00662 100644 --- a/xmake/actions/test/main.lua +++ b/xmake/actions/test/main.lua @@ -270,7 +270,7 @@ function _run_tests(tests) print("running tests ...") local report = {passed = 0, total = #ordertests} local jobs = tonumber(option.get("jobs")) or os.default_njob() - runjobs("run_tests", function (index) + runjobs("run_tests", function (index, total, opt) local testinfo = ordertests[index] if testinfo then local target = testinfo.target @@ -286,7 +286,7 @@ function _run_tests(tests) if option.get("verbose") then progress_format = progress_format .. "${dim}" end - local progress = math.floor(index * 100 / #ordertests) + local progress = opt.progress:percent() local padding = maxwidth - #testinfo.name cprint(progress_format .. "%s%s .................................... " .. status_color .. "%s${clear} ${bright}%0.3fs", progress, testinfo.name, (" "):rep(padding), passed and "passed" or "failed", spent / 1000) diff --git a/xmake/modules/async/runjobs.lua b/xmake/modules/async/runjobs.lua index 19090da5d..476efe08f 100644 --- a/xmake/modules/async/runjobs.lua +++ b/xmake/modules/async/runjobs.lua @@ -40,12 +40,12 @@ end -- runjobs("test", function () os.sleep(10000) end, { progress = { chars = {'/','\'} } }) -- see module utils.progress -- -- local jobs = jobpool.new() --- local root = jobs:addjob("job/root", function (idx, total) --- print(idx, total) +-- local root = jobs:addjob("job/root", function (index, total) +-- print(index, total) -- end) -- for i = 1, 3 do --- local job = jobs:addjob("job/" .. i, function (idx, total) --- print(idx, total) +-- local job = jobs:addjob("job/" .. i, function (index, total) +-- print(index, total) -- end, {rootjob = root}) -- end -- runjobs("test", jobs, {comax = 6, timeout = 1000, on_timer = function (running_jobs_indices) end}) @@ -154,12 +154,30 @@ function main(name, jobs, opt) -- run jobs local index = 0 local count = 0 - local count_as_index = opt.count_as_index local priority_prev = 0 local priority_curr = 0 local job_pending = nil local abort = false local abort_errors + local progress_wrapper = {} + progress_wrapper.current = function () + return count + end + progress_wrapper.total = function () + return total + end + progress_wrapper.percent = function () + if total and total > 0 then + return math.floor((count * 100) / total) + else + return 0 + end + end + debug.setmetatable(progress_wrapper, { + __tostring = function () + return string.format("%d%%", progress_wrapper.percent()) + end + }) while index < total do scheduler.co_group_begin(group_name, function (co_group) local freemax = comax - #co_group @@ -234,7 +252,7 @@ function main(name, jobs, opt) if opt.curdir then os.cd(opt.curdir) end - jobfunc(count_as_index and count or i, total) + jobfunc(i, total, {progress = progress_wrapper}) count = count + 1 end running_jobs_indices[i] = nil diff --git a/xmake/modules/utils/progress.lua b/xmake/modules/utils/progress.lua index 074046a69..2655f0701 100644 --- a/xmake/modules/utils/progress.lua +++ b/xmake/modules/utils/progress.lua @@ -26,10 +26,10 @@ import("core.base.tty") import("core.theme.theme") -- define module -local process = process or object { _init = { "_RUNNING", "_INDEX", "_STREAM", "_OPT" } } +local progress = progress or object { _init = { "_RUNNING", "_INDEX", "_STREAM", "_OPT" } } -- stop the progress indicator, clear written frames -function process:stop() +function progress:stop() if self._RUNNING ~= 0 then self:clear() self._RUNNING = 0 @@ -37,7 +37,7 @@ function process:stop() end end -function process:_clear() +function progress:_clear() if self._RUNNING == 1 then tty.erase_line_to_end() self._RUNNING = 2 @@ -46,14 +46,14 @@ function process:_clear() end -- clear previous frame of the progress indicator -function process:clear() +function progress:clear() if self:_clear() then self._STREAM:flush() end end -- write next frame of the progress indicator -function process:write() +function progress:write() local chars = self._OPT.chars[self._INDEX % #self._OPT.chars + 1] tty.cursor_and_attrs_save() self._STREAM:write(chars) @@ -64,7 +64,7 @@ function process:write() end -- check if the progress indicator is running -function process:running() +function progress:running() return self._RUNNING and true or false end @@ -73,7 +73,7 @@ function showing_without_scroll() return _g.showing_without_scroll end --- show the message with process +-- show the message with progress function show(progress, format, ...) progress = math.floor(progress) local progress_prefix = "${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} " @@ -112,7 +112,7 @@ function show(progress, format, ...) end end --- get the message text with process +-- get the message text with progress function text(progress, format, ...) progress = math.floor(progress) local progress_prefix = "${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} " @@ -135,5 +135,5 @@ function new(stream, opt) if opt.chars == nil or #opt.chars == 0 then opt.chars = theme.get("text.spinner.chars") end - return process {_OPT = opt, _STREAM = stream, _RUNNING = 0, _INDEX = 0} + return progress {_OPT = opt, _STREAM = stream, _RUNNING = 0, _INDEX = 0} end |
