Skip to content

Commit

Permalink
fix: preview title not having all events listed
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaDucak committed Nov 15, 2024
1 parent a13acc5 commit 3e11dde
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 41 deletions.
43 changes: 25 additions & 18 deletions source/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <algorithm>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <memory>
#include <ranges>

Expand All @@ -16,23 +17,30 @@ using namespace utils;
namespace {

std::string makePreviewTitle(std::chrono::year_month_day date, const CalendarEvents &events) {
auto eventForDate = [&]() -> std::optional<std::tuple<std::string, CalendarEvent>> {
for (const auto &[groupName, events] : events) {
for (const auto &event : events) {
auto eventsForDate = [&]() -> std::map<std::string, std::set<std::string>> {
std::map<std::string, std::set<std::string>> filteredEvents;

for (const auto &[groupName, groupEvents] : events) {
for (const auto &event : groupEvents) {
if (event.date == date::monthDay(date)) {
return std::make_tuple(groupName, event);
filteredEvents[groupName].insert(event.name);
}
}
}
return std::nullopt;
return filteredEvents;
}();

const auto eventStr =
eventForDate.has_value()
? fmt::format("({} - {})", std::get<0>(*eventForDate), std::get<1>(*eventForDate).name)
: "";
const auto title =
fmt::format("log preview for {} {}", utils::date::formatToString(date), eventStr);
// join in format: "birthdays: alice, bob; holidays: christmas"
std::string eventString;
for (const auto &[group, events] : eventsForDate) {
if (not eventString.empty()) {
eventString += "; ";
}
eventString += fmt::format("{}: {}", group, fmt::join(events, ", "));
}

const auto title = fmt::format("log preview for {} {}", utils::date::formatToString(date),
eventString.empty() ? "" : " - " + eventString);
return title;
}

Expand All @@ -54,9 +62,9 @@ void ViewDataUpdater::handleFocusedTagChange() {
m_view->setHighlightedDates(
&m_data.tagsPerSection.at(m_view->getSelectedSection()).at(AnnualLogData::kAnyOrNoTag));
} else {
const auto *const highlighMap =
const auto *const highlightedDates =
&m_data.tagsPerSection.at(m_view->getSelectedSection()).at(newTag);
m_view->setHighlightedDates(highlighMap);
m_view->setHighlightedDates(highlightedDates);
}
}

Expand Down Expand Up @@ -288,8 +296,8 @@ void App::handleUiStarted() {
void App::quit() {
if (m_gitRepo) {
m_view->loadingScreen("Committing & pushing...");
m_gitRepo->commitAll([this](bool somethingCommited) {
if (somethingCommited) {
m_gitRepo->commitAll([this](bool somethingCommitted) {
if (somethingCommitted) {
m_gitRepo->push([this] { m_view->stop(); });
} else {
m_view->stop();
Expand Down Expand Up @@ -336,15 +344,14 @@ void App::handleCalendarButtonClick() {

// check that after editing still exists
log = m_repo->read(date);
if (log && noMeaningfullContent(log->getContent(), date)) {
if (log && noMeaningfulContent(log->getContent(), date)) {
m_repo->remove(date);
}
updateDataAndViewAfterLogChange(date);
});
}

bool App::noMeaningfullContent(const std::string &content,
const std::chrono::year_month_day &date) {
bool App::noMeaningfulContent(const std::string &content, const std::chrono::year_month_day &date) {
return content == date::formatToString(date, kLogBaseTemplate) || content.empty();
}
} // namespace caps_log
6 changes: 3 additions & 3 deletions source/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace utils;
static const std::string kLogBaseTemplate{"# %d. %m. %y."};

/**
* A helper class that updatest the view components after the data in the AnnualLogData has changed.
* A helper class that updates the view components after the data in the AnnualLogData has changed.
* It updates the menus, the highlighted dates and the preview string, other elements of the view
* are not part of it's set of responsibilities. This happens when the user deletes a log or adds a
* new one or update an existing one.
Expand Down Expand Up @@ -88,7 +88,7 @@ class App final : public InputHandlerBase {
void deleteFocusedLog();
void quit();

static bool noMeaningfullContent(const std::string &content,
const std::chrono::year_month_day &date);
static bool noMeaningfulContent(const std::string &content,
const std::chrono::year_month_day &date);
};
} // namespace caps_log
4 changes: 2 additions & 2 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ boost::program_options::variables_map parseCLIOptions(int argc, const char **arg
("log-name-format", po::value<std::string>()->default_value("d%Y_%m_%d.md"), "format in which log entry markdown files are saved")
("sunday-start", "have the calendar display sunday as first day of the week")
("first-line-section", "if a section mark is placed on the first line, by default it is ignored as it's left for log title, this overrides this behaviour")
("password", po::value<std::string>(), "password for encrypted log repositores or to be used with --encrypt/--decrypt")
("password", po::value<std::string>(), "password for encrypted log repositories or to be used with --encrypt/--decrypt")
("encrypt", "apply encryption to all logs in log dir path (needs --password)")
("decrypt", "apply decryption to all logs in log dir path (needs --password)");
// clang-format on
Expand All @@ -209,7 +209,7 @@ boost::program_options::variables_map parseCLIOptions(int argc, const char **arg
po::notify(vmap);

if (vmap.count("help") != 0U) {
std::cout << "Capstains Log (caps-log)! A CLI journalig tool." << '\n';
std::cout << "Captain's Log (caps-log)! A CLI journalig tool." << '\n';
std::cout << "Version: " << CAPS_LOG_VERSION_STRING << std::endl;
std::cout << desc;
std::cout.flush();
Expand Down
1 change: 0 additions & 1 deletion source/log/local_log_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "log/log_repository_crypto_applier.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
#include <utils/crypto.hpp>
Expand Down
1 change: 0 additions & 1 deletion source/log/log_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "utils/string.hpp"
#include <functional>
#include <iostream>
#include <ranges>
#include <regex>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion source/log/log_repository_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace caps_log::log {

/*
* Only class that actualy interacts with physical files on the drive
* Only class that actually interacts with physical files on the drive
*/
class LogRepositoryBase {
public:
Expand Down
2 changes: 1 addition & 1 deletion source/log/log_repository_crypto_applier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void updateEncryptionMarkerfile(Crypto crypto, const std::filesystem::path &logD
if (crypto == Crypto::Encrypt) {
std::ofstream cle{markerFilePath, std::ios::binary};
if (not cle.is_open()) {
// This would be an unfortuane situation, the repo has already been encrypted
// This would be an unfortunate situation, the repo has already been encrypted
// but the encryption marker file is not added.
// TODO: figure something out
throw std::runtime_error{"Failed writing encryption marker file"};
Expand Down
4 changes: 2 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::string promptPassword(const caps_log::Config &config) {
const auto container = Container::Vertical({input, buttonSubmit, buttonQuit});

const Component renderer = Renderer(container, [container]() {
return window(text("Pasword required!"), container->Render()) | center;
return window(text("Password required!"), container->Render()) | center;
});

screen.Loop(renderer);
Expand Down Expand Up @@ -119,6 +119,6 @@ int main(int argc, const char **argv) try {

return 0;
} catch (const std::exception &e) {
std::cerr << "Captains log encountered an error: \n " << e.what() << '\n' << std::flush;
std::cerr << "Captain's log encountered an error: \n " << e.what() << '\n' << std::flush;
return 1;
}
4 changes: 2 additions & 2 deletions source/utils/async_git_repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class AsyncGitRepo final {

void commitAll(std::function<void(bool)> acallback) {
m_taskExec.post([this, callback = std::move(acallback)]() {
auto somethingCommited = m_repo.commitAll();
callback(somethingCommited);
auto somethingCommitted = m_repo.commitAll();
callback(somethingCommitted);
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion source/utils/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::string encrypt(const std::string &password, std::istream &file) {
}

if (EVP_EncryptFinal_ex(ctx.get(), outBuffer.data(), &outLength) != 1) {
throw std::runtime_error{"Encryption failed: failed to finaleze encryption!"};
throw std::runtime_error{"Encryption failed: failed to finalize encryption!"};
}
output += std::string{outBuffer.begin(), outBuffer.begin() + outLength};

Expand Down
2 changes: 1 addition & 1 deletion source/utils/git_repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace caps_log::utils {
/**
* A utility class that manages the initialization of libgit2 and other required interactions with
* the library. It deals with commit the files, pulling and pushing. It expect that the repository
* has an innitial commit, remote added and ssh communication.
* has an initial commit, remote added and ssh communication.
*/
class GitRepo {
class GitLibRaii {
Expand Down
2 changes: 1 addition & 1 deletion source/view/annual_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AnnualView : public AnnualViewBase {
InputHandlerBase *m_handler{nullptr};
ftxui::ScreenInteractive m_screen;

// UI compontents visible to the user
// UI components visible to the user
std::shared_ptr<Calendar> m_calendarButtons;
std::shared_ptr<WindowedMenu> m_tagsMenu, m_sectionsMenu;
std::shared_ptr<Preview> m_preview = std::make_unique<Preview>();
Expand Down
2 changes: 1 addition & 1 deletion source/view/annual_view_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class AnnualViewBase { // NOLINT
virtual void loadingScreenOff() = 0;

// passing only a pointer and having a view have no ownership of
// the map allows for having precoputed maps and switching
// the map allows for having precomputed maps and switching
virtual void setDatesWithLogs(const utils::date::Dates *map) = 0;
virtual void setHighlightedDates(const utils::date::Dates *map) = 0;
virtual void setEventDates(const CalendarEvents *map) {};
Expand Down
6 changes: 4 additions & 2 deletions source/view/calendar_component.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "calendar_component.hpp"

#include "ftxui/dom/elements.hpp"
#include "utils/date.hpp"
#include "view/ftxui_ext/extended_containers.hpp"

#include <ftxui/screen/screen.hpp>
#include <iostream>

namespace caps_log::view {
using namespace ftxui;
Expand All @@ -12,7 +14,7 @@ namespace {
* @brief Utility class that allows for the arrangement of month components in a calendar.
* It distributes 12 months into 2 or more rows of equal elements.
* @Note This class had to be implemented to allow for fetching the screen size at runtime to
* avoid relying on "Terimal::GetDimensions()" which is troublign in test environment.
* avoid relying on "Terminal::GetDimensions()" which is troubling in test environment.
* @Note the class makes some assumptions about the width of the rendered month components. Like
* that month component day buttons have a border and such
*/
Expand Down
5 changes: 1 addition & 4 deletions source/view/calendar_component.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

#include "utils/date.hpp"
#include "view/ftxui_ext/extended_containers.hpp"

#include <chrono>
#include <ftxui/component/component.hpp>
#include <ftxui/component/component_options.hpp>
Expand Down Expand Up @@ -41,7 +38,7 @@ class Calendar : public ftxui::ComponentBase {

/**
* @brief A utility factory method to create a shared pointer to a Calendar instance. This is
* usefull as FTXUI works with shared pointers to ComponentBase instances.
* useful as FTXUI works with shared pointers to ComponentBase instances.
*/
static inline auto make(const ftxui::Screen &screen, const std::chrono::year_month_day &today,
CalendarOption option = {}) {
Expand Down
1 change: 1 addition & 0 deletions test/calendar_component_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "utils/date.hpp"
#include "view/calendar_component.hpp"
#include <filesystem>
#include <fstream>
Expand Down

0 comments on commit 3e11dde

Please sign in to comment.