^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "progress.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) static void null_progress__update(struct ui_progress *p __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static struct ui_progress_ops null_progress__ops =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) .update = null_progress__update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct ui_progress_ops *ui_progress__ops = &null_progress__ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void ui_progress__update(struct ui_progress *p, u64 adv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u64 last = p->curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) p->curr += adv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (p->curr >= p->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u64 nr = DIV_ROUND_UP(p->curr - last, p->step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) p->next += nr * p->step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ui_progress__ops->update(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) void __ui_progress__init(struct ui_progress *p, u64 total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const char *title, bool size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) p->curr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) p->next = p->step = total / 16 ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) p->total = total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) p->title = title;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) p->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (ui_progress__ops->init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ui_progress__ops->init(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void ui_progress__finish(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (ui_progress__ops->finish)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ui_progress__ops->finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }