-
Notifications
You must be signed in to change notification settings - Fork 697
List generic table #2924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
List generic table #2924
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
40cc867
util: add table_init_with_columns() function
ikegami-t 3970e96
util: allow table_add_columns() to set column width
ikegami-t ffe640a
util: remove unnecessary row end padding print
ikegami-t d85e800
nvme-print-stdout: replace nvme simple list with generic table
ikegami-t 443c9ae
util: use switch case statements instead of repeated else if
ikegami-t 68bfb16
util: move to add final padding printf between each columns
ikegami-t 747a6e2
util: rename table_init() to table_create()
ikegami-t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,17 +46,23 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ | |
| int i, len, left_pad, right_pad; | ||
| char buf[64]; | ||
|
|
||
| if (type == FMT_STRING) | ||
| switch (type) { | ||
| case FMT_STRING: | ||
| len = strlen(val->s); | ||
| else if (type == FMT_INT) | ||
| break; | ||
| case FMT_INT: | ||
| len = snprintf(buf, sizeof(buf), "%d", val->i); | ||
| else if (type == FMT_UNSIGNED) | ||
| break; | ||
| case FMT_UNSIGNED: | ||
| len = snprintf(buf, sizeof(buf), "%u", val->u); | ||
| else if (type == FMT_LONG) | ||
| break; | ||
| case FMT_LONG: | ||
| len = snprintf(buf, sizeof(buf), "%ld", val->ld); | ||
| else if (type == FMT_UNSIGNED_LONG) | ||
| break; | ||
| case FMT_UNSIGNED_LONG: | ||
| len = snprintf(buf, sizeof(buf), "%lu", val->lu); | ||
| else { | ||
| break; | ||
| default: | ||
| fprintf(stderr, "Invalid format!\n"); | ||
| return; | ||
| } | ||
|
|
@@ -69,16 +75,25 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ | |
| putchar(' '); | ||
|
|
||
| /* print value */ | ||
| if (type == FMT_STRING) | ||
| printf("%s ", val->s); | ||
| else if (type == FMT_INT) | ||
| printf("%d ", val->i); | ||
| else if (type == FMT_UNSIGNED) | ||
| printf("%u ", val->u); | ||
| else if (type == FMT_LONG) | ||
| printf("%ld ", val->ld); | ||
| else if (type == FMT_UNSIGNED_LONG) | ||
| switch (type) { | ||
| case FMT_STRING: | ||
| printf("%s", val->s); | ||
| break; | ||
| case FMT_INT: | ||
| printf("%d", val->i); | ||
| break; | ||
| case FMT_UNSIGNED: | ||
| printf("%u", val->u); | ||
| break; | ||
| case FMT_LONG: | ||
| printf("%ld", val->ld); | ||
| break; | ||
| case FMT_UNSIGNED_LONG: | ||
| printf("%lu", val->lu); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| /* add right padding */ | ||
| for (i = 0; i < right_pad; i++) | ||
|
|
@@ -94,23 +109,30 @@ static void table_print_columns(const struct table *t) | |
| for (col = 0; col < t->num_columns; col++) { | ||
| c = &t->columns[col]; | ||
| width = c->width; | ||
| if (c->align == LEFT) | ||
| width *= -1; | ||
|
|
||
| if (c->align == CENTERED) { | ||
| switch (c->align) { | ||
| case CENTERED: | ||
| v.s = c->name; | ||
| v.align = c->align; | ||
| table_print_centered(&v, width, FMT_STRING); | ||
| } else | ||
| printf("%*s ", width, c->name); | ||
| break; | ||
| case LEFT: | ||
| width *= -1; | ||
| fallthrough; | ||
| default: | ||
| printf("%*s", width, c->name); | ||
| break; | ||
| } | ||
| if (col + 1 != t->num_columns) | ||
| putchar(' '); | ||
| } | ||
|
|
||
| printf("\n"); | ||
|
|
||
| for (col = 0; col < t->num_columns; col++) { | ||
| for (j = 0; j < t->columns[col].width; j++) | ||
| putchar('-'); | ||
| printf(" "); | ||
| if (col + 1 != t->num_columns) | ||
| putchar(' '); | ||
| } | ||
|
|
||
| printf("\n"); | ||
|
|
@@ -131,38 +153,38 @@ static void table_print_rows(const struct table *t) | |
| v = &r->val[col]; | ||
|
|
||
| width = c->width; | ||
| if (v->align == LEFT) | ||
| width *= -1; | ||
|
|
||
| if (v->align == CENTERED) | ||
| switch (v->align) { | ||
| case CENTERED: | ||
| table_print_centered(v, width, v->type); | ||
| else { | ||
| break; | ||
| case LEFT: | ||
| width *= -1; | ||
| fallthrough; | ||
| default: | ||
| switch (v->type) { | ||
| case FMT_STRING: | ||
| printf("%*s ", width, v->s); | ||
| printf("%*s", width, v->s); | ||
| break; | ||
|
|
||
| case FMT_INT: | ||
| printf("%*d ", width, v->i); | ||
| printf("%*d", width, v->i); | ||
| break; | ||
|
|
||
| case FMT_UNSIGNED: | ||
| printf("%*u ", width, v->u); | ||
| printf("%*u", width, v->u); | ||
| break; | ||
|
|
||
| case FMT_LONG: | ||
| printf("%*ld ", width, v->ld); | ||
| printf("%*ld", width, v->ld); | ||
| break; | ||
|
|
||
| case FMT_UNSIGNED_LONG: | ||
| printf("%*lu ", width, v->lu); | ||
| printf("%*lu", width, v->lu); | ||
| break; | ||
|
|
||
| default: | ||
| fprintf(stderr, "Invalid format!\n"); | ||
| break; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here Just add a final padding printf if it is needed. |
||
| } | ||
| break; | ||
| } | ||
| if (col + 1 != t->num_columns) | ||
| putchar(' '); | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
@@ -209,15 +231,23 @@ void table_add_row(struct table *t, int row_id) | |
| } | ||
| } | ||
|
|
||
| struct table *table_init(void) | ||
| struct table *table_create(void) | ||
| { | ||
| struct table *t; | ||
| return calloc(1, sizeof(struct table)); | ||
| } | ||
|
|
||
| struct table *table_init_with_columns(struct table_column *c, int num_columns) | ||
| { | ||
| struct table *t = table_create(); | ||
|
|
||
| t = malloc(sizeof(struct table)); | ||
| if (!t) | ||
| return NULL; | ||
|
|
||
| memset(t, 0, sizeof(struct table)); | ||
| if (table_add_columns(t, c, num_columns)) { | ||
| table_free(t); | ||
| return NULL; | ||
| } | ||
|
|
||
| return t; | ||
| } | ||
|
|
||
|
|
@@ -278,7 +308,10 @@ int table_add_columns(struct table *t, struct table_column *c, int num_columns) | |
| goto free_col; | ||
|
|
||
| t->columns[col].align = c[col].align; | ||
| t->columns[col].width = strlen(t->columns[col].name); | ||
| if (c[col].width > strlen(t->columns[col].name)) | ||
| t->columns[col].width = c[col].width; | ||
| else | ||
| t->columns[col].width = strlen(t->columns[col].name); | ||
| } | ||
| t->num_columns = num_columns; | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say when the table allocation fails just error out. no need for a fallback mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No this is not for the allocation error but for the
zns listplugin command. The print function used by the both commands but currently the generic table is used by only thelistcommand. (Later I will do chnage thezns listcommand also to use the generic table.)