diff options
-rw-r--r-- | src/common.c | 19 | ||||
-rw-r--r-- | tests/test_common.c | 17 |
2 files changed, 26 insertions, 10 deletions
diff --git a/src/common.c b/src/common.c index e1bfaaf..812431a 100644 --- a/src/common.c +++ b/src/common.c @@ -47,27 +47,26 @@ char* trim(char* str) { } void print_bar(int width, float percent, char* buf) { - double fill = width * percent; + int fill_maj = width * percent; + int fill_min = (width * percent - fill_maj) * 10; int i = 1; - int remainder = 0; buf[0] = '['; - while(i < fill) { + while(i <= fill_maj) { buf[i] = ':'; i++; } - remainder = (fill + 1 - i) * 10; - if(i > fill && remainder >= 5.0) { - buf[i] = ':'; - i++; - } else if(i > fill && remainder < 5.0 && remainder > 0) { - buf[i] = '.'; + if(fill_maj > 0 && fill_min > 0) { + if(fill_min >= 5) + buf[i] = ':'; + else + buf[i] = '.'; i++; } - while(i < width) { + while(i <= width) { buf[i] = ' '; i++; } diff --git a/tests/test_common.c b/tests/test_common.c index e08c0ee..42acf48 100644 --- a/tests/test_common.c +++ b/tests/test_common.c @@ -41,6 +41,23 @@ void test_bar() { print_bar(10, 0, buf); assert_strncmp(buf, "[ ", 3); + print_bar(8, 0, buf); + assert_strcmp(buf, "[ ]"); + + print_bar(8, 1, buf); + assert_strcmp(buf, "[::::::::]"); + + print_bar(8, .42, buf); + assert_strcmp(buf, "[:::. ]"); + + // Fill is 0, so no colons or periods + print_bar(10, .13, buf); + assert_strcmp(buf, "[:. ]"); + + // Fill is 0, so no colons or periods + print_bar(10, .17, buf); + assert_strncmp(buf, "[:: ", 4); + // Fill is 2, +.5, so three colons print_bar(10, .25, buf); assert_strncmp(buf, "[::: ", 5); |