1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "id128-print.h"
10 #include "pretty-print.h"
11 #include "terminal-util.h"
13 int id128_pretty_print(sd_id128_t id
, Id128PrettyPrintMode mode
) {
14 _cleanup_free_
char *man_link
= NULL
, *mod_link
= NULL
;
19 assert(mode
< _ID128_PRETTY_PRINT_MODE_MAX
);
21 if (mode
== ID128_PRINT_ID128
) {
22 printf(SD_ID128_FORMAT_STR
"\n",
23 SD_ID128_FORMAT_VAL(id
));
25 } else if (mode
== ID128_PRINT_UUID
) {
26 printf(SD_ID128_UUID_FORMAT_STR
"\n",
27 SD_ID128_FORMAT_VAL(id
));
31 on
= ansi_highlight();
34 if (terminal_urlify("man:systemd-id128(1)", "systemd-id128(1)", &man_link
) < 0)
37 if (terminal_urlify("https://docs.python.org/3/library/uuid.html", "uuid", &mod_link
) < 0)
41 "%s" SD_ID128_FORMAT_STR
"%s\n\n"
43 "%s" SD_ID128_UUID_FORMAT_STR
"%s\n\n"
45 "%s#define XYZ SD_ID128_MAKE(",
46 on
, SD_ID128_FORMAT_VAL(id
), off
,
47 on
, SD_ID128_FORMAT_VAL(id
), off
,
50 for (i
= 0; i
< 16; i
++)
51 printf("%02x%s", id
.bytes
[i
], i
!= 15 ? "," : "");
52 printf(")%s\n\n", off
);
54 printf("As Python constant:\n"
56 ">>> %sXYZ = uuid.UUID('" SD_ID128_FORMAT_STR
"')%s\n",
58 on
, SD_ID128_FORMAT_VAL(id
), off
);
63 int id128_print_new(Id128PrettyPrintMode mode
) {
67 r
= sd_id128_randomize(&id
);
69 return log_error_errno(r
, "Failed to generate ID: %m");
71 return id128_pretty_print(id
, mode
);