Improve code readability: Reorder progress parsing logic and fix comment (ETA vs elapsed time)

This commit is contained in:
mgutt
2025-10-26 14:00:57 +01:00
parent da0bfcafcc
commit 8c0ff09401

View File

@@ -63,17 +63,10 @@ function mb_strimhalf($text, $width, $trim_marker = "") {
function parse_rsync_progress($status, $action_label) {
// obtain filename line like "/mnt/disk6/images/image.jpg" (-v is used to obtain the opposite of the progress line regex)
$file_line = exec("tac $status | grep -m1 -v -E '[0-9]+%.*[0-9]+:[0-9]+:[0-9]+' | tr -s ' ' || echo ''");
$text[0] = $action_label . "... ";
if ($file_line) {
$text[0] .= htmlspecialchars(mb_strimhalf($file_line, 70, '...'), ENT_QUOTES, 'UTF-8');
}
// obtain progress line like:
// currently running (showing total ETA):
// " 37.91G 4% 58.59MB/s 3:47:20"
// transfer of single file finished (ETA changes temporarily to estimated time)
// transfer of single file finished (ETA changes temporarily to elapsed time)
// " 37.93G 4% 53.80MB/s 0:11:12 (xfr#32, to-chk=554/596)"
// note: we do not loop with PHP as there could be a huge amount of progress lines b
$progress_line = trim(exec("tac $status | grep -m1 -E '[0-9]+%.*[0-9]+:[0-9]+:[0-9]+' | tr -s ' ' || echo ''"));
@@ -84,6 +77,13 @@ function parse_rsync_progress($status, $action_label) {
}
}
// obtain filename line like "/mnt/disk6/images/image.jpg" (-v is used to obtain the opposite of the progress line regex)
$file_line = exec("tac $status | grep -m1 -v -E '[0-9]+%.*[0-9]+:[0-9]+:[0-9]+' | tr -s ' ' || echo ''");
$text[0] = $action_label . "... ";
if ($file_line) {
$text[0] .= htmlspecialchars(mb_strimhalf($file_line, 70, '...'), ENT_QUOTES, 'UTF-8');
}
return $text;
}