summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-02-16 23:22:36 +0000
committerdavidovski <david@davidovski.xyz>2022-02-16 23:22:36 +0000
commit89539c200bf93d53ee9b91b521c09bd4607e01d0 (patch)
tree106981fdbe61c423fafe2d12a74e1b6610601c6a
parente197b62585b15fbe89261922d288bbb901cd0e33 (diff)
added human formatting for hbar
-rw-r--r--src/glyphs.sh3
-rw-r--r--src/hbar.c37
-rwxr-xr-xtest/hbar.sh16
3 files changed, 48 insertions, 8 deletions
diff --git a/src/glyphs.sh b/src/glyphs.sh
index dc4c285..56b5625 100644
--- a/src/glyphs.sh
+++ b/src/glyphs.sh
@@ -4,5 +4,8 @@
export CHECKMARK="✔"
export CROSSMARK="✘"
+export HOURGLASS="⧖"
+export LARGE_CIRCLE="◯"
+export REFRESH="🗘"
export TABCHAR="╰┈➤ "
diff --git a/src/hbar.c b/src/hbar.c
index e301fb4..17840d8 100644
--- a/src/hbar.c
+++ b/src/hbar.c
@@ -20,6 +20,21 @@
#define SAVE_POS "\033[s"
#define LOAD_POS "\033[u"
+static void human_format(int bytes, char *output) {
+ char *suffix[] = {"B", "KB", "MB", "GB", "TB"};
+ char length = sizeof(suffix) / sizeof(suffix[0]);
+
+ int i = 0;
+ double dblBytes = bytes;
+
+ if (bytes > 1024) {
+ for (i = 0; (bytes / 1024) > 0 && i<length-1; i++, bytes /= 1024)
+ dblBytes = bytes / 1024.0;
+ }
+
+ sprintf(output, "%.0lf%s", dblBytes, suffix[i]);
+}
+
int main (int argc, char **argv) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
@@ -31,6 +46,7 @@ int main (int argc, char **argv) {
int completed = 0;
int line = 0;
bool terminate = false;
+ bool human = false;
char *color = DEFAULT_COLOR;
char *reset = DEFAULT_RESET;
@@ -38,14 +54,15 @@ int main (int argc, char **argv) {
int opt;
int option_index = 0;
- const char *optstring = "T:u:c:r:l:t";
+ const char *optstring = "T:u:c:r:l:th";
static const struct option opts[] = {
{"text", required_argument, 0, 'T'},
{"unit", optional_argument, 0, 'u'},
{"color", optional_argument, 0, 'c'},
{"reset", optional_argument, 0, 'r'},
- {"line", optional_argument, 0, ';'},
- {"terminate", no_argument, 0, 't'}
+ {"line", optional_argument, 0, 'l'},
+ {"terminate", no_argument, 0, 't'},
+ {"human-readable", no_argument, 0, 'h'}
};
while ((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) {
@@ -65,6 +82,9 @@ int main (int argc, char **argv) {
case 't':
terminate = true;
break;
+ case 'h':
+ human = true;
+ break;
case 'l':
line = atoi(optarg);
break;
@@ -85,7 +105,16 @@ int main (int argc, char **argv) {
}
char *count = malloc(width);
- sprintf(count, "[%d%s/%d%s]", completed, unit, total, unit);
+ if (human) {
+ // i suck at these
+ char *c = malloc(width);
+ char *t = malloc(width);
+ human_format(completed, c);
+ human_format(total, t);
+ sprintf(count, "[%s%s/%s%s]", c, unit, t, unit);
+ } else {
+ sprintf(count, "[%d%s/%d%s]", completed, unit, total, unit);
+ }
printf(RESET "\r");
printf(color);
diff --git a/test/hbar.sh b/test/hbar.sh
index 80f6a9c..e7f43ef 100755
--- a/test/hbar.sh
+++ b/test/hbar.sh
@@ -13,9 +13,17 @@ ${HBAR} -t -T "${TEXT}" -u ${UNIT} $x $MAX
hbar
for x in $(seq $MAX); do
- ${HBAR} -l 0 -T "${TEXT}" -u ${UNIT} $((MAX - x)) $MAX
- ${HBAR} -l 1 -T "${TEXT}" -u ${UNIT} $x $MAX
+ ${HBAR} -l 0 -T "${TEXT}" $((MAX - x)) $MAX
+ ${HBAR} -l 1 -T "${TEXT}" $x $MAX
sleep 0.01
done
-${HBAR} -l 0 -t -T "${TEXT}" -u ${UNIT} $((MAX-x)) $MAX
-${HBAR} -l 1 -t -T "${TEXT}" -u ${UNIT} $x $MAX
+${HBAR} -l 1 -T "${TEXT}" $x $MAX
+${HBAR} -l 0 -t -T "${TEXT}" $((MAX-x)) $MAX
+
+MAX=20000000
+
+for x in $(seq 0 991 $MAX); do
+ ${HBAR} -h -T "${TEXT}" $x $MAX
+ sleep 0.01
+done
+${HBAR} -ht -T "${TEXT}" $x $MAX