Make history-related constants configurable
Some checks failed
CI / lint (push) Failing after 59s
CI / Build and test (, 1.73.0) (push) Failing after 5m1s
CI / Build and test (, beta) (push) Failing after 5m8s
CI / Build and test (, nightly) (push) Failing after 4m46s

This commit is contained in:
Val Lorentz 2023-11-22 16:51:29 +01:00
parent 501ccc007e
commit d79f5d7527
8 changed files with 60 additions and 14 deletions

View File

@ -39,7 +39,7 @@ use crate::{
};
pub struct App {
pub config: Config,
pub config: Arc<Config>,
pub clients: NonEmpty<matrix_sdk::Client>,
pub commands: RataCommands,
pub tick_rate: f64,
@ -120,6 +120,7 @@ impl App {
}
let clients = NonEmpty::collect(clients).expect("map on NonEmpty returned empty vec");
let config = Arc::new(config);
let home = Home::new(config.clone());
let fps = FpsCounter::default();
let mode = Mode::Home;
@ -398,7 +399,7 @@ impl App {
) {
futures::future::join_all(
rooms
.map(|(client, room)| RoomBuffer::new(client, room))
.map(|(client, room)| RoomBuffer::new(self.config.clone(), client, room))
.map(|fut| fut.map(|res| res.expect("Failed to create RoomBuffer at startup"))),
)
.await

View File

@ -47,6 +47,7 @@ use sorted_vec::SortedVec;
use tokio::sync::oneshot;
use super::{Buffer, BufferId, BufferItem, BufferItemContent, BufferSortKey, FullyReadStatus};
use crate::config::Config;
use crate::widgets::Prerender;
/// Like [`BufferItemContent`] but owned.
@ -368,6 +369,8 @@ impl SingleClientRoomBuffer {
}
pub struct RoomBuffer {
config: Arc<Config>,
room_id: OwnedRoomId,
computed_roominfo: Option<ComputedRoomInfo>,
@ -400,8 +403,13 @@ impl DynamicUsage for RoomBuffer {
}
impl RoomBuffer {
pub async fn new(initial_client: Client, room_id: OwnedRoomId) -> Result<Self> {
pub async fn new(
config: Arc<Config>,
initial_client: Client,
room_id: OwnedRoomId,
) -> Result<Self> {
let mut self_ = RoomBuffer {
config,
room_id,
computed_roominfo: None,
update_roominfo_rx: None,
@ -674,8 +682,12 @@ impl Buffer for RoomBuffer {
// * we already backfilled a lot so we don't exhaust the available memory
// * we already reached an unread message, in which case it's pointless to
// backfill as we already know there are unread messages.
if buf.items.len() < 1000 {
buf.back_pagination_request.fetch_max(50, Ordering::Relaxed);
if buf.items.len() < self.config.history.max_prefetch_unread
&& self.config.history.prefetch_unread > 0
{
buf
.back_pagination_request
.fetch_max(self.config.history.prefetch_unread, Ordering::Relaxed);
}
}
}

View File

@ -15,6 +15,7 @@
*/
use std::ops::DerefMut;
use std::sync::Arc;
use color_eyre::eyre::{Result, WrapErr};
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
@ -41,7 +42,7 @@ struct ScrollPosition {
#[derive(Debug)]
pub struct Backlog {
config: Config,
config: Arc<Config>,
/// Used to compute scroll on PageUp/PageDown when configured to a percentage.
last_height: u16,
scroll_position: Option<ScrollPosition>,
@ -97,8 +98,10 @@ impl Component for Backlog {
// We are reaching the end of the backlog we have locally, ask the buffer to fetch
// more if it can
if undrawn_widgets_at_top <= 100 {
active_buffer.request_back_pagination(200);
if undrawn_widgets_at_top <= self.config.history.min_prefetch
&& self.config.history.prefetch > 0
{
active_buffer.request_back_pagination(self.config.history.prefetch);
}
Ok(())
@ -106,7 +109,7 @@ impl Component for Backlog {
}
impl Backlog {
pub fn new(config: Config) -> Self {
pub fn new(config: Arc<Config>) -> Self {
Backlog {
config,
last_height: 30, // Arbitrary default, only useful when user scrolls before first render

View File

@ -14,6 +14,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
use color_eyre::eyre::{Result, WrapErr};
use crossterm::event::{MouseEvent, MouseEventKind};
use ratatui::{prelude::*, widgets::*};
@ -29,14 +31,14 @@ use crate::{
pub struct Buflist {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
config: Arc<Config>,
/// Updated by [`draw`], used by [`handle_mouse_events`] to find which buffer was clicked.
last_area: Option<Rect>,
last_num_buffers: Option<usize>,
}
impl Buflist {
pub fn new(config: Config) -> Self {
pub fn new(config: Arc<Config>) -> Self {
Buflist {
command_tx: None,
config,

View File

@ -14,6 +14,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
use std::{collections::HashMap, time::Duration};
use color_eyre::eyre::{Result, WrapErr};
@ -32,14 +33,14 @@ use crate::{
pub struct Home {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
config: Arc<Config>,
buflist: Buflist,
backlog: Backlog,
textarea: TextArea<'static>,
}
impl Home {
pub fn new(config: Config) -> Self {
pub fn new(config: Arc<Config>) -> Self {
let mut self_ = Home {
command_tx: None,
buflist: Buflist::new(config.clone()),

View File

@ -102,6 +102,14 @@ impl<'de> Visitor<'de> for ScrollAmountVisitor {
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct HistoryConfig {
pub prefetch_unread: u16,
pub max_prefetch_unread: usize,
pub min_prefetch: usize,
pub prefetch: u16,
}
#[derive(Clone, Debug, Deserialize)]
pub struct MouseConfig {
pub enable: bool,
@ -149,6 +157,7 @@ pub struct Config {
#[serde(default, flatten)]
pub config: AppConfig,
pub accounts: nonempty::NonEmpty<AccountConfig>,
pub history: HistoryConfig,
#[serde(default)]
pub keybindings: KeyBindings,
pub keyboard: KeyboardConfig,

View File

@ -14,6 +14,23 @@ scroll_page = "50%"
[mouse]
enable = true
[history]
# When there are unread events in a room, how many past events should be fetched
# at once to check whether there are unread messages (as opposed to non-message
# events), to find if the room should colored with style.buflist.unread_message or
# style.buflist.unread_event
prefetch_unread = 50
# When do stop iteratively fetching pages of size $prefetch_unread when no unread
# messages are found
max_prefetch_unread = 1000
# How many older events to keep above the screen in case the user hits PageUp later.
# Set to 0 to only fetch when the user reaches an unfetched message.
min_prefetch = 100
# When getting close to the oldest event we know about, how many events should be
# fetched at once
prefetch = 200
# Configuration for the list of rooms on the right.
[layout.buflist]
# How long room names can be before being truncated.

View File

@ -14,6 +14,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
use std::path::PathBuf;
use color_eyre::eyre::WrapErr;
@ -29,7 +30,7 @@ fn config() -> Config {
std::env::set_var("RATATRIX_CONFIG", PathBuf::from(".config/"));
let c = Config::new();
std::env::remove_var("RATATRIX_CONFIG");
c.unwrap()
Arc::new(c.unwrap())
}
fn rect(x: u16, y: u16, width: u16, height: u16) -> Rect {