summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ui.c10
-rw-r--r--src/utils/size_info.c27
-rw-r--r--src/utils/size_info.h14
3 files changed, 47 insertions, 4 deletions
diff --git a/src/ui.c b/src/ui.c
index ff6f565..d140207 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -9,8 +9,8 @@
#include "constants.h"
#include "logger.h"
-#include "process_url.h"
#include "ui.h"
+#include "utils/size_info.h"
static nk_size pct;
static char *outPath;
@@ -26,9 +26,11 @@ void load_ui(struct ui_struct *ui) {
poll_status(ui->stat);
if (ui->stat->total) {
pct = (ui->stat->cur) * 100 / (ui->stat->total);
- sprintf(status_string,
- "%" CURL_FORMAT_CURL_OFF_T "/%" CURL_FORMAT_CURL_OFF_T ", %hhu%%",
- ui->stat->cur, ui->stat->total, (unsigned char)pct);
+ size_info_t info_cur = num2sizeinfo((unsigned long)ui->stat->cur);
+ size_info_t info_total = num2sizeinfo((unsigned long)ui->stat->total);
+ sprintf(status_string, "%.2Lf%s/%.2Lf%s, %hhu%%", info_cur.n,
+ info_cur.unit_str, info_total.n, info_total.unit_str,
+ (unsigned char)pct);
}
if (ui->stat->is_done) {
(ui->stat->total) = 0; // To prevent nuklear further updating status_string
diff --git a/src/utils/size_info.c b/src/utils/size_info.c
new file mode 100644
index 0000000..6b238bb
--- /dev/null
+++ b/src/utils/size_info.c
@@ -0,0 +1,27 @@
+#include "size_info.h"
+
+static const char *size_unit_map[] = {"B", "KB", "MB", "GB"};
+
+size_info_t num2sizeinfo(const unsigned long n) {
+ size_info_t size_info = {0};
+ long double tmp = (long double)n;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_B;
+ } else {
+ tmp /= 1024;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_KB;
+ } else {
+ tmp /= 1024;
+ if ((unsigned long)tmp / 1024 == 0) {
+ size_info.unit = SIZE_MB;
+ } else {
+ tmp /= 1024;
+ size_info.unit = SIZE_GB;
+ }
+ }
+ }
+ size_info.n = tmp;
+ size_info.unit_str = size_unit_map[size_info.unit];
+ return size_info;
+}
diff --git a/src/utils/size_info.h b/src/utils/size_info.h
new file mode 100644
index 0000000..e0994c1
--- /dev/null
+++ b/src/utils/size_info.h
@@ -0,0 +1,14 @@
+#ifndef SIZE_INFO_H_
+#define SIZE_INFO_H_
+
+typedef enum size_unit { SIZE_B, SIZE_KB, SIZE_MB, SIZE_GB } size_unit_t;
+
+typedef struct size_info {
+ long double n;
+ size_unit_t unit;
+ const char *unit_str;
+} size_info_t;
+
+size_info_t num2sizeinfo(const unsigned long n);
+
+#endif