Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preview window title #70

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions source/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ using namespace log;
using namespace view;
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) {
if (event.date == date::monthDay(date)) {
return std::make_tuple(groupName, event);
}
}
}
return std::nullopt;
}();

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);
return title;
}

} // namespace

ViewDataUpdater::ViewDataUpdater(std::shared_ptr<AnnualViewBase> view, const AnnualLogData &data)
: m_view{std::move(view)}, m_data{data} {}

Expand Down Expand Up @@ -60,7 +85,8 @@ void ViewDataUpdater::updateTagMenuItemsPerSection() {
}
}

void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewString) {
void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewTitle,
const std::string &previewString) {
// update sections menu items
{
const auto oldSections = m_view->sectionMenuItems().getKeys();
Expand Down Expand Up @@ -117,7 +143,7 @@ void ViewDataUpdater::updateViewAfterDataChange(const std::string &previewString
}

// update preview string
m_view->setPreviewString(previewString);
m_view->setPreviewString(previewTitle, previewString);
}

MenuItems ViewDataUpdater::makeTagMenuItems(const std::string &section) {
Expand Down Expand Up @@ -168,7 +194,9 @@ void App::updateDataAndViewAfterLogChange(const std::chrono::year_month_day &dat
previewString = log->getContent();
}
}
m_viewDataUpdater.updateViewAfterDataChange(previewString);

m_viewDataUpdater.updateViewAfterDataChange(makePreviewTitle(dateOfChangedLog, m_config.events),
previewString);
}

App::App(std::shared_ptr<AnnualViewBase> view, std::shared_ptr<LogRepositoryBase> repo,
Expand Down Expand Up @@ -230,10 +258,12 @@ bool App::handleRootEvent(const std::string &input) {
}

void App::handleFocusedDateChange() {

const auto title = makePreviewTitle(m_view->getFocusedDate(), m_config.events);
if (auto log = m_repo->read(m_view->getFocusedDate())) {
m_view->setPreviewString(log->getContent());
m_view->setPreviewString(title, log->getContent());
} else {
m_view->setPreviewString("");
m_view->setPreviewString(title, "");
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <chrono>
#include <cstddef>
#include <memory>
#include <vector>

namespace caps_log {

Expand Down Expand Up @@ -39,7 +38,8 @@ class ViewDataUpdater final {

void handleFocusedTagChange();
void handleFocusedSectionChange();
void updateViewAfterDataChange(const std::string &previewString);
void updateViewAfterDataChange(const std::string &previewTitle,
const std::string &previewString);

private:
void updateTagMenuItemsPerSection();
Expand Down
4 changes: 3 additions & 1 deletion source/view/annual_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class AnnualView : public AnnualViewBase {
void setHighlightedDates(const utils::date::Dates *map) override { m_highlightedDates = map; }
void setEventDates(const CalendarEvents *events) override { m_eventDates = events; }

void setPreviewString(const std::string &string) override { m_preview->setContent(string); }
void setPreviewString(const std::string &title, const std::string &string) override {
m_preview->setContent(title, string);
}

void withRestoredIO(std::function<void()> func) override { m_screen.WithRestoredIO(func)(); }

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 @@ -78,7 +78,7 @@ class AnnualViewBase { // NOLINT
virtual MenuItems &tagMenuItems() = 0;
virtual MenuItems &sectionMenuItems() = 0;

virtual void setPreviewString(const std::string &string) = 0;
virtual void setPreviewString(const std::string &title, const std::string &string) = 0;

virtual void withRestoredIO(std::function<void()> func) = 0;

Expand Down
9 changes: 4 additions & 5 deletions source/view/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ Element Preview::Render() {
}

constexpr auto kHeight = 14;
if (Focused()) {
return vbox(visibleLines) | borderRounded | flex_shrink | size(HEIGHT, EQUAL, kHeight);
}
return vbox(visibleLines) | borderRounded | flex_shrink | size(HEIGHT, EQUAL, kHeight) | dim;
auto element = window(m_title, vbox(visibleLines)) | flex_shrink | size(HEIGHT, EQUAL, kHeight);
return Focused() ? element : element | dim;
}

bool Preview::Focusable() const { return true; }
Expand Down Expand Up @@ -42,7 +40,8 @@ bool Preview::OnEvent(Event event) {

void Preview::resetScroll() { m_topLineIndex = 0; }

void Preview::setContent(const std::string &str) {
void Preview::setContent(const std::string &title, const std::string &str) {
m_title = text(title) | underlined | center;
Elements lines;
std::istringstream input{str};
for (std::string line; std::getline(input, line);) {
Expand Down
3 changes: 2 additions & 1 deletion source/view/preview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ namespace caps_log::view {
class Preview : public ftxui::ComponentBase {
int m_topLineIndex = 0;
ftxui::Elements m_lines;
ftxui::Element m_title = ftxui::text("log preview");

public:
ftxui::Element Render() override;
bool Focusable() const override;
bool OnEvent(ftxui::Event event) override;

void resetScroll();
void setContent(const std::string &str);
void setContent(const std::string &title, const std::string &str);
};

} // namespace caps_log::view
14 changes: 9 additions & 5 deletions test/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DummyYearView : public caps_log::view::AnnualViewBase {
std::chrono::year m_displayedYear{};
std::chrono::year_month_day m_focusedDate{std::chrono::year{2005}, std::chrono::month{1},
std::chrono::day{1}};
std::string m_previewString;
std::string m_previewString, m_previewTitle;
const caps_log::utils::date::Dates *m_datesWithLogs{}, *m_highlightedDates{};
caps_log::view::MenuItems m_tagMenuItems, m_sectionMenuItems;
std::string m_selectedTag, m_selectedSection;
Expand Down Expand Up @@ -48,7 +48,10 @@ class DummyYearView : public caps_log::view::AnnualViewBase {
m_highlightedDates = map;
}

void setPreviewString(const std::string &string) override { m_previewString = string; }
void setPreviewString(const std::string &title, const std::string &string) override {
m_previewString = string;
m_previewTitle = title;
}
void withRestoredIO(std::function<void()> func) override { func(); }

caps_log::view::MenuItems &tagMenuItems() override { return m_tagMenuItems; }
Expand Down Expand Up @@ -81,8 +84,8 @@ class DMockYearView : public caps_log::view::AnnualViewBase {
ON_CALL(*this, prompt).WillByDefault([&](auto msg, auto callback) {
m_view.prompt(std::move(msg), std::move(callback));
});
ON_CALL(*this, setPreviewString).WillByDefault([&](const auto &str) {
m_view.setPreviewString(str);
ON_CALL(*this, setPreviewString).WillByDefault([&](const auto &title, const auto &str) {
m_view.setPreviewString(title, str);
});

ON_CALL(*this, tagMenuItems).WillByDefault([&]() -> auto & {
Expand Down Expand Up @@ -124,7 +127,8 @@ class DMockYearView : public caps_log::view::AnnualViewBase {
MOCK_METHOD(void, setDatesWithLogs, (const caps_log::utils::date::Dates *map), (override));
MOCK_METHOD(void, setHighlightedDates, (const caps_log::utils::date::Dates *map), (override));

MOCK_METHOD(void, setPreviewString, (const std::string &string), (override));
MOCK_METHOD(void, setPreviewString, (const std::string &titile, const std::string &string),
(override));
MOCK_METHOD(void, withRestoredIO, (std::function<void()> func), (override));

MOCK_METHOD(caps_log::view::MenuItems &, tagMenuItems, (), (override));
Expand Down
Loading