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