table: introduce FORMAT_BPS type
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Jun 2019 13:03:42 +0000 (22:03 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Jun 2019 14:15:19 +0000 (23:15 +0900)
src/shared/format-table.c
src/shared/format-table.h

index d6612a7..54ca197 100644 (file)
@@ -15,6 +15,7 @@
 #include "pretty-print.h"
 #include "sort-util.h"
 #include "string-util.h"
+#include "strxcpyx.h"
 #include "terminal-util.h"
 #include "time-util.h"
 #include "utf8.h"
@@ -235,6 +236,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_SIZE:
         case TABLE_INT64:
         case TABLE_UINT64:
+        case TABLE_BPS:
                 return sizeof(uint64_t);
 
         case TABLE_INT32:
@@ -723,6 +725,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         break;
 
                 case TABLE_SIZE:
+                case TABLE_BPS:
                         buffer.size = va_arg(ap, uint64_t);
                         data = &buffer.size;
                         break;
@@ -885,6 +888,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                         return CMP(a->timespan, b->timespan);
 
                 case TABLE_SIZE:
+                case TABLE_BPS:
                         return CMP(a->size, b->size);
 
                 case TABLE_INT:
@@ -1023,6 +1027,24 @@ static const char *table_data_format(TableData *d) {
                 break;
         }
 
+        case TABLE_BPS: {
+                _cleanup_free_ char *p;
+                size_t n;
+
+                p = new(char, FORMAT_BYTES_MAX+2);
+                if (!p)
+                        return NULL;
+
+                if (!format_bytes_full(p, FORMAT_BYTES_MAX, d->size, 0))
+                        return "n/a";
+
+                n = strlen(p);
+                strscpy(p + n, FORMAT_BYTES_MAX + 2 - n, "bps");
+
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
         case TABLE_INT: {
                 _cleanup_free_ char *p;
 
@@ -1622,6 +1644,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
                 return json_variant_new_unsigned(ret, d->timespan);
 
         case TABLE_SIZE:
+        case TABLE_BPS:
                 if (d->size == (size_t) -1)
                         return json_variant_new_null(ret);
 
index ec2bbba..ada59f3 100644 (file)
@@ -15,6 +15,7 @@ typedef enum TableDataType {
         TABLE_TIMESTAMP,
         TABLE_TIMESPAN,
         TABLE_SIZE,
+        TABLE_BPS,
         TABLE_INT,
         TABLE_INT32,
         TABLE_INT64,