journalctl: don't print QR codes if we can't/shan't use colors

We need them to display things properly, hence treat color-less
terminals the same way as UTF-8-less systems: avoid the QR code.
This commit is contained in:
Lennart Poettering
2020-06-25 09:28:18 +02:00
parent 9bff141052
commit 0523022d5b
3 changed files with 22 additions and 12 deletions
+16 -6
View File
@@ -10,27 +10,29 @@
#include "fd-util.h"
#include "fileio.h"
#include "journal-qrcode.h"
#include "locale-util.h"
#include "macro.h"
#include "terminal-util.h"
#define WHITE_ON_BLACK "\033[40;37;1m"
#define NORMAL "\033[0m"
#define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
static void print_border(FILE *output, unsigned width) {
unsigned x, y;
/* Four rows of border */
for (y = 0; y < 4; y += 2) {
fputs(WHITE_ON_BLACK, output);
fputs(ANSI_WHITE_ON_BLACK, output);
for (x = 0; x < 4 + width + 4; x++)
fputs("\342\226\210", output);
fputs(NORMAL "\n", output);
fputs(ANSI_NORMAL "\n", output);
}
}
int print_qr_code(
FILE *output,
const char *prefix_text,
const void *seed,
size_t seed_size,
uint64_t start,
@@ -48,6 +50,11 @@ int print_qr_code(
assert(seed);
assert(seed_size > 0);
/* If this is not an UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
* codes */
if (!is_locale_utf8() || !colors_enabled())
return -EOPNOTSUPP;
f = open_memstream_unlocked(&url, &url_size);
if (!f)
return -ENOMEM;
@@ -78,6 +85,9 @@ int print_qr_code(
if (!qr)
return -ENOMEM;
if (prefix_text)
fputs(prefix_text, output);
print_border(output, qr->width);
for (y = 0; y < (unsigned) qr->width; y += 2) {
@@ -86,7 +96,7 @@ int print_qr_code(
row1 = qr->data + qr->width * y;
row2 = row1 + qr->width;
fputs(WHITE_ON_BLACK, output);
fputs(ANSI_WHITE_ON_BLACK, output);
for (x = 0; x < 4; x++)
fputs("\342\226\210", output);
@@ -108,7 +118,7 @@ int print_qr_code(
for (x = 0; x < 4; x++)
fputs("\342\226\210", output);
fputs(NORMAL "\n", output);
fputs(ANSI_NORMAL "\n", output);
}
print_border(output, qr->width);
+1 -1
View File
@@ -6,4 +6,4 @@
#include "sd-id128.h"
int print_qr_code(FILE *f, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine);
int print_qr_code(FILE *f, const char *prefix_text, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine);
+5 -5
View File
@@ -1922,11 +1922,11 @@ static int setup_keys(void) {
fprintf(stderr, "\nThe keys have been generated for host " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(machine));
#if HAVE_QRENCODE
/* If this is not an UTF-8 system don't print any QR codes */
if (is_locale_utf8()) {
fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr);
print_qr_code(stderr, seed, seed_size, n, arg_interval, hn, machine);
}
(void) print_qr_code(stderr,
"\nTo transfer the verification key to your phone please scan the QR code below:\n\n",
seed, seed_size,
n, arg_interval,
hn, machine);
#endif
}