List all supported format sequences in the output of "--help".

This commit is contained in:
Andrew Wood
2024-12-08 20:40:05 +00:00
parent fb2e6b4074
commit f495303177
11 changed files with 129 additions and 40 deletions
+5
View File
@@ -275,6 +275,11 @@ extern void pv_sig_fini(pvstate_t);
*/
extern void pv_state_free(/*@only@*/ pvstate_t);
/*
* Return a string listing all supported format sequences.
*/
/*@null@*/ char *pv_format_sequences(void);
#if ! HAVE_SETPROCTITLE
void initproctitle(int, char **);
void setproctitle(const char *, ...);
+16
View File
@@ -434,6 +434,7 @@ void display_help(void)
size_t right_margin = 77;
const char *program_description;
const char *bug_report_note;
char *format_sequences = NULL;
unsigned int terminal_width = 0, terminal_height = 0;
pv_screensize(&terminal_width, &terminal_height);
@@ -593,6 +594,21 @@ void display_help(void)
printf("\n");
}
format_sequences = pv_format_sequences();
if (NULL != format_sequences) {
const char *format_sequence_header;
format_sequence_header = _("Supported format sequences:");
if (NULL != format_sequence_header) {
printf("\n");
display_word_wrap(format_sequence_header, right_margin, 0);
printf("\n");
}
printf("\n ");
display_word_wrap(format_sequences, right_margin, 2);
free(format_sequences);
printf("\n");
}
printf("\n");
bug_report_note = _("Please report any bugs to: %s");
+36
View File
@@ -1329,6 +1329,42 @@ static struct pvdisplay_component_s {
};
/*
* Return a pointer to a malloc()ed string containing a space-separated list
* of all supported format sequences. The caller should free() it.
*/
/*@null@ */
char *pv_format_sequences(void)
{
size_t component_idx, buffer_size, offset;
char *buffer;
buffer_size = 0;
for (component_idx = 0; NULL != format_component[component_idx].match; component_idx++) {
size_t component_sequence_length = strlen(format_component[component_idx].match); /* flawfinder: ignore */
/* flawfinder - static strings, guaranteed null-terminated. */
buffer_size += 2 + component_sequence_length; /* 2 for '%' + ' ' */
}
buffer = malloc(buffer_size + 1);
if (NULL == buffer)
return NULL;
offset = 0;
for (component_idx = 0; NULL != format_component[component_idx].match; component_idx++) {
size_t component_sequence_length = strlen(format_component[component_idx].match); /* flawfinder: ignore - as above */
if (0 != offset)
buffer[offset++] = ' ';
buffer[offset++] = '%';
memmove(buffer + offset, format_component[component_idx].match, component_sequence_length);
offset += component_sequence_length;
}
buffer[offset] = '\0';
return buffer;
}
/*
* Initialise the output format structure, based on the current options.
*/