terminal-util: add a function that shows a pretty separator line

Follow-up for #8824
This commit is contained in:
Lennart Poettering 2018-05-10 11:28:33 -07:00 committed by Zbigniew Jędrzejewski-Szmek
parent e4915c2797
commit cb91deaf77
4 changed files with 27 additions and 4 deletions

View File

@ -1322,10 +1322,7 @@ static int cat_config(int argc, char *argv[], void *userdata) {
const char *t = NULL;
if (arg != argv + 1)
printf("%s%*s%s\n\n",
ansi_underline(),
columns(), "",
ansi_normal());
print_separator();
if (path_is_absolute(*arg)) {
const char *dir;

View File

@ -1418,3 +1418,25 @@ int cat_files(const char *file, char **dropins, CatFlags flags) {
return 0;
}
void print_separator(void) {
/* Outputs a separator line that resolves to whitespace when copied from the terminal. We do that by outputting
* one line filled with spaces with ANSI underline set, followed by a second (empty) line. */
if (underline_enabled()) {
size_t i, c;
c = columns();
flockfile(stdout);
fputs_unlocked(ANSI_UNDERLINE, stdout);
for (i = 0; i < c; i++)
fputc_unlocked(' ', stdout);
fputs_unlocked(ANSI_NORMAL "\n\n", stdout);
funlockfile(stdout);
} else
fputs("\n\n", stdout);
}

View File

@ -166,3 +166,5 @@ typedef enum CatFlags {
} CatFlags;
int cat_files(const char *file, char **dropins, CatFlags flags);
void print_separator(void);

View File

@ -94,5 +94,7 @@ int main(int argc, char *argv[]) {
test_terminal_urlify();
test_cat_files();
print_separator();
return 0;
}