Skip to content

Commit

Permalink
add aww-date support for common commandline flags
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Nov 3, 2024
1 parent 688a90e commit 76c7a69
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 35 deletions.
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
23 changes: 23 additions & 0 deletions include/aww-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <string_view>
#include <thread>

#include <aww-collection/aww-collection.hpp>

// Windows tricks for aww::os::Proccess
#ifdef _WIN32
#define popen _popen
Expand All @@ -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) {}
Expand All @@ -44,6 +56,17 @@ template <size_t N> 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<std::string>& args, const std::string& flag);

// RESULT
class Result {
public:
/* Create a successful result */
Expand Down
9 changes: 9 additions & 0 deletions src/aww-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#include <cctype>
#include <fstream>

namespace aww {
/**
* @brief Remove a flag from the arguments
*/
bool erase_flag_from_args(std::vector<std::string>& 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/)
Expand Down
44 changes: 31 additions & 13 deletions src/internal/aww-date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@ namespace aww::internal::aww_date {

int aww_date_main(const std::vector<std::string>& 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, "-");

Expand All @@ -25,20 +41,22 @@ int aww_date_main(const std::vector<std::string>& 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;
}

Expand Down
24 changes: 3 additions & 21 deletions src/internal/aww-run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& 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<std::string>& 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) {
Expand Down

0 comments on commit 76c7a69

Please sign in to comment.