Trigger re-render when pushing to the log
This commit is contained in:
@ -18,34 +18,52 @@ use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use matrix_sdk::async_trait;
|
||||
use ratatui::text::Text;
|
||||
use tokio::sync::mpsc::UnboundedReceiver;
|
||||
use tracing_error::ErrorLayer;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
use super::Buffer;
|
||||
|
||||
/// Maximum number of log lines to be stored in memory
|
||||
const MAX_MEM_LOG_LINES: usize = 100;
|
||||
|
||||
pub struct LogBuffer {
|
||||
lines: &'static RwLock<VecDeque<String>>,
|
||||
lines: VecDeque<String>,
|
||||
receiver: UnboundedReceiver<String>,
|
||||
}
|
||||
|
||||
impl LogBuffer {
|
||||
pub fn new(lines: &'static RwLock<VecDeque<String>>) -> Self {
|
||||
LogBuffer { lines }
|
||||
pub fn new(receiver: UnboundedReceiver<String>) -> Self {
|
||||
LogBuffer {
|
||||
lines: VecDeque::new(),
|
||||
receiver,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Buffer for LogBuffer {
|
||||
fn short_name(&self) -> String {
|
||||
"ratatrix".to_owned()
|
||||
}
|
||||
|
||||
async fn poll_updates(&mut self) {
|
||||
let line = self
|
||||
.receiver
|
||||
.recv()
|
||||
.await
|
||||
.expect("LogBuffer's channel was closed");
|
||||
if self.lines.len() >= MAX_MEM_LOG_LINES {
|
||||
self.lines.pop_front();
|
||||
}
|
||||
self.lines.push_back(line);
|
||||
}
|
||||
|
||||
fn content(&self) -> Vec<Text> {
|
||||
use ansi_to_tui::IntoText;
|
||||
let lines = self
|
||||
.lines
|
||||
.read()
|
||||
.expect("LogBuffer could not get log's RwLock as it is poisoned");
|
||||
let (slice1, slice2) = lines.as_slices();
|
||||
let (slice1, slice2) = self.lines.as_slices();
|
||||
slice1
|
||||
.into_iter()
|
||||
.chain(slice2.into_iter())
|
||||
|
Reference in New Issue
Block a user