list.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. };
  27. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  28. void totp_cli_command_list_docopt_commands() {
  29. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  30. " List all available tokens\r\n");
  31. }
  32. void totp_cli_command_list_docopt_usage() {
  33. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  34. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  35. }
  36. #endif
  37. void totp_cli_command_list_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  38. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  39. return;
  40. }
  41. TokenInfoIteratorContext* iterator_context =
  42. totp_config_get_token_iterator_context(plugin_state);
  43. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  44. if(total_count <= 0) {
  45. TOTP_CLI_PRINTF("There are no tokens");
  46. return;
  47. }
  48. const TotpCliListFormatter* formatter = &available_formatters[0];
  49. FuriString* arg = furi_string_alloc();
  50. if(args_read_string_and_trim(args, arg) && furi_string_cmpi_str(arg, "--tsv") == 0) {
  51. formatter = &available_formatters[1];
  52. }
  53. furi_string_free(arg);
  54. TOTP_CLI_LOCK_UI(plugin_state);
  55. size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  56. (*formatter->header_formatter)();
  57. for(size_t i = 0; i < total_count; i++) {
  58. totp_token_info_iterator_go_to(iterator_context, i);
  59. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  60. (*formatter->body_item_formatter)(i, token_info);
  61. }
  62. (*formatter->footer_formatter)();
  63. totp_token_info_iterator_go_to(iterator_context, original_index);
  64. TOTP_CLI_UNLOCK_UI(plugin_state);
  65. }