Redraw whenever a message is logged
This commit is contained in:
parent
fa746d0749
commit
e0db158ece
4 changed files with 48 additions and 20 deletions
|
|
@ -5,6 +5,7 @@ use async_trait::async_trait;
|
|||
use chrono::{DateTime, Utc};
|
||||
use log::{Level, Log};
|
||||
use parking_lot::Mutex;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::store::{Msg, MsgStore, Path, Tree};
|
||||
|
||||
|
|
@ -41,7 +42,10 @@ impl Msg for LogMsg {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Logger(Arc<Mutex<Vec<LogMsg>>>);
|
||||
pub struct Logger {
|
||||
event_tx: mpsc::UnboundedSender<()>,
|
||||
messages: Arc<Mutex<Vec<LogMsg>>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MsgStore<LogMsg> for Logger {
|
||||
|
|
@ -51,7 +55,7 @@ impl MsgStore<LogMsg> for Logger {
|
|||
|
||||
async fn tree(&self, root: &usize) -> Tree<LogMsg> {
|
||||
let msgs = self
|
||||
.0
|
||||
.messages
|
||||
.lock()
|
||||
.get(*root)
|
||||
.map(|msg| vec![msg.clone()])
|
||||
|
|
@ -64,17 +68,17 @@ impl MsgStore<LogMsg> for Logger {
|
|||
}
|
||||
|
||||
async fn next_tree(&self, tree: &usize) -> Option<usize> {
|
||||
let len = self.0.lock().len();
|
||||
let len = self.messages.lock().len();
|
||||
tree.checked_add(1).filter(|t| *t < len)
|
||||
}
|
||||
|
||||
async fn first_tree(&self) -> Option<usize> {
|
||||
let empty = self.0.lock().is_empty();
|
||||
let empty = self.messages.lock().is_empty();
|
||||
Some(0).filter(|_| !empty)
|
||||
}
|
||||
|
||||
async fn last_tree(&self) -> Option<usize> {
|
||||
self.0.lock().len().checked_sub(1)
|
||||
self.messages.lock().len().checked_sub(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +92,7 @@ impl Log for Logger {
|
|||
return;
|
||||
}
|
||||
|
||||
let mut guard = self.0.lock();
|
||||
let mut guard = self.messages.lock();
|
||||
let msg = LogMsg {
|
||||
id: guard.len(),
|
||||
time: Utc::now(),
|
||||
|
|
@ -96,18 +100,24 @@ impl Log for Logger {
|
|||
content: format!("<{}> {}", record.target(), record.args()),
|
||||
};
|
||||
guard.push(msg);
|
||||
|
||||
let _ = self.event_tx.send(());
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn init(level: Level) -> &'static Self {
|
||||
let logger = Box::leak(Box::new(Self(Arc::new(Mutex::new(Vec::new())))));
|
||||
pub fn init(level: Level) -> (Self, mpsc::UnboundedReceiver<()>) {
|
||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||
let logger = Self {
|
||||
event_tx,
|
||||
messages: Arc::new(Mutex::new(Vec::new())),
|
||||
};
|
||||
|
||||
log::set_logger(logger).expect("logger already set");
|
||||
log::set_boxed_logger(Box::new(logger.clone())).expect("logger already set");
|
||||
log::set_max_level(level.to_level_filter());
|
||||
|
||||
logger
|
||||
(logger, event_rx)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ use crate::logger::Logger;
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let logger = Logger::init(log::Level::Debug);
|
||||
let (logger, logger_rx) = Logger::init(log::Level::Debug);
|
||||
info!(
|
||||
"Welcome to {} {}",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
|
|
@ -31,7 +31,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let mut terminal = Terminal::new()?;
|
||||
// terminal.set_measuring(true);
|
||||
Ui::run(&mut terminal, logger.clone()).await?;
|
||||
Ui::run(&mut terminal, logger, logger_rx).await?;
|
||||
drop(terminal); // So the vault can print again
|
||||
|
||||
vault.close().await;
|
||||
|
|
|
|||
32
src/ui.rs
32
src/ui.rs
|
|
@ -11,6 +11,7 @@ use toss::frame::{Frame, Pos, Size};
|
|||
use toss::terminal::Terminal;
|
||||
|
||||
use crate::chat::Chat;
|
||||
use crate::euph;
|
||||
use crate::logger::{LogMsg, Logger};
|
||||
use crate::store::dummy::{DummyMsg, DummyStore};
|
||||
|
||||
|
|
@ -34,6 +35,7 @@ pub struct Ui {
|
|||
event_tx: UnboundedSender<UiEvent>,
|
||||
|
||||
visible: Visible,
|
||||
room: euph::Room,
|
||||
chat: Chat<DummyMsg, DummyStore>,
|
||||
log_chat: Chat<LogMsg, Logger>,
|
||||
}
|
||||
|
|
@ -41,7 +43,11 @@ pub struct Ui {
|
|||
impl Ui {
|
||||
const POLL_DURATION: Duration = Duration::from_millis(100);
|
||||
|
||||
pub async fn run(terminal: &mut Terminal, logger: Logger) -> anyhow::Result<()> {
|
||||
pub async fn run(
|
||||
terminal: &mut Terminal,
|
||||
logger: Logger,
|
||||
logger_rx: mpsc::UnboundedReceiver<()>,
|
||||
) -> anyhow::Result<()> {
|
||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||
let crossterm_lock = Arc::new(FairMutex::new(()));
|
||||
|
||||
|
|
@ -77,16 +83,17 @@ impl Ui {
|
|||
// On the other hand, if the crossterm_event_task stops for any reason,
|
||||
// the rest of the UI is also shut down and the client stops.
|
||||
let mut ui = Self {
|
||||
event_tx,
|
||||
event_tx: event_tx.clone(),
|
||||
visible: Visible::Log,
|
||||
room: euph::Room::new("test".to_string()),
|
||||
chat,
|
||||
log_chat: Chat::new(logger),
|
||||
};
|
||||
let result = tokio::select! {
|
||||
e = ui.run_main(terminal, event_rx, crossterm_lock) => e,
|
||||
Ok(e) = crossterm_event_task => e,
|
||||
};
|
||||
result
|
||||
tokio::select! {
|
||||
e = ui.run_main(terminal, event_rx, crossterm_lock) => Ok(e),
|
||||
_ = Self::update_on_log_event(logger_rx, &event_tx) => Ok(Ok(())),
|
||||
e = crossterm_event_task => e,
|
||||
}?
|
||||
}
|
||||
|
||||
fn poll_crossterm_events(
|
||||
|
|
@ -103,6 +110,17 @@ impl Ui {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_on_log_event(
|
||||
mut logger_rx: mpsc::UnboundedReceiver<()>,
|
||||
event_tx: &mpsc::UnboundedSender<UiEvent>,
|
||||
) {
|
||||
while let Some(()) = logger_rx.recv().await {
|
||||
if event_tx.send(UiEvent::Redraw).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_main(
|
||||
&mut self,
|
||||
terminal: &mut Terminal,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue