list.c 3.0 KB

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