| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <sys/utsname.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "color-util.h" |
| 9 |
#include "conf-files.h" |
| 10 |
#include "constants.h" |
| 11 |
#include "env-util.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "fileio.h" |
| 15 |
#include "log.h" |
| 16 |
#include "path-util.h" |
| 17 |
#include "pretty-print.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "terminal-util.h" |
| 21 |
#include "utf8.h" |
| 22 |
|
| 23 |
void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { |
| 24 |
char *p = buffer; |
| 25 |
|
| 26 |
assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); |
| 27 |
assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ |
| 28 |
|
| 29 |
if (pos > 1) { |
| 30 |
if (pos > 2) |
| 31 |
p = mempset(p, ' ', pos-2); |
| 32 |
if (log_get_show_color()) |
| 33 |
p = stpcpy(p, ANSI_RED); |
| 34 |
*p++ = '*'; |
| 35 |
} |
| 36 |
|
| 37 |
if (pos > 0 && pos <= width) { |
| 38 |
if (log_get_show_color()) |
| 39 |
p = stpcpy(p, ANSI_HIGHLIGHT_RED); |
| 40 |
*p++ = '*'; |
| 41 |
} |
| 42 |
|
| 43 |
if (log_get_show_color()) |
| 44 |
p = stpcpy(p, ANSI_NORMAL); |
| 45 |
|
| 46 |
if (pos < width) { |
| 47 |
if (log_get_show_color()) |
| 48 |
p = stpcpy(p, ANSI_RED); |
| 49 |
*p++ = '*'; |
| 50 |
if (pos < width-1) |
| 51 |
p = mempset(p, ' ', width-1-pos); |
| 52 |
if (log_get_show_color()) |
| 53 |
p = stpcpy(p, ANSI_NORMAL); |
| 54 |
} |
| 55 |
|
| 56 |
*p = '\0'; |
| 57 |
} |
| 58 |
|
| 59 |
bool urlify_enabled(void) { |
| 60 |
#if ENABLE_URLIFY |
| 61 |
static int cached_urlify_enabled = -1; |
| 62 |
|
| 63 |
if (cached_urlify_enabled < 0) { |
| 64 |
int val; |
| 65 |
|
| 66 |
val = getenv_bool("SYSTEMD_URLIFY"); |
| 67 |
if (val >= 0) |
| 68 |
cached_urlify_enabled = val; |
| 69 |
else |
| 70 |
cached_urlify_enabled = colors_enabled(); |
| 71 |
} |
| 72 |
|
| 73 |
return cached_urlify_enabled; |
| 74 |
#else |
| 75 |
return 0; |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
static bool url_suitable_for_osc8(const char *url) { |
| 80 |
assert(url); |
| 81 |
|
| 82 |
/* Not all URLs are safe for inclusion in OSC 8 due to charset and length restrictions. Let's detect |
| 83 |
* which ones those are */ |
| 84 |
|
| 85 |
/* If the URL is longer than 2K let's not try to do OSC 8. As per recommendation in |
| 86 |
* https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#length-limits */ |
| 87 |
if (strlen(url) > 2000) |
| 88 |
return false; |
| 89 |
|
| 90 |
/* OSC sequences may only contain chars from the 32..126 range, as per ECMA-48 */ |
| 91 |
for (const char *c = url; *c; c++) |
| 92 |
if (!osc_char_is_valid(*c)) |
| 93 |
return false; |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
int terminal_urlify(const char *url, const char *text, char **ret) { |
| 99 |
char *n; |
| 100 |
|
| 101 |
assert(url); |
| 102 |
assert(ret); |
| 103 |
|
| 104 |
/* Takes a URL and a pretty string and formats it as clickable link for the terminal. See |
| 105 |
* https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details. */ |
| 106 |
|
| 107 |
if (isempty(text)) |
| 108 |
text = url; |
| 109 |
|
| 110 |
if (urlify_enabled() && url_suitable_for_osc8(url)) |
| 111 |
n = strjoin(ANSI_OSC "8;;", url, ANSI_ST, |
| 112 |
text, |
| 113 |
ANSI_OSC "8;;" ANSI_ST); |
| 114 |
else |
| 115 |
n = strdup(text); |
| 116 |
if (!n) |
| 117 |
return -ENOMEM; |
| 118 |
|
| 119 |
*ret = n; |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
int file_url_from_path(const char *path, char **ret) { |
| 124 |
_cleanup_free_ char *absolute = NULL; |
| 125 |
struct utsname u; |
| 126 |
char *url = NULL; |
| 127 |
int r; |
| 128 |
|
| 129 |
assert(ret); |
| 130 |
|
| 131 |
if (uname(&u) < 0) |
| 132 |
return -errno; |
| 133 |
|
| 134 |
if (!path_is_absolute(path)) { |
| 135 |
r = path_make_absolute_cwd(path, &absolute); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
path = absolute; |
| 140 |
} |
| 141 |
|
| 142 |
/* As suggested by https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda, let's include the local |
| 143 |
* hostname here. Note that we don't use gethostname_malloc() or gethostname_strict() since we are interested |
| 144 |
* in the raw string the kernel has set, whatever it may be, under the assumption that terminals are not overly |
| 145 |
* careful with validating the strings either. */ |
| 146 |
|
| 147 |
url = strjoin("file://", u.nodename, path); |
| 148 |
if (!url) |
| 149 |
return -ENOMEM; |
| 150 |
|
| 151 |
*ret = url; |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
int terminal_urlify_path(const char *path, const char *text, char **ret) { |
| 156 |
_cleanup_free_ char *url = NULL; |
| 157 |
int r; |
| 158 |
|
| 159 |
assert(path); |
| 160 |
|
| 161 |
/* Much like terminal_urlify() above, but takes a file system path as input |
| 162 |
* and turns it into a proper file:// URL first. */ |
| 163 |
|
| 164 |
if (isempty(path)) |
| 165 |
return -EINVAL; |
| 166 |
|
| 167 |
if (isempty(text)) |
| 168 |
text = path; |
| 169 |
|
| 170 |
if (!urlify_enabled()) |
| 171 |
return strdup_to(ret, text); |
| 172 |
|
| 173 |
r = file_url_from_path(path, &url); |
| 174 |
if (r < 0) |
| 175 |
return r; |
| 176 |
|
| 177 |
return terminal_urlify(url, text, ret); |
| 178 |
} |
| 179 |
|
| 180 |
int terminal_urlify_man_full(const char *page, const char *section, const char *suffix, char **ret) { |
| 181 |
const char *url, *text; |
| 182 |
|
| 183 |
url = strjoina("man:", page, "(", section, ")"); |
| 184 |
text = strjoina(page, "(", section, ")", suffix); |
| 185 |
|
| 186 |
return terminal_urlify(url, text, ret); |
| 187 |
} |
| 188 |
|
| 189 |
static int cat_file(const ConfFile *c, bool *newline, CatFlags flags) { |
| 190 |
_cleanup_fclose_ FILE *f = NULL; |
| 191 |
_cleanup_free_ char *urlified = NULL, *section = NULL, *old_section = NULL; |
| 192 |
int r; |
| 193 |
|
| 194 |
assert(c); |
| 195 |
assert(c->original_path); |
| 196 |
assert(c->resolved_path); |
| 197 |
assert(c->fd >= 0); |
| 198 |
|
| 199 |
if (newline) { |
| 200 |
if (*newline) |
| 201 |
putc('\n', stdout); |
| 202 |
*newline = true; |
| 203 |
} |
| 204 |
|
| 205 |
bool resolved = !path_equal(c->original_path, c->resolved_path); |
| 206 |
|
| 207 |
r = terminal_urlify_path(c->resolved_path, NULL, &urlified); |
| 208 |
if (r < 0) |
| 209 |
return log_error_errno(r, "Failed to urlify path \"%s\": %m", c->resolved_path); |
| 210 |
|
| 211 |
printf("%s# %s%s%s%s\n", |
| 212 |
ansi_highlight_blue(), |
| 213 |
resolved ? c->original_path : "", |
| 214 |
resolved ? " -> " : "", |
| 215 |
urlified, |
| 216 |
ansi_normal()); |
| 217 |
|
| 218 |
f = fopen(FORMAT_PROC_FD_PATH(c->fd), "re"); |
| 219 |
if (!f) |
| 220 |
return log_error_errno(errno, "Failed to open \"%s\": %m", c->resolved_path); |
| 221 |
|
| 222 |
for (bool continued = false;;) { |
| 223 |
_cleanup_free_ char *line = NULL; |
| 224 |
|
| 225 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 226 |
if (r < 0) |
| 227 |
return log_error_errno(r, "Failed to read \"%s\": %m", c->resolved_path); |
| 228 |
if (r == 0) |
| 229 |
break; |
| 230 |
|
| 231 |
const char *l = skip_leading_chars(line, WHITESPACE); |
| 232 |
|
| 233 |
/* comment */ |
| 234 |
if (*l != '\0' && strchr(COMMENTS, *l)) { |
| 235 |
if (!FLAGS_SET(flags, CAT_TLDR)) |
| 236 |
printf("%s%s%s\n", ansi_highlight_grey(), line, ansi_normal()); |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
/* empty line */ |
| 241 |
if (FLAGS_SET(flags, CAT_TLDR) && (isempty(l) || streq(l, "\\"))) |
| 242 |
continue; |
| 243 |
|
| 244 |
/* section */ |
| 245 |
if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS) && *l == '[' && !continued) { |
| 246 |
if (FLAGS_SET(flags, CAT_TLDR)) |
| 247 |
/* On TLDR, let's not print it yet. */ |
| 248 |
free_and_replace(section, line); |
| 249 |
else |
| 250 |
printf("%s%s%s\n", ansi_highlight_cyan(), line, ansi_normal()); |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
/* normal line */ |
| 255 |
|
| 256 |
/* Before we print the line, print the last section header. */ |
| 257 |
if (FLAGS_SET(flags, CAT_TLDR) && section) { |
| 258 |
/* Do not print redundant section headers */ |
| 259 |
if (!streq_ptr(section, old_section)) |
| 260 |
printf("%s%s%s\n", ansi_highlight_cyan(), section, ansi_normal()); |
| 261 |
|
| 262 |
free_and_replace(old_section, section); |
| 263 |
} |
| 264 |
|
| 265 |
/* Check if the line ends with a backslash. */ |
| 266 |
bool escaped = false; |
| 267 |
char *e; |
| 268 |
for (e = line; *e != '\0'; e++) { |
| 269 |
if (escaped) |
| 270 |
escaped = false; |
| 271 |
else if (*e == '\\') |
| 272 |
escaped = true; |
| 273 |
} |
| 274 |
|
| 275 |
/* Highlight the trailing backslash. */ |
| 276 |
if (escaped) { |
| 277 |
assert(e > line); |
| 278 |
*(e-1) = '\0'; |
| 279 |
|
| 280 |
if (!strextend(&line, ansi_highlight_red(), "\\", ansi_normal())) |
| 281 |
return log_oom(); |
| 282 |
} |
| 283 |
|
| 284 |
/* Highlight the left side (directive) of a Foo=bar assignment */ |
| 285 |
if (FLAGS_SET(flags, CAT_FORMAT_HAS_SECTIONS) && !continued) { |
| 286 |
const char *p = strchr(line, '='); |
| 287 |
if (p) { |
| 288 |
_cleanup_free_ char *directive = NULL; |
| 289 |
|
| 290 |
directive = strndup(line, p - line); |
| 291 |
if (!directive) |
| 292 |
return log_oom(); |
| 293 |
|
| 294 |
printf("%s%s=%s%s\n", ansi_highlight_green(), directive, ansi_normal(), p + 1); |
| 295 |
continued = escaped; |
| 296 |
continue; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/* Otherwise, print the line as is. */ |
| 301 |
printf("%s\n", line); |
| 302 |
continued = escaped; |
| 303 |
} |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
int cat_files_full(const ConfFile *file, ConfFile * const *dropins, size_t n_dropins, CatFlags flags) { |
| 309 |
bool newline = false; |
| 310 |
int ret = 0; |
| 311 |
|
| 312 |
assert(dropins || n_dropins == 0); |
| 313 |
|
| 314 |
if (file) |
| 315 |
ret = cat_file(file, &newline, flags); |
| 316 |
|
| 317 |
FOREACH_ARRAY(i, dropins, n_dropins) |
| 318 |
RET_GATHER(ret, cat_file(*i, &newline, flags)); |
| 319 |
|
| 320 |
return ret; |
| 321 |
} |
| 322 |
|
| 323 |
static int cat_file_by_path(const char *p, bool *newline, CatFlags flags) { |
| 324 |
_cleanup_(conf_file_freep) ConfFile *c = NULL; |
| 325 |
int r; |
| 326 |
|
| 327 |
assert(p); |
| 328 |
|
| 329 |
r = conf_file_new(p, /* root= */ NULL, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN, &c); |
| 330 |
if (r == -ERFKILL) { /* masked */ |
| 331 |
if (newline) { |
| 332 |
if (*newline) |
| 333 |
putc('\n', stdout); |
| 334 |
*newline = true; |
| 335 |
} |
| 336 |
|
| 337 |
printf("%s# %s is a mask.%s\n", |
| 338 |
ansi_highlight_magenta(), |
| 339 |
p, |
| 340 |
ansi_normal()); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
if (r < 0) |
| 344 |
return r; |
| 345 |
|
| 346 |
return cat_file(c, newline, flags); |
| 347 |
} |
| 348 |
|
| 349 |
int cat_files(const char *file, char **dropins, CatFlags flags) { |
| 350 |
bool newline = false; |
| 351 |
int ret = 0; |
| 352 |
|
| 353 |
if (file) |
| 354 |
ret = cat_file_by_path(file, &newline, flags); |
| 355 |
|
| 356 |
STRV_FOREACH(path, dropins) |
| 357 |
RET_GATHER(ret, cat_file_by_path(*path, &newline, flags)); |
| 358 |
|
| 359 |
return ret; |
| 360 |
} |
| 361 |
|
| 362 |
void print_separator(void) { |
| 363 |
|
| 364 |
/* Outputs a separator line that resolves to whitespace when copied from the terminal. We do that by outputting |
| 365 |
* one line filled with spaces with ANSI underline set, followed by a second (empty) line. */ |
| 366 |
|
| 367 |
if (underline_enabled()) { |
| 368 |
size_t c = columns(); |
| 369 |
|
| 370 |
flockfile(stdout); |
| 371 |
fputs_unlocked(ansi_grey_underline(), stdout); |
| 372 |
|
| 373 |
for (size_t i = 0; i < c; i++) |
| 374 |
fputc_unlocked(' ', stdout); |
| 375 |
|
| 376 |
fputs_unlocked(ansi_normal(), stdout); |
| 377 |
fputs_unlocked("\n\n", stdout); |
| 378 |
funlockfile(stdout); |
| 379 |
} else |
| 380 |
fputs("\n\n", stdout); |
| 381 |
} |
| 382 |
|
| 383 |
static int guess_type(const char **name, char ***ret_prefixes, bool *ret_is_collection, const char **ret_extension) { |
| 384 |
/* Try to figure out if name is like tmpfiles.d/ or systemd/system-presets/, |
| 385 |
* i.e. a collection of directories without a main config file. |
| 386 |
* Incidentally, all those formats don't use sections. So we return a single |
| 387 |
* is_collection boolean, which also means that the format doesn't use sections. |
| 388 |
*/ |
| 389 |
|
| 390 |
_cleanup_free_ char *n = NULL; |
| 391 |
bool run = false, coll = false; |
| 392 |
const char *ext = ".conf"; |
| 393 |
|
| 394 |
assert(name); |
| 395 |
assert(ret_prefixes); |
| 396 |
assert(ret_is_collection); |
| 397 |
assert(ret_extension); |
| 398 |
|
| 399 |
/* This is static so that the array doesn't get deallocated when we exit the function */ |
| 400 |
static const char* const std_prefixes[] = { CONF_PATHS(""), NULL }; |
| 401 |
static const char* const run_prefixes[] = { "/run/", NULL }; |
| 402 |
|
| 403 |
if (path_equal(*name, "environment.d")) |
| 404 |
/* Special case: we need to include /etc/environment in the search path, even |
| 405 |
* though the whole concept is called environment.d. */ |
| 406 |
*name = "environment"; |
| 407 |
|
| 408 |
n = strdup(*name); |
| 409 |
if (!n) |
| 410 |
return log_oom(); |
| 411 |
|
| 412 |
delete_trailing_chars(n, "/"); |
| 413 |
|
| 414 |
/* We assume systemd-style config files support the /usr-/run-/etc split and dropins. */ |
| 415 |
|
| 416 |
if (endswith(n, ".d")) |
| 417 |
coll = true; |
| 418 |
|
| 419 |
if (path_equal(n, "udev/hwdb.d")) |
| 420 |
ext = ".hwdb"; |
| 421 |
else if (path_equal(n, "udev/rules.d")) |
| 422 |
ext = ".rules"; |
| 423 |
else if (path_equal(n, "kernel/install.d")) |
| 424 |
ext = ".install"; |
| 425 |
else if (path_equal(n, "systemd/ntp-units.d")) { |
| 426 |
coll = true; |
| 427 |
ext = ".list"; |
| 428 |
} else if (path_equal(n, "systemd/relabel-extra.d")) { |
| 429 |
coll = run = true; |
| 430 |
ext = ".relabel"; |
| 431 |
} else if (PATH_IN_SET(n, "systemd/system-preset", "systemd/user-preset", "systemd/initrd-preset")) { |
| 432 |
coll = true; |
| 433 |
ext = ".preset"; |
| 434 |
} |
| 435 |
|
| 436 |
*ret_prefixes = (char**) (run ? run_prefixes : std_prefixes); |
| 437 |
*ret_is_collection = coll; |
| 438 |
*ret_extension = ext; |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
int conf_files_cat(const char *root, const char *name, CatFlags flags) { |
| 443 |
_cleanup_strv_free_ char **dirs = NULL; |
| 444 |
char **prefixes = NULL; /* explicit initialization to appease gcc */ |
| 445 |
bool is_collection; |
| 446 |
const char *extension; |
| 447 |
int r; |
| 448 |
|
| 449 |
r = guess_type(&name, &prefixes, &is_collection, &extension); |
| 450 |
if (r < 0) |
| 451 |
return r; |
| 452 |
assert(prefixes); |
| 453 |
assert(extension); |
| 454 |
|
| 455 |
STRV_FOREACH(prefix, prefixes) { |
| 456 |
assert(endswith(*prefix, "/")); |
| 457 |
r = strv_extendf(&dirs, "%s%s%s", *prefix, name, |
| 458 |
is_collection ? "" : ".d"); |
| 459 |
if (r < 0) |
| 460 |
return log_error_errno(r, "Failed to build directory list: %m"); |
| 461 |
} |
| 462 |
|
| 463 |
if (DEBUG_LOGGING) { |
| 464 |
log_debug("Looking for configuration in:"); |
| 465 |
if (!is_collection) |
| 466 |
STRV_FOREACH(prefix, prefixes) |
| 467 |
log_debug(" %s%s%s", strempty(root), *prefix, name); |
| 468 |
|
| 469 |
STRV_FOREACH(t, dirs) |
| 470 |
log_debug(" %s%s/*%s", strempty(root), *t, extension); |
| 471 |
} |
| 472 |
|
| 473 |
/* First locate the main config file, if any */ |
| 474 |
_cleanup_(conf_file_freep) ConfFile *c = NULL; |
| 475 |
if (!is_collection) { |
| 476 |
STRV_FOREACH(prefix, prefixes) { |
| 477 |
_cleanup_free_ char *p = path_join(*prefix, name); |
| 478 |
if (!p) |
| 479 |
return log_oom(); |
| 480 |
|
| 481 |
r = conf_file_new(p, root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, &c); |
| 482 |
if (r >= 0 || r == -ERFKILL) /* Found a regular file or masked file */ |
| 483 |
break; |
| 484 |
} |
| 485 |
|
| 486 |
if (!c) |
| 487 |
printf("%s# Main configuration file %s not found%s\n", |
| 488 |
ansi_highlight_magenta(), |
| 489 |
name, |
| 490 |
ansi_normal()); |
| 491 |
} |
| 492 |
|
| 493 |
/* Then locate the drop-ins, if any */ |
| 494 |
ConfFile **dropins = NULL; |
| 495 |
size_t n_dropins = 0; |
| 496 |
CLEANUP_ARRAY(dropins, n_dropins, conf_file_free_array); |
| 497 |
r = conf_files_list_strv_full(extension, root, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN, (const char* const*) dirs, &dropins, &n_dropins); |
| 498 |
if (r < 0) |
| 499 |
return log_error_errno(r, "Failed to query file list: %m"); |
| 500 |
|
| 501 |
/* Show */ |
| 502 |
if (is_collection) |
| 503 |
flags |= CAT_FORMAT_HAS_SECTIONS; |
| 504 |
|
| 505 |
return cat_files_full(c, dropins, n_dropins, flags); |
| 506 |
} |
| 507 |
|
| 508 |
int terminal_tint_color(double hue, char **ret) { |
| 509 |
double red, green, blue; |
| 510 |
int r; |
| 511 |
|
| 512 |
assert(ret); |
| 513 |
|
| 514 |
r = get_default_background_color(&red, &green, &blue); |
| 515 |
if (r < 0) |
| 516 |
return log_debug_errno(r, "Unable to get terminal background color: %m"); |
| 517 |
|
| 518 |
double s, v; |
| 519 |
rgb_to_hsv(red, green, blue, /* ret_h= */ NULL, &s, &v); |
| 520 |
|
| 521 |
if (v > 50) /* If the background is bright, then pull down saturation */ |
| 522 |
s = 25; |
| 523 |
else /* otherwise pump it up */ |
| 524 |
s = 75; |
| 525 |
|
| 526 |
v = MAX(20, v); /* Make sure we don't hide the color in black */ |
| 527 |
|
| 528 |
uint8_t r8, g8, b8; |
| 529 |
hsv_to_rgb(hue, s, v, &r8, &g8, &b8); |
| 530 |
|
| 531 |
if (asprintf(ret, "48;2;%u;%u;%u", r8, g8, b8) < 0) |
| 532 |
return -ENOMEM; |
| 533 |
|
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
bool shall_tint_background(void) { |
| 538 |
static int cache = -1; |
| 539 |
|
| 540 |
if (cache >= 0) |
| 541 |
return cache; |
| 542 |
|
| 543 |
cache = getenv_bool("SYSTEMD_TINT_BACKGROUND"); |
| 544 |
if (cache == -ENXIO) |
| 545 |
return (cache = true); |
| 546 |
if (cache < 0) |
| 547 |
log_debug_errno(cache, "Failed to parse $SYSTEMD_TINT_BACKGROUND, leaving background tinting enabled: %m"); |
| 548 |
|
| 549 |
return cache != 0; |
| 550 |
} |
| 551 |
|
| 552 |
void draw_progress_bar_unbuffered(const char *prefix, double percentage) { |
| 553 |
if (!on_tty()) |
| 554 |
return; |
| 555 |
|
| 556 |
fputc('\r', stderr); |
| 557 |
if (prefix) { |
| 558 |
fputs(prefix, stderr); |
| 559 |
fputc(' ', stderr); |
| 560 |
} |
| 561 |
|
| 562 |
if (!terminal_is_dumb()) { |
| 563 |
/* Generate the Windows Terminal progress indication OSC sequence here. Most Linux terminals currently |
| 564 |
* ignore this. But let's hope this changes one day. For details about this OSC sequence, see: |
| 565 |
* |
| 566 |
* https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC |
| 567 |
* https://github.com/microsoft/terminal/pull/8055 |
| 568 |
*/ |
| 569 |
unsigned percentage_ceil = (unsigned) percentage; |
| 570 |
if ((double) percentage_ceil < percentage) |
| 571 |
percentage_ceil++; |
| 572 |
fprintf(stderr, ANSI_OSC "9;4;1;%u" ANSI_ST, percentage_ceil); |
| 573 |
|
| 574 |
size_t cols = columns(); |
| 575 |
size_t prefix_width = utf8_console_width(prefix) + 1 /* space */; |
| 576 |
size_t length = cols > prefix_width + 6 ? cols - prefix_width - 6 : 0; |
| 577 |
|
| 578 |
if (length > 5 && percentage >= 0.0 && percentage <= 100.0) { |
| 579 |
size_t p = (size_t) (length * percentage / 100.0); |
| 580 |
bool separator_done = false; |
| 581 |
|
| 582 |
fputs(ansi_highlight_green(), stderr); |
| 583 |
|
| 584 |
for (size_t i = 0; i < length; i++) { |
| 585 |
|
| 586 |
if (i <= p) { |
| 587 |
if (get_color_mode() == COLOR_24BIT) { |
| 588 |
uint8_t r8, g8, b8; |
| 589 |
double z = i == 0 ? 0 : (((double) i / p) * 100); |
| 590 |
hsv_to_rgb(145 /* green */, z, 33 + z*2/3, &r8, &g8, &b8); |
| 591 |
fprintf(stderr, "\x1B[38;2;%u;%u;%um", r8, g8, b8); |
| 592 |
} |
| 593 |
|
| 594 |
fputs(glyph(GLYPH_HORIZONTAL_FAT), stderr); |
| 595 |
} else if (i+1 < length && !separator_done) { |
| 596 |
fputs(ansi_normal(), stderr); |
| 597 |
fputc(' ', stderr); |
| 598 |
separator_done = true; |
| 599 |
fputs(ansi_grey(), stderr); |
| 600 |
} else |
| 601 |
fputs(glyph(GLYPH_HORIZONTAL_DOTTED), stderr); |
| 602 |
} |
| 603 |
|
| 604 |
fputs(ansi_normal(), stderr); |
| 605 |
fputc(' ', stderr); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
fprintf(stderr, |
| 610 |
"%s%3.0f%%%s", |
| 611 |
ansi_highlight(), |
| 612 |
percentage, |
| 613 |
ansi_normal()); |
| 614 |
|
| 615 |
if (!terminal_is_dumb()) |
| 616 |
fputs(ANSI_ERASE_TO_END_OF_LINE, stderr); |
| 617 |
|
| 618 |
fputc('\r', stderr); |
| 619 |
} |
| 620 |
|
| 621 |
void clear_progress_bar_unbuffered(const char *prefix) { |
| 622 |
if (!on_tty()) |
| 623 |
return; |
| 624 |
|
| 625 |
fputc('\r', stderr); |
| 626 |
|
| 627 |
if (terminal_is_dumb()) |
| 628 |
fputs(strrepa(" ", |
| 629 |
prefix ? utf8_console_width(prefix) + 5 : /* %3.0f%% (4 chars) + space */ |
| 630 |
LESS_BY(columns(), 1U)), |
| 631 |
stderr); |
| 632 |
else |
| 633 |
/* Undo Windows Terminal progress indication again. */ |
| 634 |
fputs(ANSI_OSC "9;4;0;" ANSI_ST |
| 635 |
ANSI_ERASE_TO_END_OF_LINE, stderr); |
| 636 |
|
| 637 |
fputc('\r', stderr); |
| 638 |
} |
| 639 |
|
| 640 |
void draw_progress_bar(const char *prefix, double percentage) { |
| 641 |
if (!on_tty()) |
| 642 |
return; |
| 643 |
|
| 644 |
/* We are going output a bunch of small strings that shall appear as a single line to STDERR which is |
| 645 |
* unbuffered by default. Let's temporarily turn on full buffering, so that this is passed to the tty |
| 646 |
* as a single buffer, to make things more efficient. */ |
| 647 |
WITH_BUFFERED_STDERR; |
| 648 |
draw_progress_bar_unbuffered(prefix, percentage); |
| 649 |
} |
| 650 |
|
| 651 |
int draw_progress_barf(double percentage, const char *prefixf, ...) { |
| 652 |
_cleanup_free_ char *s = NULL; |
| 653 |
va_list ap; |
| 654 |
int r; |
| 655 |
|
| 656 |
if (!on_tty()) |
| 657 |
return 0; |
| 658 |
|
| 659 |
va_start(ap, prefixf); |
| 660 |
r = vasprintf(&s, prefixf, ap); |
| 661 |
va_end(ap); |
| 662 |
|
| 663 |
if (r < 0) |
| 664 |
return -ENOMEM; |
| 665 |
|
| 666 |
draw_progress_bar(s, percentage); |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
void clear_progress_bar(const char *prefix) { |
| 671 |
if (!on_tty()) |
| 672 |
return; |
| 673 |
|
| 674 |
WITH_BUFFERED_STDERR; |
| 675 |
clear_progress_bar_unbuffered(prefix); |
| 676 |
} |