list.c 2.2 KB

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