New --bar-style option; replace "default" with "plain" or with a lookup against the selected default, as appropriate.

This commit is contained in:
Andrew Wood
2024-12-14 00:06:30 +00:00
parent e3382b1a62
commit 8fa9df6e7a
21 changed files with 568 additions and 414 deletions
+1
View File
@@ -34,6 +34,7 @@ struct opts_s {
/*@keep@*/ const char *program_name; /* name the program is running as */
/*@keep@*/ /*@null@*/ char *output; /* fd to write output to */
/*@keep@*/ /*@null@*/ char *name; /* display name, if any */
/*@keep@*/ /*@null@*/ char *default_bar_style; /* default bar style */
/*@keep@*/ /*@null@*/ char *format; /* output format, if any */
/*@keep@*/ /*@null@*/ char *pidfile; /* PID file, if any */
/*@keep@*/ /*@null@*/ char *store_and_forward_file; /* store and forward file, if any */
+2
View File
@@ -154,6 +154,7 @@ struct pvstate_s {
/*@only@*/ /*@null@*/ char *format_string; /* output format string */
/*@only@*/ /*@null@*/ char *extra_format_string; /* extra format string */
/*@null@*/ char *output_name; /* name of the output, for diagnostics */
/*@null@*/ char *default_bar_style; /* which bar style to use by default */
off_t error_skip_block; /* skip block size, 0 for adaptive */
off_t rate_limit; /* rate limit, in bytes per second */
size_t target_buffer_size; /* buffer size (0=default) */
@@ -496,6 +497,7 @@ size_t pv_formatter_progress(pvformatter_args_t);
size_t pv_formatter_progress_bar_only(pvformatter_args_t);
size_t pv_formatter_progress_amount_only(pvformatter_args_t);
size_t pv_formatter_bar_default(pvformatter_args_t);
size_t pv_formatter_bar_plain(pvformatter_args_t);
size_t pv_formatter_bar_block(pvformatter_args_t);
size_t pv_formatter_bar_granular(pvformatter_args_t);
size_t pv_formatter_bar_shaded(pvformatter_args_t);
+1
View File
@@ -225,6 +225,7 @@ extern void pv_state_interval_set(pvstate_t, double);
extern void pv_state_width_set(pvstate_t, unsigned int, bool);
extern void pv_state_height_set(pvstate_t, unsigned int, bool);
extern void pv_state_name_set(pvstate_t, /*@null@*/ const char *);
extern void pv_state_default_bar_style_set(pvstate_t, /*@null@ */ const char *);
extern void pv_state_format_string_set(pvstate_t, /*@null@*/ const char *);
extern void pv_state_extra_display_set(pvstate_t, /*@null@*/ const char *);
extern void pv_state_watch_pid_set(pvstate_t, pid_t);
+3
View File
@@ -308,6 +308,9 @@ void display_help(void)
{ "-N", "--name", N_("NAME"),
N_("prefix visual information with NAME"),
{ 0, 0, 0, 0} },
{ "-u", "--bar-style", N_("STYLE"),
N_("set default bar style to NAME"),
{ 0, 0, 0, 0} },
{ "-x", "--extra-display", N_("SPEC"),
N_("also send progress to SPEC"),
{ 0, 0, 0, 0} },
+1
View File
@@ -502,6 +502,7 @@ int main(int argc, char **argv)
pv_state_no_splice_set(state, opts->no_splice);
pv_state_size_set(state, opts->size);
pv_state_name_set(state, opts->name);
pv_state_default_bar_style_set(state, opts->default_bar_style);
pv_state_format_string_set(state, opts->format);
pv_state_extra_display_set(state, opts->extra_display);
pv_state_watch_pid_set(state, opts->watch_pid);
+10 -1
View File
@@ -296,6 +296,7 @@ opts_t opts_parse(unsigned int argc, char **argv)
{ "width", 1, NULL, (int) 'w' },
{ "height", 1, NULL, (int) 'H' },
{ "name", 1, NULL, (int) 'N' },
{ "bar-style", 1, NULL, (int) 'u' },
{ "format", 1, NULL, (int) 'F' },
{ "extra-display", 1, NULL, (int) 'x' },
{ "stats", 0, NULL, (int) 'v' },
@@ -322,7 +323,7 @@ opts_t opts_parse(unsigned int argc, char **argv)
/*@+nullassign@ */
int option_index = 0;
#endif /* HAVE_GETOPT_LONG */
char *short_options = "hVpteIrab8kTA:fvnqcWD:s:gl0i:w:H:N:F:x:L:B:CEZ:SYKXU:R:P:d:m:o:"
char *short_options = "hVpteIrab8kTA:fvnqcWD:s:gl0i:w:H:N:u:F:x:L:B:CEZ:SYKXU:R:P:d:m:o:"
#ifdef ENABLE_DEBUGGING
"!:"
#endif
@@ -595,6 +596,14 @@ opts_t opts_parse(unsigned int argc, char **argv)
return NULL;
}
break;
case 'u':
opts->default_bar_style = pv_strdup(optarg);
if (NULL == opts->default_bar_style) {
fprintf(stderr, "%s: -u: %s\n", opts->program_name, strerror(errno));
opts_free(opts);
return NULL;
}
break;
case 'L':
opts->rate_limit = pv_getnum_size(optarg, opts->decimal_units);
break;
+1 -2
View File
@@ -541,8 +541,7 @@ static bool pv__format_numeric(pvstate_t state, pvdisplay_t display)
{ "{progress}", &pv_formatter_progress, true },
{ "{progress-amount-only}", &pv_formatter_progress_amount_only, false },
{ "{progress-bar-only}", &pv_formatter_bar_default, true },
{ "{bar}", &pv_formatter_bar_default, true },
{ "{bar-default}", &pv_formatter_bar_default, true },
{ "{bar-plain}", &pv_formatter_bar_plain, true },
{ "{bar-block}", &pv_formatter_bar_block, true },
{ "{bar-granular}", &pv_formatter_bar_granular, true },
{ "{bar-shaded}", &pv_formatter_bar_shaded, true },
+21 -6
View File
@@ -20,8 +20,8 @@
/*
* Populate "style" with the named bar style, falling back to the default if
* the name was not recognised. Returns true if the named style was found.
* Populate "style" with the named bar style, falling back to plain if the
* name was not recognised. Returns true if the named style was found.
*
* Note that strings are copied into the structure, rather than just
* updating pointers, to maintain separation of concern between different
@@ -96,7 +96,7 @@ static bool pv_barstyle(pvformatter_args_t args, pvbarstyle_t style, const char
return true;
}
/* Default style. */
/* Default plain style. */
style->style_id = 1;
@@ -108,7 +108,7 @@ static bool pv_barstyle(pvformatter_args_t args, pvbarstyle_t style, const char
style->filler_entries = 2;
if (0 == strcmp(name, "default"))
if (0 == strcmp(name, "plain"))
return true;
return false;
@@ -134,7 +134,7 @@ int pv_display_barstyle_index(pvformatter_args_t args, const char *name)
#ifdef ENABLE_DEBUGGING
found = pv_barstyle(args, &style, name);
if (!found)
debug("%s: %s", name, "bar style not found, using default");
debug("%s: %s", name, "bar style not found, using plain");
#else
(void) pv_barstyle(args, &style, name);
#endif
@@ -161,9 +161,24 @@ int pv_display_barstyle_index(pvformatter_args_t args, const char *name)
size_t pv_formatter_bar_default(pvformatter_args_t args)
{
if (0 == args->segment->parameter) {
const char *default_name;
default_name = args->state->control.default_bar_style;
/*@-branchstate@ */
if (NULL == default_name)
default_name = "plain";
/*@+branchstate@ */
/* splint - it doesn't matter that default_name may be static */
args->segment->parameter = 1 + pv_display_barstyle_index(args, default_name);
}
return pv_formatter_progress_bar_only(args);
}
size_t pv_formatter_bar_plain(pvformatter_args_t args)
{
if (0 == args->segment->parameter)
args->segment->parameter = 1 + pv_display_barstyle_index(args, "default");
args->segment->parameter = 1 + pv_display_barstyle_index(args, "plain");
return pv_formatter_progress_bar_only(args);
}
+20 -4
View File
@@ -297,8 +297,16 @@ size_t pv_formatter_progress(pvformatter_args_t args)
content[0] = '\0';
if (0 == args->segment->parameter)
args->segment->parameter = 1 + pv_display_barstyle_index(args, "default");
if (0 == args->segment->parameter) {
const char *default_name;
default_name = args->state->control.default_bar_style;
/*@-branchstate@ */
if (NULL == default_name)
default_name = "plain";
/*@+branchstate@ */
/* splint - it doesn't matter that default_name may be static */
args->segment->parameter = 1 + pv_display_barstyle_index(args, default_name);
}
if (0 == args->buffer_size)
return 0;
@@ -327,8 +335,16 @@ size_t pv_formatter_progress_bar_only(pvformatter_args_t args)
content[0] = '\0';
if (0 == args->segment->parameter)
args->segment->parameter = 1 + pv_display_barstyle_index(args, "default");
if (0 == args->segment->parameter) {
const char *default_name;
default_name = args->state->control.default_bar_style;
/*@-branchstate@ */
if (NULL == default_name)
default_name = "plain";
/*@+branchstate@ */
/* splint - it doesn't matter that default_name may be static */
args->segment->parameter = 1 + pv_display_barstyle_index(args, default_name);
}
if (0 == args->buffer_size)
return 0;
+15
View File
@@ -202,6 +202,11 @@ void pv_state_free(pvstate_t state)
state->control.name = NULL;
}
if (NULL != state->control.default_bar_style) {
free(state->control.default_bar_style);
state->control.default_bar_style = NULL;
}
if (NULL != state->control.format_string) {
free(state->control.format_string);
state->control.format_string = NULL;
@@ -429,6 +434,16 @@ void pv_state_name_set(pvstate_t state, /*@null@ */ const char *val)
state->control.name = pv_strdup(val);
}
void pv_state_default_bar_style_set(pvstate_t state, /*@null@ */ const char *val)
{
if (NULL != state->control.default_bar_style) {
free(state->control.default_bar_style);
state->control.default_bar_style = NULL;
}
if (NULL != val)
state->control.default_bar_style = pv_strdup(val);
}
void pv_state_format_string_set(pvstate_t state, /*@null@ */ const char *val)
{
if (NULL != state->control.format_string) {