|
7 | 7 | #include "../../../ui/scene_director.h"
|
8 | 8 | #include "../../cli_helpers.h"
|
9 | 9 | #include "../../common_command_arguments.h"
|
| 10 | +#include "formatters/table/details_output_formatter_table.h" |
| 11 | +#include "formatters/tsv/details_output_formatter_tsv.h" |
10 | 12 |
|
11 |
| -#define TOTP_CLI_PRINTF_AUTOMATION_FEATURE(description, header_printed) \ |
12 |
| - do { \ |
13 |
| - TOTP_CLI_PRINTF( \ |
14 |
| - "| %-20s | %-28.28s |\r\n", \ |
15 |
| - header_printed ? "" : "Automation features", \ |
16 |
| - description); \ |
17 |
| - header_printed = true; \ |
18 |
| - } while(false) |
| 13 | +typedef void (*TOTP_CLI_DETAILS_HEADER_FORMATTER)(); |
| 14 | +typedef void (*TOTP_CLI_DETAILS_FOOTER_FORMATTER)(); |
| 15 | +typedef void (*TOTP_CLI_DETAILS_AUTOMATION_FEATURE_ITEM_FORMATTER)(const char* key, const char* feature, bool* header_printed); |
| 16 | +typedef void (*TOTP_CLI_DETAILS_CSTR_FORMATTER)(const char* key, const char* value); |
| 17 | +typedef void (*TOTP_CLI_DETAILS_UINT8T_FORMATTER)(const char* key, uint8_t value); |
| 18 | +typedef void (*TOTP_CLI_DETAILS_SIZET_FORMATTER)(const char* key, size_t value); |
19 | 19 |
|
20 |
| -static void print_automation_features(const TokenInfo* token_info) { |
| 20 | +typedef struct { |
| 21 | + const TOTP_CLI_DETAILS_HEADER_FORMATTER header_formatter; |
| 22 | + const TOTP_CLI_DETAILS_FOOTER_FORMATTER footer_formatter; |
| 23 | + const TOTP_CLI_DETAILS_AUTOMATION_FEATURE_ITEM_FORMATTER automation_feature_item_formatter; |
| 24 | + const TOTP_CLI_DETAILS_CSTR_FORMATTER cstr_formatter; |
| 25 | + const TOTP_CLI_DETAILS_UINT8T_FORMATTER uint8t_formatter; |
| 26 | + const TOTP_CLI_DETAILS_SIZET_FORMATTER sizet_formatter; |
| 27 | +} TotpCliDetailsFormatter; |
| 28 | + |
| 29 | +static const TotpCliDetailsFormatter available_formatters[] = { |
| 30 | + {.header_formatter = &details_output_formatter_print_header_table, |
| 31 | + .footer_formatter = &details_output_formatter_print_footer_table, |
| 32 | + .automation_feature_item_formatter = &details_output_formatter_print_automation_feature_table, |
| 33 | + .cstr_formatter = &details_output_formatter_print_cstr_table, |
| 34 | + .uint8t_formatter = &details_output_formatter_print_uint8t_table, |
| 35 | + .sizet_formatter = &details_output_formatter_print_sizet_table}, |
| 36 | + |
| 37 | + {.header_formatter = &details_output_formatter_print_header_tsv, |
| 38 | + .footer_formatter = &details_output_formatter_print_footer_tsv, |
| 39 | + .automation_feature_item_formatter = &details_output_formatter_print_automation_feature_tsv, |
| 40 | + .cstr_formatter = &details_output_formatter_print_cstr_tsv, |
| 41 | + .uint8t_formatter = &details_output_formatter_print_uint8t_tsv, |
| 42 | + .sizet_formatter = &details_output_formatter_print_sizet_tsv}, |
| 43 | +}; |
| 44 | + |
| 45 | +static void print_automation_features(const TokenInfo* token_info, const TotpCliDetailsFormatter* formatter) { |
| 46 | + bool header_printed = false; |
| 47 | + const char* AUTOMATION_FEATURES_PRINT_KEY = "Automation features"; |
21 | 48 | if(token_info->automation_features == TokenAutomationFeatureNone) {
|
22 |
| - TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None"); |
| 49 | + (*formatter->automation_feature_item_formatter)(AUTOMATION_FEATURES_PRINT_KEY, "None", &header_printed); |
23 | 50 | return;
|
24 | 51 | }
|
25 |
| - |
26 |
| - bool header_printed = false; |
| 52 | + |
27 | 53 | if(token_info->automation_features & TokenAutomationFeatureEnterAtTheEnd) {
|
28 |
| - TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Enter> key at the end", header_printed); |
| 54 | + (*formatter->automation_feature_item_formatter)(AUTOMATION_FEATURES_PRINT_KEY, "Type <Enter> key at the end", &header_printed); |
29 | 55 | }
|
30 | 56 |
|
31 | 57 | if(token_info->automation_features & TokenAutomationFeatureTabAtTheEnd) {
|
32 |
| - TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type <Tab> key at the end", header_printed); |
| 58 | + (*formatter->automation_feature_item_formatter)(AUTOMATION_FEATURES_PRINT_KEY, "Type <Tab> key at the end", &header_printed); |
33 | 59 | }
|
34 | 60 |
|
35 | 61 | if(token_info->automation_features & TokenAutomationFeatureTypeSlower) {
|
36 |
| - TOTP_CLI_PRINTF_AUTOMATION_FEATURE("Type slower", header_printed); |
| 62 | + (*formatter->automation_feature_item_formatter)(AUTOMATION_FEATURES_PRINT_KEY, "Type slower", &header_printed); |
37 | 63 | }
|
38 | 64 | }
|
39 | 65 |
|
@@ -64,26 +90,29 @@ void totp_cli_command_details_handle(PluginState* plugin_state, FuriString* args
|
64 | 90 | return;
|
65 | 91 | }
|
66 | 92 |
|
| 93 | + const TotpCliDetailsFormatter* formatter = &available_formatters[0]; |
| 94 | + FuriString* arg = furi_string_alloc(); |
| 95 | + if(args_read_string_and_trim(args, arg) && furi_string_cmpi_str(arg, "--tsv") == 0) { |
| 96 | + formatter = &available_formatters[1]; |
| 97 | + } |
| 98 | + |
| 99 | + furi_string_free(arg); |
| 100 | + |
67 | 101 | TOTP_CLI_LOCK_UI(plugin_state);
|
68 | 102 |
|
69 | 103 | size_t original_token_index =
|
70 | 104 | totp_token_info_iterator_get_current_token_index(iterator_context);
|
71 | 105 | if(totp_token_info_iterator_go_to(iterator_context, token_number - 1)) {
|
72 | 106 | const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
|
73 | 107 |
|
74 |
| - TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n"); |
75 |
| - TOTP_CLI_PRINTF("| %-20s | %-28s |\r\n", "Property", "Value"); |
76 |
| - TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n"); |
77 |
| - TOTP_CLI_PRINTF("| %-20s | %-28d |\r\n", "Index", token_number); |
78 |
| - TOTP_CLI_PRINTF( |
79 |
| - "| %-20s | %-28.28s |\r\n", "Name", furi_string_get_cstr(token_info->name)); |
80 |
| - TOTP_CLI_PRINTF( |
81 |
| - "| %-20s | %-28s |\r\n", "Hashing algorithm", token_info_get_algo_as_cstr(token_info)); |
82 |
| - TOTP_CLI_PRINTF("| %-20s | %-28" PRIu8 " |\r\n", "Number of digits", token_info->digits); |
83 |
| - TOTP_CLI_PRINTF( |
84 |
| - "| %-20s | %" PRIu8 " sec.%-21s |\r\n", "Token lifetime", token_info->duration, " "); |
85 |
| - print_automation_features(token_info); |
86 |
| - TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n"); |
| 108 | + (*formatter->header_formatter)(); |
| 109 | + (*formatter->sizet_formatter)("Index", token_number); |
| 110 | + (*formatter->cstr_formatter)("Name", furi_string_get_cstr(token_info->name)); |
| 111 | + (*formatter->cstr_formatter)("Hashing algorithm", token_info_get_algo_as_cstr(token_info)); |
| 112 | + (*formatter->uint8t_formatter)("Number of digits", token_info->digits); |
| 113 | + (*formatter->uint8t_formatter)("Token lifetime", token_info->duration); |
| 114 | + print_automation_features(token_info, formatter); |
| 115 | + (*formatter->footer_formatter)(); |
87 | 116 | } else {
|
88 | 117 | totp_cli_print_error_loading_token_info();
|
89 | 118 | }
|
|
0 commit comments