list.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = totp_config_get_token_iterator_context(plugin_state);
  21. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  22. if(total_count <= 0) {
  23. TOTP_CLI_PRINTF("There are no tokens");
  24. return;
  25. }
  26. TOTP_CLI_LOCK_UI(plugin_state);
  27. size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  28. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  29. TOTP_CLI_PRINTF("| %-3s | %-25s | %-6s | %-s | %-s |\r\n", "#", "Name", "Algo", "Ln", "Dur");
  30. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  31. for(size_t i = 0; i < total_count; i++) {
  32. totp_token_info_iterator_go_to(iterator_context, i);
  33. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  34. TOTP_CLI_PRINTF(
  35. "| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-3" PRIu8 " |\r\n",
  36. i + 1,
  37. furi_string_get_cstr(token_info->name),
  38. token_info_get_algo_as_cstr(token_info),
  39. token_info->digits,
  40. token_info->duration);
  41. }
  42. TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
  43. totp_token_info_iterator_go_to(iterator_context, original_index);
  44. TOTP_CLI_UNLOCK_UI(plugin_state);
  45. }