Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
fix(screens/log): jump-to-top if entries_.size() > capacity()
Browse files Browse the repository at this point in the history
Resolves #89
  • Loading branch information
tmplt committed Jul 10, 2019
1 parent d7982c2 commit 80900e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org)
* Screens/details: Correctly print empty fields.
* Screens/index: Correctly print Unicode strings. #83
* Screens/log: Possible MT-related crashes when marking log entries as seen.
* Screens/log: Correctly jump-to-top if entries do not fit in screen. #89

## [v0.8.0] - 2019-05-26

Expand Down
16 changes: 14 additions & 2 deletions src/tui/screens/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace bookwyrm::tui::screen {
});
}

/* Figure out how many entries we can fit on screen given an entry to start from. */
/* Figure out how many entries we can fit on screen given a reverse entry to start from. */
int log::capacity(const entry_ri &start) const
{
assert(!entries_mutex_.try_lock());
Expand All @@ -57,6 +57,18 @@ namespace bookwyrm::tui::screen {
});
}

/* Figure out how many entries we can fit on screen given a forward entry to start from. */
int log::fcapacity(const entry_i &start) const
{
assert(!entries_mutex_.try_lock());

int lines = get_height();
return std::count_if(start, cend(entries_), [&](const auto e) {
lines -= std::ceil(static_cast<double>(e.second.length()) / get_width());
return lines >= 0;
});
}

void log::paint()
{
erase();
Expand Down Expand Up @@ -204,7 +216,7 @@ namespace bookwyrm::tui::screen {
(*detached_at_)--;
break;
case top:
detached_at_ = entries_.crend() - 1;
detached_at_ = entries_.crend() - fcapacity(entries_.cbegin());
break;
case bot:
detached_at_ = entries_.crbegin();
Expand Down
10 changes: 9 additions & 1 deletion src/tui/screens/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ namespace bookwyrm::tui::screen {
std::function<bool(void)> is_log_focused_;

std::vector<core::log_pair> entries_, unread_entries_;

using entry_ri = decltype(entries_.crbegin());
using entry_i = decltype(entries_.cbegin());
std::optional<entry_ri> detached_at_;

/*
* Entries will be modified both by calls to `log_entry()` from plugins
* and by calls to `mark_read()` when the log is toggled.
* Protects `{,unread_}entries_` and `detached_at_` above.
*/
mutable std::mutex entries_mutex_;

void maybe_update_detached(std::function<void()> &&fun);
int capacity(const entry_ri &start) const;
int fcapacity(const entry_i &start) const;
void print_entry(int &y, const entry_ri entry);
};

Expand Down

0 comments on commit 80900e4

Please sign in to comment.