list.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include "../../../types/token_info.h"
  4. #include "../../../services/config/constants.h"
  5. #include "../../../services/config/config.h"
  6. #include "../../../ui/scene_director.h"
  7. #include "../../cli_helpers.h"
  8. void totp_cli_command_list_docopt_commands() {
  9. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
  10. " List all available tokens\r\n");
  11. }
  12. void totp_cli_command_list_docopt_usage() {
  13. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " DOCOPT_REQUIRED(
  14. TOTP_CLI_COMMAND_LIST " | " TOTP_CLI_COMMAND_LIST_ALT) "\r\n");
  15. }
  16. void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
  17. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  18. return;
  19. }
  20. TokenInfoIteratorContext* iterator_context =
  21. totp_config_get_token_iterator_context(plugin_state);
  22. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  23. if(total_count <= 0) {
  24. TOTP_CLI_PRINTF("There are no tokens");
  25. return;
  26. }
  27. TOTP_CLI_LOCK_UI(plugin_state);
  28. size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  29. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  30. TOTP_CLI_PRINTF("| %-3s | %-25s | %-6s | %-s | %-s |\r\n", "#", "Name", "Algo", "Ln", "Dur");
  31. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  32. for(size_t i = 0; i < total_count; i++) {
  33. totp_token_info_iterator_go_to(iterator_context, i);
  34. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  35. TOTP_CLI_PRINTF(
  36. "| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-3" PRIu8 " |\r\n",
  37. i + 1,
  38. furi_string_get_cstr(token_info->name),
  39. token_info_get_algo_as_cstr(token_info),
  40. token_info->digits,
  41. token_info->duration);
  42. }
  43. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  44. totp_token_info_iterator_go_to(iterator_context, original_index);
  45. TOTP_CLI_UNLOCK_UI(plugin_state);
  46. }