147 lines
3.9 KiB
Rust
147 lines
3.9 KiB
Rust
use std::{io, sync::Arc};
|
|
|
|
use joecalsend::{Client, JoecalState, error, models::device::DeviceInfo};
|
|
use local_ip_address::local_ip;
|
|
use network_interface::{Addr, NetworkInterface, NetworkInterfaceConfig, V4IfAddr};
|
|
use ratatui::{
|
|
DefaultTerminal, Frame,
|
|
buffer::Buffer,
|
|
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
|
|
layout::Rect,
|
|
style::Stylize,
|
|
symbols::border,
|
|
text::{Line, Text},
|
|
widgets::{Block, Paragraph, Widget},
|
|
};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> error::Result<()> {
|
|
let device = DeviceInfo::default();
|
|
dbg!(&device);
|
|
|
|
let std::net::IpAddr::V4(ip) = local_ip()? else {
|
|
unreachable!()
|
|
};
|
|
|
|
// for enumerating subnet peers when multicast fails (https://github.com/localsend/protocol?tab=readme-ov-file#32-http-legacy-mode)
|
|
let mut network_ip = ip;
|
|
let nifs = NetworkInterface::show().unwrap();
|
|
for addr in nifs.into_iter().flat_map(|i| i.addr) {
|
|
if let Addr::V4(V4IfAddr {
|
|
ip: ifip,
|
|
netmask: Some(netmask),
|
|
..
|
|
}) = addr
|
|
&& ip == ifip
|
|
{
|
|
network_ip = ip & netmask;
|
|
break;
|
|
}
|
|
}
|
|
dbg!(network_ip);
|
|
|
|
let client = Client::with_config(device, 53317, "/home/ardent/joecalsend".into())
|
|
.await
|
|
.unwrap();
|
|
let (h1, h2, h3) = client.start().await.unwrap();
|
|
|
|
let mut app = App::new(Arc::new(client.clone()));
|
|
let mut terminal = ratatui::init();
|
|
let result = app.run(&mut terminal);
|
|
ratatui::restore();
|
|
//let _ = tokio::join!(h1, h2, h3);
|
|
|
|
Ok(result?)
|
|
}
|
|
|
|
struct App {
|
|
client: Arc<Client>,
|
|
runstate: AppRunState,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum AppRunState {
|
|
Running,
|
|
Stopping,
|
|
}
|
|
|
|
impl Default for AppRunState {
|
|
fn default() -> Self {
|
|
Self::Running
|
|
}
|
|
}
|
|
|
|
impl App {
|
|
pub fn new(client: Arc<Client>) -> Self {
|
|
App {
|
|
client,
|
|
runstate: AppRunState::Running,
|
|
}
|
|
}
|
|
|
|
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
|
|
while AppRunState::Running == self.runstate {
|
|
terminal.draw(|frame| self.draw(frame))?;
|
|
self.handle_events()?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn draw(&self, frame: &mut Frame) {
|
|
frame.render_widget(self, frame.area());
|
|
}
|
|
|
|
fn handle_events(&mut self) -> io::Result<()> {
|
|
match event::read()? {
|
|
// it's important to check that the event is a key press event as
|
|
// crossterm also emits key release and repeat events on Windows.
|
|
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
|
self.handle_key_event(key_event)
|
|
}
|
|
_ => {}
|
|
};
|
|
Ok(())
|
|
}
|
|
|
|
fn handle_key_event(&mut self, key_event: KeyEvent) {
|
|
match key_event.code {
|
|
KeyCode::Char('q') => self.exit(),
|
|
KeyCode::Char('s') => {}
|
|
KeyCode::Char('r') => {}
|
|
KeyCode::Char('d') => {}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn exit(&mut self) {
|
|
self.runstate = AppRunState::Stopping
|
|
}
|
|
}
|
|
|
|
impl Widget for &App {
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
|
let title = Line::from(" Counter App Tutorial ".bold());
|
|
let instructions = Line::from(vec![
|
|
" Send ".into(),
|
|
"<S>".blue().bold(),
|
|
" Receive ".into(),
|
|
"<R>".blue().bold(),
|
|
" Discover ".into(),
|
|
"<D>".blue().bold(),
|
|
" Quit ".into(),
|
|
"<Q> ".blue().bold(),
|
|
]);
|
|
let block = Block::bordered()
|
|
.title(title.centered())
|
|
.title_bottom(instructions.centered())
|
|
.border_set(border::THICK);
|
|
|
|
let rs = format!("{:?}", self.runstate);
|
|
let state_text = Text::from(vec![Line::from(vec!["runstate: ".into(), rs.yellow()])]);
|
|
|
|
Paragraph::new(state_text)
|
|
.centered()
|
|
.block(block)
|
|
.render(area, buf);
|
|
}
|
|
}
|