add logging screen, clean up widget rendering
This commit is contained in:
parent
1e64c5b1e8
commit
9b2172aa3a
2 changed files with 84 additions and 64 deletions
|
@ -8,7 +8,7 @@ use axum::{
|
|||
Json,
|
||||
extract::{ConnectInfo, State},
|
||||
};
|
||||
use log::{error, warn};
|
||||
use log::{debug, error, warn};
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
use crate::{Config, JoecalState, RunningState, models::Device};
|
||||
|
@ -19,6 +19,7 @@ impl JoecalState {
|
|||
socket: Option<SocketAddr>,
|
||||
config: &Config,
|
||||
) -> crate::error::Result<()> {
|
||||
debug!("announcing");
|
||||
announce_http(&self.device, socket, self.client.clone()).await?;
|
||||
announce_multicast(&self.device, config.multicast_addr, self.socket.clone()).await?;
|
||||
Ok(())
|
||||
|
@ -38,10 +39,12 @@ impl JoecalState {
|
|||
};
|
||||
if rstate == RunningState::Stopping
|
||||
{
|
||||
debug!("stopping multicast listen");
|
||||
break;
|
||||
}
|
||||
},
|
||||
r = self.socket.recv_from(&mut buf) => {
|
||||
debug!("received multicast datagram");
|
||||
match r {
|
||||
Ok((size, src)) => {
|
||||
let received_msg = String::from_utf8_lossy(&buf[..size]);
|
||||
|
|
|
@ -46,6 +46,7 @@ pub enum CurrentScreen {
|
|||
Sending,
|
||||
Receiving,
|
||||
Stopping,
|
||||
Logging,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
|
@ -165,6 +166,7 @@ impl App {
|
|||
KeyCode::Char('q') => self.exit(),
|
||||
KeyCode::Char('s') => self.send(),
|
||||
KeyCode::Char('r') => self.recv(),
|
||||
KeyCode::Char('l') => self.logs(),
|
||||
KeyCode::Esc => self.pop(),
|
||||
_ => {}
|
||||
}
|
||||
|
@ -194,6 +196,14 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
fn logs(&mut self) {
|
||||
let last = self.screen.last();
|
||||
match last {
|
||||
Some(CurrentScreen::Logging) => {}
|
||||
_ => self.screen.push(CurrentScreen::Logging),
|
||||
}
|
||||
}
|
||||
|
||||
fn pop(&mut self) {
|
||||
self.screen.pop();
|
||||
if self.screen.last().is_none() {
|
||||
|
@ -204,50 +214,73 @@ impl App {
|
|||
|
||||
impl Widget for &App {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
let [top, middle, bottom] = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([Constraint::Min(10), Constraint::Min(10), Constraint::Min(3)])
|
||||
let [top, middle, bottom] =
|
||||
Layout::vertical([Constraint::Min(5), Constraint::Min(10), Constraint::Min(3)])
|
||||
.split(area)
|
||||
.as_array()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
|
||||
let [footer_left, footer_right] = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
let [footer_left, footer_right] =
|
||||
Layout::horizontal([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(bottom)
|
||||
.as_array()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
|
||||
let [header_left, header_right] = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
let [header_left, header_right] =
|
||||
Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
.split(top)
|
||||
.as_array()
|
||||
.cloned()
|
||||
.unwrap();
|
||||
|
||||
let mode = self.screen.last().unwrap();
|
||||
match mode {
|
||||
CurrentScreen::Main => {
|
||||
main_page(*mode, area, buf);
|
||||
logger(header_right.inner(Margin::new(1, 2)), buf);
|
||||
let peers = PeersWidget { peers: &self.peers };
|
||||
peers.render(footer_right.inner(Margin::new(1, 1)), buf);
|
||||
NetworkInfoWidget.render(footer_left.inner(Margin::new(1, 1)), buf);
|
||||
}
|
||||
CurrentScreen::Receiving => {
|
||||
main_page(*mode, area, buf);
|
||||
}
|
||||
CurrentScreen::Logging => {
|
||||
main_page(*mode, area, buf);
|
||||
logger(area.inner(Margin::new(2, 4)), buf);
|
||||
}
|
||||
_ => {
|
||||
main_page(*mode, area, buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn logger(area: Rect, buf: &mut Buffer) {
|
||||
let logger = TuiLoggerWidget::default()
|
||||
.output_separator('|')
|
||||
.output_timestamp(Some("%F %H:%M:%S%.3f".to_string()))
|
||||
.output_timestamp(Some("%H:%M:%S%.3f".to_string()))
|
||||
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
|
||||
.output_target(false)
|
||||
.output_target(true)
|
||||
.output_file(false)
|
||||
.output_line(false)
|
||||
.block(Block::bordered().border_set(border::THICK))
|
||||
.style(Style::default())
|
||||
.state(&TuiWidgetState::new().set_default_display_level(LevelFilter::Debug));
|
||||
logger.render(area, buf);
|
||||
}
|
||||
|
||||
{
|
||||
fn main_page(screen: CurrentScreen, area: Rect, buf: &mut Buffer) {
|
||||
let title = Line::from(" Joecalsend ".bold());
|
||||
let instructions = Line::from(vec![
|
||||
" Send ".into(),
|
||||
"<S>".blue().bold(),
|
||||
" Receive ".into(),
|
||||
"<R>".blue().bold(),
|
||||
" Discover ".into(),
|
||||
"<D>".blue().bold(),
|
||||
" Logs ".into(),
|
||||
"<L>".blue().bold(),
|
||||
" Quit ".into(),
|
||||
"<Q> ".blue().bold(),
|
||||
]);
|
||||
|
@ -256,10 +289,7 @@ impl Widget for &App {
|
|||
.title_bottom(instructions.centered())
|
||||
.border_set(border::THICK);
|
||||
|
||||
let current_screen = format!(
|
||||
"{:?}",
|
||||
self.screen.last().copied().unwrap_or(CurrentScreen::Main)
|
||||
);
|
||||
let current_screen = format!("{screen:?}",);
|
||||
let text = Text::from(Line::from(current_screen.yellow()));
|
||||
|
||||
Paragraph::new(text)
|
||||
|
@ -267,19 +297,6 @@ impl Widget for &App {
|
|||
.block(block)
|
||||
.render(area, buf);
|
||||
}
|
||||
let mode = self.screen.last().unwrap();
|
||||
match mode {
|
||||
CurrentScreen::Main => {
|
||||
let peers = PeersWidget { peers: &self.peers };
|
||||
peers.render(footer_right.inner(Margin::new(1, 1)), buf);
|
||||
NetworkInfoWidget.render(footer_left.inner(Margin::new(1, 1)), buf);
|
||||
logger.render(header_right.inner(Margin::new(1, 2)), buf);
|
||||
}
|
||||
CurrentScreen::Receiving => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn shutdown(handles: &mut JoinSet<Listeners>) {
|
||||
let mut alarm = tokio::time::interval(tokio::time::Duration::from_secs(5));
|
||||
|
|
Loading…
Reference in a new issue