Make pv__calc_percentage() into pv_percentage() in number.c instead of a static function in display.c, so it can be used in multiple places in future.

This commit is contained in:
Andrew Wood
2024-10-04 17:15:18 +01:00
parent 9bd5a518ea
commit cd696e60ab
3 changed files with 25 additions and 17 deletions
+7
View File
@@ -78,6 +78,13 @@ extern unsigned int pv_getnum_count(const char *, bool);
*/
extern bool pv_getnum_check(const char *, pv_numtype);
/*
* Return an integer representing the first amount as a percentage of the
* second total, i.e. 100*amount/total. If the second value, the total, is
* zero or less, return 0.
*/
extern int pv_percentage(off_t, const off_t);
/*
* String handling wrappers.
*/
+2 -17
View File
@@ -151,21 +151,6 @@ void pv_screensize(unsigned int *width, unsigned int *height)
}
/*
* Calculate the percentage transferred so far and return it.
*/
static int pv__calc_percentage(off_t so_far, const off_t total)
{
if (total < 1)
return 0;
so_far *= 100;
so_far /= total;
return (int) so_far;
}
/*
* Given how many bytes have been transferred, the total byte count to
* transfer, and the current average transfer rate, return the estimated
@@ -751,7 +736,7 @@ void pv_calculate_transfer_rate(pvstate_t state, bool final)
if (state->calc.percentage > 199)
state->calc.percentage = 0;
} else {
state->calc.percentage = pv__calc_percentage(state->transfer.total_written, state->control.size);
state->calc.percentage = pv_percentage(state->transfer.total_written, state->control.size);
}
/* Ensure the percentage is never negative or huge. */
@@ -1128,7 +1113,7 @@ bool pv_format(pvstate_t state, bool final)
case PV_COMPONENT_BUFPERCENT:
/* Transfer buffer percentage utilisation. */
if (state->transfer.buffer_size > 0) {
int pct_used = pv__calc_percentage((off_t)
int pct_used = pv_percentage((off_t)
(state->transfer.read_position -
state->transfer.write_position),
(off_t)
+16
View File
@@ -280,4 +280,20 @@ bool pv_getnum_check(const char *str, pv_numtype type)
return true;
}
/*
* Return an integer representing "amount" as a percentage of "total", i.e.
* 100*amount/total. If "total" is zero or less, return 0.
*/
int pv_percentage(off_t amount, const off_t total)
{
if (total < 1)
return 0;
amount *= 100;
amount /= total;
return (int) amount;
}
/* EOF */