| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "analyze.h" |
| 4 |
#include "analyze-timespan.h" |
| 5 |
#include "ansi-color.h" |
| 6 |
#include "format-table.h" |
| 7 |
#include "glyph-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
int verb_timespan(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 13 |
STRV_FOREACH(input_timespan, strv_skip(argv, 1)) { |
| 14 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 15 |
usec_t output_usecs; |
| 16 |
TableCell *cell; |
| 17 |
int r; |
| 18 |
|
| 19 |
r = parse_time(*input_timespan, &output_usecs, USEC_PER_SEC); |
| 20 |
if (r < 0) { |
| 21 |
log_error_errno(r, "Failed to parse time span '%s': %m", *input_timespan); |
| 22 |
time_parsing_hint(*input_timespan, /* calendar= */ true, /* timestamp= */ true, /* timespan= */ false); |
| 23 |
return r; |
| 24 |
} |
| 25 |
|
| 26 |
table = table_new_vertical(); |
| 27 |
if (!table) |
| 28 |
return log_oom(); |
| 29 |
|
| 30 |
assert_se(cell = table_get_cell(table, 0, 0)); |
| 31 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 32 |
|
| 33 |
assert_se(cell = table_get_cell(table, 0, 1)); |
| 34 |
(void) table_set_ellipsize_percent(table, cell, 100); |
| 35 |
|
| 36 |
r = table_add_many(table, |
| 37 |
TABLE_FIELD, "Original", |
| 38 |
TABLE_STRING, *input_timespan); |
| 39 |
if (r < 0) |
| 40 |
return table_log_add_error(r); |
| 41 |
|
| 42 |
r = table_add_cell_stringf_full(table, NULL, TABLE_FIELD, "%ss", glyph(GLYPH_MU)); |
| 43 |
if (r < 0) |
| 44 |
return table_log_add_error(r); |
| 45 |
|
| 46 |
r = table_add_many(table, |
| 47 |
TABLE_UINT64, output_usecs, |
| 48 |
TABLE_FIELD, "Human", |
| 49 |
TABLE_TIMESPAN, output_usecs, |
| 50 |
TABLE_SET_COLOR, ansi_highlight()); |
| 51 |
if (r < 0) |
| 52 |
return table_log_add_error(r); |
| 53 |
|
| 54 |
r = table_print_or_warn(table); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
if (input_timespan[1]) |
| 59 |
putchar('\n'); |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |