diff options
| author | ruki <[email protected]> | 2025-11-20 12:29:42 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-20 12:29:42 +0800 |
| commit | 6046c050650e63b430777dda1c6ce497ac43917e (patch) | |
| tree | c87babf08377b31f2ddf87b4e79cb34312927b83 /xmake/modules/utils/progress.lua | |
| parent | 867cac26f83933451ab21ea94c6d2ddded373992 (diff) | |
| parent | 800e4177ac9584c30a56bf1c3ce356b76b6653e8 (diff) | |
Merge pull request #7040 from xmake-io/progress
Improve multi-row progress
Diffstat (limited to 'xmake/modules/utils/progress.lua')
| -rw-r--r-- | xmake/modules/utils/progress.lua | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/xmake/modules/utils/progress.lua b/xmake/modules/utils/progress.lua index 4778c073c..60066ddc5 100644 --- a/xmake/modules/utils/progress.lua +++ b/xmake/modules/utils/progress.lua @@ -202,11 +202,22 @@ function _display_subprocess_lines(order_lineinfos) linecount = linecount + 1 end end + -- clear the left lines + local left_linecount = #order_lineinfos - linecount + if left_linecount > 0 then + for i = 1, left_linecount do + tty.erase_line().cr() + print("") + end + tty.cursor_move_up(left_linecount) + end _g.linecount = linecount end -- redraw the multirow progress area (internal helper) -function _redraw_multirow_progress(maxwidth) +-- @param maxwidth: window width +-- @param current_time: optional current time (to avoid repeated os.mclock() calls) +function _redraw_multirow_progress(maxwidth, current_time) local last_total_progress = _g.last_total_progress if not last_total_progress then return @@ -217,7 +228,9 @@ function _redraw_multirow_progress(maxwidth) cprint(last_total_progress) -- build and display the subprocess lines - local current_time = os.mclock() + if not current_time then + current_time = os.mclock() + end local order_lineinfos = _build_ordered_subprocess_lineinfos(maxwidth, current_time) _display_subprocess_lines(order_lineinfos) io.flush() @@ -266,6 +279,7 @@ function _show_progress_with_multirow_refresh(progress, format, ...) -- save the total progress line and progress value for potential redraw in show_output _g.last_total_progress = progress_line _g.last_total_progress_value = progress + _g.last_show_time = current_time -- update the current progress info local current_lineinfo = progress_lineinfos[running] @@ -382,6 +396,41 @@ function show_output(format, ...) end end +-- check if multirow refresh mode is enabled +function is_multirow() + return _is_multirow_refresh() +end + +-- refresh the multirow progress display to update elapsed time +-- this is useful for long-running tasks to keep the elapsed time updated +function refresh() + local refresh_mode = _g.refresh_mode + if refresh_mode == "multirow" then + -- get current time once and reuse it + local current_time = os.mclock() + + -- only refresh if more than 500ms has passed since last show + -- this avoids too frequent refreshes + local last_show_time = _g.last_show_time + if last_show_time then + local elapsed = current_time - last_show_time + if elapsed <= 500 then + return + end + end + + -- move cursor back to the top of progress area to avoid scrolling + local linecount = _g.linecount or 0 + if linecount > 0 then + tty.cursor_move_up(linecount + 1) + end + + -- redraw the progress area immediately, passing current_time to avoid repeated calls + local maxwidth = os.getwinsize().width + _redraw_multirow_progress(maxwidth, current_time) + end +end + -- abort the progress display mode, used for error exit or early termination -- this function cleans up the progress display area and restores the terminal state function show_abort() |
