bectl(8): Take -H parameter to list into account

-H is for a scripted format, where all fields are tab-delimited and the
headers go away. We accomplish this by splitting out pad printing to a
separate function that'll take into account whether we're supposed to be
scripted or not.

This has the nice side effect of maintaining positive column sizes again.
This commit is contained in:
Kyle Evans 2018-08-05 04:16:00 +00:00
parent 3443420646
commit f97b318f7d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/projects/bectl/; revision=337341

View file

@ -59,7 +59,7 @@ struct printc {
int current_indent;
int mount_colsz;
int space_colsz;
bool hide_headers;
bool script_fmt;
bool show_all_datasets;
bool show_snaps;
bool show_space;
@ -73,6 +73,7 @@ static int bectl_cmd_import(int argc, char *argv[]);
static int bectl_cmd_add(int argc, char *argv[]);
static int bectl_cmd_jail(int argc, char *argv[]);
static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops);
static void print_padding(const char *fval, int colsz, struct printc *pc);
static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc);
static void print_headers(nvlist_t *props, struct printc *pc);
static int bectl_cmd_list(int argc, char *argv[]);
@ -456,6 +457,19 @@ get_origin_props(nvlist_t *dsprops, nvlist_t **originprops)
return (NULL);
}
static void
print_padding(const char *fval, int colsz, struct printc *pc)
{
if (pc->script_fmt) {
printf("\t");
return;
}
if (fval != NULL)
colsz -= strlen(fval);
printf("%*s ", colsz, "");
}
static void
print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
@ -470,8 +484,7 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
boolean_t active_now, active_reboot;
originprops = NULL;
printf("%*s%*s ", pc->current_indent, "",
pc->be_colsz + pc->current_indent, name);
printf("%*s%s", pc->current_indent, "", name);
/* Recurse at the base level if we're breaking info down */
if (pc->current_indent == 0 && (pc->show_all_datasets ||
@ -489,7 +502,8 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
}
pc->current_indent = 0;
return;
}
} else
print_padding(name, pc->be_colsz - pc->current_indent, pc);
active_colsz = pc->active_colsz_def;
if (nvlist_lookup_boolean_value(dsprops, "active",
@ -506,11 +520,14 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
printf("-");
active_colsz--;
}
printf("%*s ", -active_colsz, " ");
if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0)
printf("%*s ", pc->mount_colsz, propstr);
else
printf("%*s ", pc->mount_colsz, "-");
print_padding(NULL, active_colsz, pc);
if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0){
printf("%s", propstr);
print_padding(propstr, pc->mount_colsz, pc);
} else {
printf("%s", "-");
print_padding("-", pc->mount_colsz, pc);
}
get_origin_props(dsprops, &originprops);
@ -524,9 +541,12 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
/* Alas, there's more to it,. */
humanize_number(buf, 6, space, "", HN_AUTOSCALE,
HN_DECIMAL | HN_NOSPACE | HN_B);
printf("%*s ", pc->space_colsz, buf);
} else
printf("%*s ", pc->space_colsz, "-");
printf("%s", buf);
print_padding(buf, pc->space_colsz, pc);
} else {
printf("%s", "-");
print_padding("-", pc->space_colsz, pc);
}
if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
ctimenum = strtoull(propstr, NULL, 10);
@ -573,13 +593,11 @@ print_headers(nvlist_t *props, struct printc *pc)
strlen(propstr) + INDENT_INCREMENT * 2);
}
pc->be_colsz = -be_maxcol;
/* To be made negative after calculating final col sz */
pc->be_colsz = be_maxcol;
pc->active_colsz_def = strlen(HEADER_ACTIVE);
pc->mount_colsz = -(int)strlen(HEADER_MOUNT);
pc->space_colsz = -(int)strlen(HEADER_SPACE);
/* XXX TODO: Take -H into account */
printf("%*s %s %s %s %s\n", pc->be_colsz, chosen_be_header,
pc->mount_colsz = strlen(HEADER_MOUNT);
pc->space_colsz = strlen(HEADER_SPACE);
printf("%*s %s %s %s %s\n", -pc->be_colsz, chosen_be_header,
HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
/*
@ -612,7 +630,7 @@ bectl_cmd_list(int argc, char *argv[])
pc.show_space = true;
break;
case 'H':
pc.hide_headers = true;
pc.script_fmt = true;
break;
case 's':
pc.show_snaps = true;
@ -641,7 +659,8 @@ bectl_cmd_list(int argc, char *argv[])
return (1);
}
print_headers(props, &pc);
if (!pc.script_fmt)
print_headers(props, &pc);
/* Do a first pass to print active and next active first */
for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
cur = nvlist_next_nvpair(props, cur)) {