diff options
| author | ruki <[email protected]> | 2024-03-18 18:11:05 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-18 18:11:05 +0800 |
| commit | 1ab1302b0568c83dbfe150d2cd67331f8ec9661c (patch) | |
| tree | f7f4073c1675d79921fa47abb1e4c3da350d63f5 /xmake/modules | |
| parent | ef76c91d0517361173246f6ee62b3b6c3ba1e29e (diff) | |
| parent | 9847562068e7a9460a04f69d8146f5e89dd591f5 (diff) | |
Merge pull request #4849 from xmake-io/progress
Improve progress
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/async/runjobs.lua | 30 | ||||
| -rw-r--r-- | xmake/modules/private/action/build/object.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/async/buildjobs.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/utils/progress.lua | 22 |
4 files changed, 38 insertions, 20 deletions
diff --git a/xmake/modules/async/runjobs.lua b/xmake/modules/async/runjobs.lua index 19090da5d..299b868df 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, opt) +-- print(index, total, opt.progress) -- 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, opt) +-- print(index, total, opt.progress) -- 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,8 +252,8 @@ function main(name, jobs, opt) if opt.curdir then os.cd(opt.curdir) end - jobfunc(count_as_index and count or i, total) count = count + 1 + jobfunc(i, total, {progress = progress_wrapper}) end running_jobs_indices[i] = nil end, diff --git a/xmake/modules/private/action/build/object.lua b/xmake/modules/private/action/build/object.lua index 486c2e7ce..c4c646cba 100644 --- a/xmake/modules/private/action/build/object.lua +++ b/xmake/modules/private/action/build/object.lua @@ -142,8 +142,8 @@ function main(target, batchjobs, sourcebatch, opt) local objectfile = sourcebatch.objectfiles[i] local dependfile = sourcebatch.dependfiles[i] local sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile) - batchjobs:addjob(sourcefile, function (index, total) - local build_opt = table.join({objectfile = objectfile, dependfile = dependfile, sourcekind = sourcekind, progress = (index * 100) / total}, opt) + batchjobs:addjob(sourcefile, function (index, total, jobopt) + local build_opt = table.join({objectfile = objectfile, dependfile = dependfile, sourcekind = sourcekind, progress = jobopt.progress}, opt) build_object(target, sourcefile, build_opt) end, {rootjob = rootjob, distcc = opt.distcc}) end diff --git a/xmake/modules/private/async/buildjobs.lua b/xmake/modules/private/async/buildjobs.lua index d19335167..dc2e736f3 100644 --- a/xmake/modules/private/async/buildjobs.lua +++ b/xmake/modules/private/async/buildjobs.lua @@ -51,7 +51,7 @@ end nodes["node1"] = { name = "node1", deps = {"node2", "node3"}, - job = batchjobs:newjob("/job/node1", function(index, total) + job = batchjobs:newjob("/job/node1", function(index, total, opt) end) } --]] diff --git a/xmake/modules/utils/progress.lua b/xmake/modules/utils/progress.lua index 074046a69..aefaef1af 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,9 +73,9 @@ 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) + progress = type(progress) == "table" and progress:percent() or math.floor(progress) local progress_prefix = "${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} " if option.get("verbose") then cprint(progress_prefix .. "${dim}" .. format, progress, ...) @@ -112,9 +112,9 @@ 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) + progress = type(progress) == "table" and progress:percent() or math.floor(progress) local progress_prefix = "${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} " if option.get("verbose") then return string.format(progress_prefix .. "${dim}" .. format, progress, ...) @@ -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 |
