list.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/constants.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../../ui/scene_director.h"
  8. #include "../../cli_helpers.h"
  9. #include "formatters/table/list_output_formatter_table.h"
  10. #include "formatters/tsv/list_output_formatter_tsv.h"
  11. typedef void (*TOTP_CLI_LIST_HEADER_FORMATTER)();
  12. typedef void (*TOTP_CLI_LIST_FOOTER_FORMATTER)();
  13. typedef void (*TOTP_CLI_LIST_BODY_ITEM_FORMATTER)(size_t index, const TokenInfo* token_info);
  14. typedef struct {
  15. const TOTP_CLI_LIST_HEADER_FORMATTER header_formatter;
  16. const TOTP_CLI_LIST_FOOTER_FORMATTER footer_formatter;
  17. const TOTP_CLI_LIST_BODY_ITEM_FORMATTER body_item_formatter;
  18. } TotpCliListFormatter;
  19. static const TotpCliListFormatter available_formatters[] = {
  20. {.header_formatter = &list_output_formatter_print_header_table,
  21. .body_item_formatter = &list_output_formatter_print_body_item_table,
  22. .footer_formatter = &list_output_formatter_print_footer_table},
  23. {.header_formatter = &list_output_formatter_print_header_tsv,
  24. .body_item_formatter = &list_output_formatter_print_body_item_tsv,
  25. .footer_formatter = &list_output_formatter_print_footer_tsv}};
  26. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  27. void totp_cli_command_list_docopt_commands() {
  28. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  29. " List all available tokens\r\n");
  30. }
  31. void totp_cli_command_list_docopt_usage() {
  32. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  33. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  34. }
  35. #endif
  36. void totp_cli_command_list_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  37. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  38. return;
  39. }
  40. TokenInfoIteratorContext* iterator_context =
  41. totp_config_get_token_iterator_context(plugin_state);
  42. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  43. if(total_count <= 0) {
  44. TOTP_CLI_PRINTF("There are no tokens");
  45. return;
  46. }
  47. const TotpCliListFormatter* formatter = &available_formatters[0];
  48. FuriString* arg = furi_string_alloc();
  49. if(args_read_string_and_trim(args, arg) && furi_string_cmpi_str(arg, "--tsv") == 0) {
  50. formatter = &available_formatters[1];
  51. }
  52. furi_string_free(arg);
  53. TOTP_CLI_LOCK_UI(plugin_state);
  54. size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  55. (*formatter->header_formatter)();
  56. for(size_t i = 0; i < total_count; i++) {
  57. totp_token_info_iterator_go_to(iterator_context, i);
  58. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  59. (*formatter->body_item_formatter)(i, token_info);
  60. }
  61. (*formatter->footer_formatter)();
  62. totp_token_info_iterator_go_to(iterator_context, original_index);
  63. TOTP_CLI_UNLOCK_UI(plugin_state);
  64. }