cli_helpers.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <cli/cli.h>
  3. #include "../types/plugin_state.h"
  4. #define TOTP_CLI_COMMAND_NAME "totp"
  5. #define DOCOPT_ARGUMENT(arg) "<" arg ">"
  6. #define DOCOPT_MULTIPLE(arg) arg "..."
  7. #define DOCOPT_OPTIONAL(param) "[" param "]"
  8. #define DOCOPT_REQUIRED(param) "(" param ")"
  9. #define DOCOPT_OPTION(option, value) option " " value
  10. #define DOCOPT_SWITCH(option) option
  11. #define DOCOPT_OPTIONS "[options]"
  12. #define DOCOPT_DEFAULT(val) "[default: " val "]"
  13. #define TOTP_CLI_PRINTF(format, ...) printf(format, ##__VA_ARGS__)
  14. #define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \
  15. printf("\e[%s", color); \
  16. printf(format, ##__VA_ARGS__); \
  17. printf("\e[0m"); \
  18. fflush(stdout)
  19. #define TOTP_CLI_COLOR_ERROR "91m"
  20. #define TOTP_CLI_COLOR_WARNING "93m"
  21. #define TOTP_CLI_COLOR_SUCCESS "92m"
  22. #define TOTP_CLI_COLOR_INFO "96m"
  23. #define TOTP_CLI_PRINTF_ERROR(format, ...) \
  24. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__)
  25. #define TOTP_CLI_PRINTF_WARNING(format, ...) \
  26. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__)
  27. #define TOTP_CLI_PRINTF_SUCCESS(format, ...) \
  28. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__)
  29. #define TOTP_CLI_PRINTF_INFO(format, ...) \
  30. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__)
  31. #define TOTP_CLI_DELETE_LAST_LINE() \
  32. TOTP_CLI_PRINTF("\033[A\33[2K\r"); \
  33. fflush(stdout)
  34. #define TOTP_CLI_DELETE_CURRENT_LINE() \
  35. TOTP_CLI_PRINTF("\33[2K\r"); \
  36. fflush(stdout)
  37. #define TOTP_CLI_DELETE_LAST_CHAR() \
  38. TOTP_CLI_PRINTF("\b \b"); \
  39. fflush(stdout)
  40. #define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \
  41. TOTP_CLI_PRINTF_ERROR( \
  42. "Invalid command arguments. use \"help\" command to get list of available commands")
  43. #define TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE() \
  44. TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n")
  45. /**
  46. * @brief Checks whether user is authenticated and entered correct PIN.
  47. * If user is not authenticated it prompts user to enter correct PIN to authenticate.
  48. * @param plugin_state application state
  49. * @param cli pointer to the firmware CLI subsystem
  50. * @return \c true if user is already authenticated or successfully authenticated; \c false otherwise
  51. */
  52. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli);
  53. /**
  54. * @brief Forces application to be instantly closed
  55. * @param event_queue main app queue
  56. */
  57. void totp_cli_force_close_app(FuriMessageQueue* event_queue);
  58. /**
  59. * @brief Reads line of characters from console
  60. * @param cli pointer to the firmware CLI subsystem
  61. * @param out_str pointer to an output string to put read line to
  62. * @param mask_user_input whether to mask input characters in console or not
  63. * @return \c true if line successfully read and confirmed; \c false otherwise
  64. */
  65. bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input);
  66. /**
  67. * @brief Extracts \c uint8_t value and trims arguments string
  68. * @param args arguments string
  69. * @param[out] value parsed value
  70. * @return \c true if value successfully read and parsed as \c uint8_t ; \c false otherwise
  71. */
  72. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value);
  73. /**
  74. * @brief Free \c FuriString instance in a secure manner by clearing it first
  75. * @param str instance to free
  76. */
  77. void furi_string_secure_free(FuriString* str);