Skip to content

Commit 57dfaae

Browse files
committed
util: remove unnecessary row end padding print
A space added between each columns but not necessary for end of line. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent a9f9f90 commit 57dfaae

1 file changed

Lines changed: 31 additions & 29 deletions

File tree

util/table.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ static int table_get_value_width(struct value *v)
4141
return len;
4242
}
4343

44-
static void table_print_centered(struct value *val, int width, enum fmt_type type)
44+
static void table_print_centered(struct value *val, int width, enum fmt_type type, bool add_pad)
4545
{
4646
int i, len, left_pad, right_pad;
4747
char buf[64];
4848

49-
if (type == FMT_STRING)
49+
if (type == FMT_STRING) {
5050
len = strlen(val->s);
51-
else if (type == FMT_INT)
51+
} else if (type == FMT_INT) {
5252
len = snprintf(buf, sizeof(buf), "%d", val->i);
53-
else if (type == FMT_UNSIGNED)
53+
} else if (type == FMT_UNSIGNED) {
5454
len = snprintf(buf, sizeof(buf), "%u", val->u);
55-
else if (type == FMT_LONG)
55+
} else if (type == FMT_LONG) {
5656
len = snprintf(buf, sizeof(buf), "%ld", val->ld);
57-
else if (type == FMT_UNSIGNED_LONG)
57+
} else if (type == FMT_UNSIGNED_LONG) {
5858
len = snprintf(buf, sizeof(buf), "%lu", val->lu);
59-
else {
59+
} else {
6060
fprintf(stderr, "Invalid format!\n");
6161
return;
6262
}
@@ -70,15 +70,15 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ
7070

7171
/* print value */
7272
if (type == FMT_STRING)
73-
printf("%s ", val->s);
73+
printf("%s%s", val->s, add_pad ? "" : " ");
7474
else if (type == FMT_INT)
75-
printf("%d ", val->i);
75+
printf("%d%s", val->i, add_pad ? "" : " ");
7676
else if (type == FMT_UNSIGNED)
77-
printf("%u ", val->u);
77+
printf("%u%s", val->u, add_pad ? "" : " ");
7878
else if (type == FMT_LONG)
79-
printf("%ld ", val->ld);
79+
printf("%ld%s", val->ld, add_pad ? "" : " ");
8080
else if (type == FMT_UNSIGNED_LONG)
81-
printf("%lu", val->lu);
81+
printf("%lu%s", val->lu, add_pad ? "" : " ");
8282

8383
/* add right padding */
8484
for (i = 0; i < right_pad; i++)
@@ -90,8 +90,10 @@ static void table_print_columns(const struct table *t)
9090
int col, j, width;
9191
struct table_column *c;
9292
struct value v;
93+
bool last_col;
9394

9495
for (col = 0; col < t->num_columns; col++) {
96+
last_col = col == t->num_columns - 1 ? true : false;
9597
c = &t->columns[col];
9698
width = c->width;
9799
if (c->align == LEFT)
@@ -100,17 +102,20 @@ static void table_print_columns(const struct table *t)
100102
if (c->align == CENTERED) {
101103
v.s = c->name;
102104
v.align = c->align;
103-
table_print_centered(&v, width, FMT_STRING);
104-
} else
105-
printf("%*s ", width, c->name);
105+
table_print_centered(&v, width, FMT_STRING, !last_col);
106+
} else {
107+
printf("%*s%s", width, c->name, last_col ? "" : " ");
108+
}
106109
}
107110

108111
printf("\n");
109112

110113
for (col = 0; col < t->num_columns; col++) {
114+
last_col = col == t->num_columns - 1 ? true : false;
111115
for (j = 0; j < t->columns[col].width; j++)
112116
putchar('-');
113-
printf(" ");
117+
if (!last_col)
118+
printf(" ");
114119
}
115120

116121
printf("\n");
@@ -123,9 +128,11 @@ static void table_print_rows(const struct table *t)
123128
struct table_row *r;
124129
int width;
125130
struct value *v;
131+
bool last_col;
126132

127133
for (row = 0; row < t->num_rows; row++) {
128134
for (col = 0; col < t->num_columns; col++) {
135+
last_col = col == t->num_columns - 1 ? true : false;
129136
c = &t->columns[col];
130137
r = &t->rows[row];
131138
v = &r->val[col];
@@ -134,30 +141,25 @@ static void table_print_rows(const struct table *t)
134141
if (v->align == LEFT)
135142
width *= -1;
136143

137-
if (v->align == CENTERED)
138-
table_print_centered(v, width, v->type);
139-
else {
144+
if (v->align == CENTERED) {
145+
table_print_centered(v, width, v->type, !last_col);
146+
} else {
140147
switch (v->type) {
141148
case FMT_STRING:
142-
printf("%*s ", width, v->s);
149+
printf("%*s%s", width, v->s, last_col ? "" : " ");
143150
break;
144-
145151
case FMT_INT:
146-
printf("%*d ", width, v->i);
152+
printf("%*d%s", width, v->i, last_col ? "" : " ");
147153
break;
148-
149154
case FMT_UNSIGNED:
150-
printf("%*u ", width, v->u);
155+
printf("%*u%s", width, v->u, last_col ? "" : " ");
151156
break;
152-
153157
case FMT_LONG:
154-
printf("%*ld ", width, v->ld);
158+
printf("%*ld%s", width, v->ld, last_col ? "" : " ");
155159
break;
156-
157160
case FMT_UNSIGNED_LONG:
158-
printf("%*lu ", width, v->lu);
161+
printf("%*lu%s", width, v->lu, last_col ? "" : " ");
159162
break;
160-
161163
default:
162164
fprintf(stderr, "Invalid format!\n");
163165
break;

0 commit comments

Comments
 (0)