#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,
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;
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) {
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);
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);
#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);
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
}