From 76c7a696fe27828bc0c27ed8a70c22bddea59455 Mon Sep 17 00:00:00 2001 From: John Doe Date: Sun, 3 Nov 2024 14:29:03 -0800 Subject: [PATCH] add aww-date support for common commandline flags --- CMakeLists.txt | 14 ++++++++++++- include/aww-common.hpp | 23 ++++++++++++++++++++ src/aww-common.cpp | 9 ++++++++ src/internal/aww-date.cpp | 44 +++++++++++++++++++++++++++------------ src/internal/aww-run.cpp | 24 +++------------------ 5 files changed, 79 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 050a53c..a8b5924 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,7 @@ endif() set(TESTFILES # All .cpp files in tests/ tests/aww.cpp ) -set(LIBRARY_NAME awwlib) # Default name for the library built from src/*.cpp (change if you wish) +set(LIBRARY_NAME libawwtools) # Default name for the library built from src/*.cpp (change if you wish) ## Third-party # https://github.com/nlohmann/json @@ -103,9 +103,21 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/clip-1.5) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/fmt-9.1.0) ## end of fmt +## AwwLib + +FetchContent_Declare( + awwlib + GIT_REPOSITORY https://github.com/dzharii/awwlib-cpp.git + GIT_TAG dmytro_zharii/2024-10-13-dev + GIT_SHALLOW TRUE +) + +FetchContent_MakeAvailable(awwlib) + # There's also (probably) doctests within the library, so we need to see this as well. target_link_libraries( ${LIBRARY_NAME} PUBLIC + awwlib doctest clip ) diff --git a/include/aww-common.hpp b/include/aww-common.hpp index 0fb0c91..4fa2f6d 100644 --- a/include/aww-common.hpp +++ b/include/aww-common.hpp @@ -16,6 +16,8 @@ #include #include +#include + // Windows tricks for aww::os::Proccess #ifdef _WIN32 #define popen _popen @@ -24,6 +26,16 @@ #endif namespace aww { + +// CONSTANTS +namespace constants { + +// CPMMON COMMANDLINE FLAGS +const std::string CMD_FLAG_NO_LOGGING = "--aww-no-logging"; +const std::string CMD_FLAG_NO_NOTIFICATIONS = "--aww-no-notifications"; + +} // namespace constants + // CallTag struct definition struct call_tag_t { constexpr explicit call_tag_t(std::uint64_t value) : value(value) {} @@ -44,6 +56,17 @@ template constexpr call_tag_t call_tag(const char (&str)[N]) { return call_tag_t(_compiletime_hash(str)); } +// COLLECTIONS + +/** + * @brief Remove a flag from the arguments + * @param args The arguments to remove the flag from + * @param flag The flag name to remove + * @returns true if the flag was found and removed, false otherwise + */ +bool erase_flag_from_args(std::vector& args, const std::string& flag); + +// RESULT class Result { public: /* Create a successful result */ diff --git a/src/aww-common.cpp b/src/aww-common.cpp index 3664c42..954a418 100644 --- a/src/aww-common.cpp +++ b/src/aww-common.cpp @@ -3,6 +3,15 @@ #include #include +namespace aww { +/** + * @brief Remove a flag from the arguments + */ +bool erase_flag_from_args(std::vector& args, const std::string& flag) { + return aww::erase_all_matched_elements(args, flag); +} +} // namespace aww + namespace aww::date { // TODO: - 2022-10-18 [Exploring C++11, part 2 localtime and time again Kjellkod's // Blog](https://kjellkod.wordpress.com/2013/01/22/exploring-c11-part-2-localtime-and-time-again/) diff --git a/src/internal/aww-date.cpp b/src/internal/aww-date.cpp index 2aaf5d3..5aed8ef 100644 --- a/src/internal/aww-date.cpp +++ b/src/internal/aww-date.cpp @@ -10,12 +10,28 @@ namespace aww::internal::aww_date { int aww_date_main(const std::vector& cmdArgs, aww_date_io_dependencies_interface& deps) { - spdlog::info("Hello spdlog"); - fmt::print("Hello, world from fmt PLEASE REMOVE THIS \b!\n"); + + auto mutableCmdArgs = cmdArgs; + + bool noLogging = aww::erase_flag_from_args(mutableCmdArgs, aww::constants::CMD_FLAG_NO_LOGGING); + bool noNotifications = + aww::erase_flag_from_args(mutableCmdArgs, aww::constants::CMD_FLAG_NO_NOTIFICATIONS); + + // Configure spdlog based on the flag + if (noLogging) { + spdlog::set_level(spdlog::level::off); + } else { + spdlog::set_level(spdlog::level::info); // Set desired log level + } + + if (mutableCmdArgs.size() == 0) { + spdlog::warn("No arguments provided"); + return 1; + } std::string currentDate = deps.get_date_yyyymmdd(aww::call_tag("1rfpeonkhns")); - std::string fileName = aww::string::join(cmdArgs, "-"); + std::string fileName = aww::string::join(mutableCmdArgs, "-"); std::regex replaceFilenameUnsafeChars("[^\\._a-zA-Z0-9-]"); std::string safeFileName = std::regex_replace(fileName, replaceFilenameUnsafeChars, "-"); @@ -25,20 +41,22 @@ int aww_date_main(const std::vector& cmdArgs, result = result + "-" + safeFileName; } if (deps.clipboard_set_text(result, aww::call_tag("t7svmrrhai0"))) { - std::cout << "Copied to clipboard: " << result << "\n"; - deps.show_notification("aww date", "The date has been copied to the clipboard", - aww::call_tag("tssis4p5ta2")); + spdlog::info("Copied to clipboard: {}", result); + + if (!noNotifications) { + deps.show_notification("aww date", "The date has been copied to the clipboard", + aww::call_tag("tssis4p5ta2")); + } } else { - std::cout << "Failed to copy to clipboard: " << result << "\n"; - deps.show_notification("aww date", "Failed to copy the date to the clipboard", - aww::call_tag("730v5jc2d3o")); + spdlog::error("Failed to copy to clipboard: {}", result); + if (!noNotifications) { + deps.show_notification("aww date", "Failed to copy the date to the clipboard", + aww::call_tag("730v5jc2d3o")); + } return 1; } - std::cout << "Result:" - << "\n" - << result << "\n"; - + std::cout << result << "\n"; return 0; } diff --git a/src/internal/aww-run.cpp b/src/internal/aww-run.cpp index a5b88d3..54635a7 100644 --- a/src/internal/aww-run.cpp +++ b/src/internal/aww-run.cpp @@ -14,30 +14,12 @@ namespace fs = std::filesystem; namespace aww::internal::aww_run { -/** - * @brief Remove a flag from the arguments - * @param args The arguments to remove the flag from - * @param flag The flag name to remove - * @returns true if the flag was found and removed, false otherwise - */ -bool erase_flag_from_args(std::vector& args, const std::string& flag) { - auto it = std::find(args.begin(), args.end(), flag); - if (it != args.end()) { - args.erase(it); - return true; - } - return false; -} - int aww_run_main(const std::vector& cmdArgs) { auto mutableCmdArgs = cmdArgs; - // Flag to disable logging - const std::string flagNoLogging = "--aww-no-logging"; - const std::string flagNoNotifications = "--aww-no-notifications"; - - bool noLogging = erase_flag_from_args(mutableCmdArgs, flagNoLogging); - bool noNotifications = erase_flag_from_args(mutableCmdArgs, flagNoNotifications); + bool noLogging = aww::erase_flag_from_args(mutableCmdArgs, aww::constants::CMD_FLAG_NO_LOGGING); + bool noNotifications = + aww::erase_flag_from_args(mutableCmdArgs, aww::constants::CMD_FLAG_NO_NOTIFICATIONS); // Configure spdlog based on the flag if (noLogging) {