From 80900e4cd0b9debdd51867a7cd1b24a6ed5f36f8 Mon Sep 17 00:00:00 2001 From: Tmplt Date: Wed, 10 Jul 2019 10:35:21 +0200 Subject: [PATCH] fix(screens/log): jump-to-top if entries_.size() > capacity() Resolves #89 --- CHANGELOG.md | 1 + src/tui/screens/log.cpp | 16 ++++++++++++++-- src/tui/screens/log.hpp | 10 +++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea9c5e5..225a3be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/tui/screens/log.cpp b/src/tui/screens/log.cpp index 87813d7..a044523 100644 --- a/src/tui/screens/log.cpp +++ b/src/tui/screens/log.cpp @@ -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()); @@ -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(e.second.length()) / get_width()); + return lines >= 0; + }); + } + void log::paint() { erase(); @@ -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(); diff --git a/src/tui/screens/log.hpp b/src/tui/screens/log.hpp index 9a3602e..a599a41 100644 --- a/src/tui/screens/log.hpp +++ b/src/tui/screens/log.hpp @@ -31,12 +31,20 @@ namespace bookwyrm::tui::screen { std::function is_log_focused_; std::vector entries_, unread_entries_; - using entry_ri = decltype(entries_.crbegin()); + using entry_i = decltype(entries_.cbegin()); std::optional 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 &&fun); int capacity(const entry_ri &start) const; + int fcapacity(const entry_i &start) const; void print_entry(int &y, const entry_ri entry); };