Skip to content

Commit 4606d64

Browse files
ikegami-tigaw
authored andcommitted
util: move to add final padding printf between each columns
Delete the padding flag by padding in the caller functions. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 11c2a8d commit 4606d64

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

util/table.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ 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, bool add_pad)
44+
static void table_print_centered(struct value *val, int width, enum fmt_type type)
4545
{
4646
int i, len, left_pad, right_pad;
4747
char buf[64];
@@ -77,19 +77,19 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ
7777
/* print value */
7878
switch (type) {
7979
case FMT_STRING:
80-
printf("%s%s", val->s, add_pad ? "" : " ");
80+
printf("%s", val->s);
8181
break;
8282
case FMT_INT:
83-
printf("%d%s", val->i, add_pad ? "" : " ");
83+
printf("%d", val->i);
8484
break;
8585
case FMT_UNSIGNED:
86-
printf("%u%s", val->u, add_pad ? "" : " ");
86+
printf("%u", val->u);
8787
break;
8888
case FMT_LONG:
89-
printf("%ld%s", val->ld, add_pad ? "" : " ");
89+
printf("%ld", val->ld);
9090
break;
9191
case FMT_UNSIGNED_LONG:
92-
printf("%lu%s", val->lu, add_pad ? "" : " ");
92+
printf("%lu", val->lu);
9393
break;
9494
default:
9595
break;
@@ -105,35 +105,34 @@ static void table_print_columns(const struct table *t)
105105
int col, j, width;
106106
struct table_column *c;
107107
struct value v;
108-
bool last_col;
109108

110109
for (col = 0; col < t->num_columns; col++) {
111-
last_col = col == t->num_columns - 1 ? true : false;
112110
c = &t->columns[col];
113111
width = c->width;
114112
switch (c->align) {
115113
case CENTERED:
116114
v.s = c->name;
117115
v.align = c->align;
118-
table_print_centered(&v, width, FMT_STRING, !last_col);
116+
table_print_centered(&v, width, FMT_STRING);
119117
break;
120118
case LEFT:
121119
width *= -1;
122120
fallthrough;
123121
default:
124-
printf("%*s%s", width, c->name, last_col ? "" : " ");
122+
printf("%*s", width, c->name);
125123
break;
126124
}
125+
if (col + 1 != t->num_columns)
126+
putchar(' ');
127127
}
128128

129129
printf("\n");
130130

131131
for (col = 0; col < t->num_columns; col++) {
132-
last_col = col == t->num_columns - 1 ? true : false;
133132
for (j = 0; j < t->columns[col].width; j++)
134133
putchar('-');
135-
if (!last_col)
136-
printf(" ");
134+
if (col + 1 != t->num_columns)
135+
putchar(' ');
137136
}
138137

139138
printf("\n");
@@ -146,46 +145,46 @@ static void table_print_rows(const struct table *t)
146145
struct table_row *r;
147146
int width;
148147
struct value *v;
149-
bool last_col;
150148

151149
for (row = 0; row < t->num_rows; row++) {
152150
for (col = 0; col < t->num_columns; col++) {
153-
last_col = col == t->num_columns - 1 ? true : false;
154151
c = &t->columns[col];
155152
r = &t->rows[row];
156153
v = &r->val[col];
157154

158155
width = c->width;
159156
switch (v->align) {
160157
case CENTERED:
161-
table_print_centered(v, width, v->type, !last_col);
158+
table_print_centered(v, width, v->type);
162159
break;
163160
case LEFT:
164161
width *= -1;
165162
fallthrough;
166163
default:
167164
switch (v->type) {
168165
case FMT_STRING:
169-
printf("%*s%s", width, v->s, last_col ? "" : " ");
166+
printf("%*s", width, v->s);
170167
break;
171168
case FMT_INT:
172-
printf("%*d%s", width, v->i, last_col ? "" : " ");
169+
printf("%*d", width, v->i);
173170
break;
174171
case FMT_UNSIGNED:
175-
printf("%*u%s", width, v->u, last_col ? "" : " ");
172+
printf("%*u", width, v->u);
176173
break;
177174
case FMT_LONG:
178-
printf("%*ld%s", width, v->ld, last_col ? "" : " ");
175+
printf("%*ld", width, v->ld);
179176
break;
180177
case FMT_UNSIGNED_LONG:
181-
printf("%*lu%s", width, v->lu, last_col ? "" : " ");
178+
printf("%*lu", width, v->lu);
182179
break;
183180
default:
184181
fprintf(stderr, "Invalid format!\n");
185182
break;
186183
}
187184
break;
188185
}
186+
if (col + 1 != t->num_columns)
187+
putchar(' ');
189188
}
190189
printf("\n");
191190
}

0 commit comments

Comments
 (0)