joecalsend/src/app/mod.rs

226 lines
6.2 KiB
Rust
Raw Normal View History

use std::{
collections::BTreeMap,
net::SocketAddr,
path::{Path, PathBuf},
};
use crossterm::event::{Event, EventStream, KeyEventKind};
use futures::{FutureExt, StreamExt};
use jocalsend::{JocalEvent, JocalService, ReceiveRequest, error::Result};
2025-08-01 16:16:05 +00:00
use julid::Julid;
use log::LevelFilter;
2025-08-03 23:43:25 +00:00
use ratatui::{
Frame,
widgets::{ListState, TableState, WidgetRef},
2025-08-03 23:43:25 +00:00
};
use ratatui_explorer::FileExplorer;
use simsearch::{SearchOptions, SimSearch};
2025-08-02 18:28:57 +00:00
use tokio::sync::mpsc::UnboundedReceiver;
use tui_input::Input;
2025-07-28 22:51:00 +00:00
pub mod widgets;
mod handle;
2025-08-03 23:43:25 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Peer {
pub alias: String,
pub fingerprint: String,
pub addr: SocketAddr,
}
#[derive(Clone)]
struct FileFinder {
explorer: FileExplorer,
fuzzy: SimSearch<usize>,
working_dir: Option<PathBuf>,
input: Input,
}
fn searcher() -> SimSearch<usize> {
SimSearch::new_with(
SearchOptions::new()
.stop_words(vec![std::path::MAIN_SEPARATOR_STR.to_string()])
.stop_whitespace(false),
)
}
pub struct App {
2025-08-06 21:09:37 +00:00
pub service: JocalService,
pub events: EventStream,
2025-08-03 23:43:25 +00:00
pub peers: Vec<Peer>,
pub peer_state: ListState,
pub receive_requests: BTreeMap<Julid, ReceiveRequest>,
2025-08-06 01:57:59 +00:00
screen: Vec<CurrentScreen>,
receiving_state: TableState,
// for getting messages back from the web server or web client about things we've done; the
2025-08-03 05:34:35 +00:00
// other end is held by the service
2025-08-14 00:48:55 +00:00
event_listener: UnboundedReceiver<JocalEvent>,
file_finder: FileFinder,
2025-08-04 17:52:58 +00:00
text: Option<String>,
2025-08-06 01:57:59 +00:00
input: Input,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CurrentScreen {
Main,
2025-08-03 19:16:38 +00:00
Sending(SendingScreen),
Receiving,
Stopping,
Logging,
}
2025-08-03 19:16:38 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SendingScreen {
Files(FileMode),
2025-08-03 19:16:38 +00:00
Peers,
Text,
2025-08-03 19:16:38 +00:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileMode {
Picking,
Fuzzy,
}
impl App {
2025-08-14 00:48:55 +00:00
pub fn new(service: JocalService, event_listener: UnboundedReceiver<JocalEvent>) -> Self {
App {
service,
event_listener,
screen: vec![CurrentScreen::Main],
file_finder: FileFinder::new().expect("could not create file explorer"),
2025-08-05 00:20:28 +00:00
text: None,
events: Default::default(),
2025-08-01 20:52:13 +00:00
peers: Default::default(),
2025-08-03 23:43:25 +00:00
peer_state: Default::default(),
receive_requests: Default::default(),
receiving_state: Default::default(),
2025-08-06 01:57:59 +00:00
input: Default::default(),
}
}
2025-08-02 18:28:57 +00:00
pub async fn handle_events(&mut self) -> Result<()> {
tokio::select! {
event = self.events.next().fuse() => {
if let Some(Ok(evt)) = event {
match evt {
Event::Key(key)
if key.kind == KeyEventKind::Press
=> self.handle_key_event(key, evt).await,
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
_ => {}
}
}
}
2025-08-08 20:56:50 +00:00
transfer_event = self.event_listener.recv().fuse() => {
if let Some(event) = transfer_event {
2025-08-14 23:25:32 +00:00
log::trace!("got JocalEvent {event:?}");
match event {
2025-08-14 00:48:55 +00:00
JocalEvent::ReceiveRequest { id, request } => {
self.receive_requests.insert(id, request);
}
2025-08-14 23:25:32 +00:00
JocalEvent::Cancelled { session_id: id } | JocalEvent::ReceivedInbound(id) => {
self.receive_requests.remove(&id);
2025-08-01 16:16:05 +00:00
}
2025-08-14 23:25:32 +00:00
JocalEvent::SendApproved(id) => log::info!("remote recipient approved outbound transfer {id}"),
JocalEvent::SendDenied => log::warn!("outbound transfer request has been denied"),
JocalEvent::SendSuccess { content, session: _session } => log::info!("successfully sent {content}"),
JocalEvent::SendFailed { error } => log::error!("could not send content: {error}"),
2025-08-14 00:48:55 +00:00
JocalEvent::Tick => {}
}
}
}
}
Ok(())
}
pub fn input(&mut self) -> &mut Input {
&mut self.input
}
pub fn files(&mut self) -> &mut FileExplorer {
&mut self.file_finder.explorer
}
pub fn text(&mut self) -> &mut Option<String> {
&mut self.text
}
2025-08-06 01:57:59 +00:00
pub fn screen(&self) -> CurrentScreen {
*self.screen.last().unwrap()
}
pub fn screen_mut(&mut self) -> &mut CurrentScreen {
self.screen.last_mut().unwrap()
}
2025-08-02 18:28:57 +00:00
pub fn draw(&mut self, frame: &mut Frame) {
2025-07-29 04:49:57 +00:00
frame.render_widget(self, frame.area());
}
}
impl FileFinder {
pub fn new() -> Result<Self> {
Ok(Self {
explorer: FileExplorer::new()?,
fuzzy: searcher(),
working_dir: None,
input: Default::default(),
})
}
pub fn handle(&mut self, event: &Event) -> Result<()> {
self.index();
Ok(self.explorer.handle(event)?)
}
pub fn cwd(&self) -> &Path {
self.explorer.cwd()
}
pub fn set_cwd(&mut self, cwd: &Path) -> Result<()> {
self.explorer.set_cwd(cwd)?;
self.index();
Ok(())
}
pub fn widget(&self) -> impl WidgetRef {
self.explorer.widget()
}
pub fn reset_fuzzy(&mut self) {
self.clear_fuzzy();
self.input.reset();
}
fn clear_fuzzy(&mut self) {
self.fuzzy = searcher();
}
fn index(&mut self) {
if let Some(owd) = self.working_dir.as_ref()
&& owd == self.cwd()
{
return;
}
self.working_dir = Some(self.cwd().to_path_buf());
self.clear_fuzzy();
for (i, f) in self.explorer.files().iter().enumerate() {
self.fuzzy.insert(i, f.name());
}
}
}
2025-07-30 19:41:18 +00:00
fn change_log_level(delta: isize) {
let level = log::max_level() as isize;
let max = log::LevelFilter::max() as isize;
let level = (level + delta).clamp(0, max) as usize;
// levelfilter is repr(usize) so this is safe
let level = unsafe { std::mem::transmute::<usize, LevelFilter>(level) };
log::set_max_level(level);
2025-07-30 05:04:20 +00:00
}