little bit of code shuffle

This commit is contained in:
Joe Ardent 2025-08-16 15:55:24 -07:00
parent 6feb6f8ab8
commit 3595f0bbc6
2 changed files with 131 additions and 83 deletions

View file

@ -1,4 +1,4 @@
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{Event, KeyCode, KeyEvent};
use jocalsend::ReceiveDialog; use jocalsend::ReceiveDialog;
use log::{debug, error, warn}; use log::{debug, error, warn};
use tui_input::backend::crossterm::EventHandler; use tui_input::backend::crossterm::EventHandler;
@ -12,14 +12,12 @@ impl App {
event: crossterm::event::Event, event: crossterm::event::Event,
) { ) {
let code = key_event.code; let code = key_event.code;
let mode = self.screen.last_mut().unwrap(); let mode = self.screen();
match mode { match mode {
CurrentScreen::Main CurrentScreen::Main
| CurrentScreen::Help | CurrentScreen::Help
| CurrentScreen::Logging | CurrentScreen::Logging
| CurrentScreen::Receiving | CurrentScreen::Receiving => match code {
| CurrentScreen::Sending(SendingScreen::Files(FileMode::Picking))
| CurrentScreen::Sending(SendingScreen::Peers) => match code {
KeyCode::Char('q') => self.exit().await, KeyCode::Char('q') => self.exit().await,
KeyCode::Char('s') => self.send(), KeyCode::Char('s') => self.send(),
KeyCode::Char('r') => self.recv(), KeyCode::Char('r') => self.recv(),
@ -42,62 +40,56 @@ impl App {
KeyCode::Char('d') => self.deny(), KeyCode::Char('d') => self.deny(),
_ => {} _ => {}
}, },
CurrentScreen::Sending(sending_screen) => match sending_screen { CurrentScreen::Stopping | CurrentScreen::Sending(_) => unreachable!(),
// we can only be in picking mode
SendingScreen::Files(fmode) => match code {
KeyCode::Char('t') => *sending_screen = SendingScreen::Text,
KeyCode::Tab => *sending_screen = SendingScreen::Peers,
KeyCode::Enter => self.chdir_or_send_file().await,
KeyCode::Char('/') => {
*fmode = FileMode::Fuzzy;
}
_ => self.file_finder.handle(&event).unwrap_or_default(),
}, },
SendingScreen::Peers => match code { },
KeyCode::Tab => { CurrentScreen::Sending(_) => self.sending_screen(key_event, event).await,
*sending_screen = SendingScreen::Files(FileMode::Picking) CurrentScreen::Stopping => {}
} }
KeyCode::Char('t') => *sending_screen = SendingScreen::Text, }
async fn sending_screen(&mut self, key_event: KeyEvent, event: Event) {
let mode = self.screen();
let CurrentScreen::Sending(mode) = mode else {
return;
};
let code = key_event.code;
match mode {
SendingScreen::Peers => match code {
KeyCode::Char('q') => self.exit().await,
KeyCode::Char('s') => self.send(),
KeyCode::Char('r') => self.recv(),
KeyCode::Char('l') => self.logs(),
KeyCode::Char('m') => self.main(),
KeyCode::Char('h') | KeyCode::Char('?') => self.help(),
KeyCode::Char('c') => self.service.clear_peers().await,
KeyCode::Char('t') => self.sending_text(),
KeyCode::Tab => self.sending_files(),
KeyCode::Enter => self.send_content().await, KeyCode::Enter => self.send_content().await,
KeyCode::Up => self.peer_state.select_previous(), KeyCode::Esc => self.pop(),
KeyCode::Down => self.peer_state.select_next(),
_ => {} _ => {}
}, },
SendingScreen::Text => unreachable!(), SendingScreen::Files(FileMode::Picking) => match code {
KeyCode::Char('q') => self.exit().await,
KeyCode::Char('s') => self.send(),
KeyCode::Char('r') => self.recv(),
KeyCode::Char('l') => self.logs(),
KeyCode::Char('m') => self.main(),
KeyCode::Char('h') | KeyCode::Char('?') => self.help(),
KeyCode::Char('c') => self.service.clear_peers().await,
KeyCode::Char('t') => self.sending_text(),
KeyCode::Tab => self.sending_peers(),
KeyCode::Enter => self.chdir_or_send_file().await,
KeyCode::Esc => self.pop(),
KeyCode::Char('/') => self.sending_fuzzy(),
_ => self.file_finder.handle(&event).unwrap_or_default(),
}, },
CurrentScreen::Stopping => unreachable!(), SendingScreen::Files(FileMode::Fuzzy) => match code {
}, KeyCode::Tab => self.sending_peers(),
},
// we only need to deal with sending text now or doing fuzzy matching
CurrentScreen::Sending(sending_screen) => match sending_screen {
SendingScreen::Text => match code {
KeyCode::Tab => *sending_screen = SendingScreen::Peers,
KeyCode::Enter => self.send_text().await,
KeyCode::Esc => {
self.text = None;
self.input.reset();
*sending_screen = SendingScreen::Files(FileMode::Picking);
}
_ => {
if let Some(changed) = self.input.handle_event(&event)
&& changed.value
{
if self.input.value().is_empty() {
self.text = None;
} else {
self.text = Some(self.input.to_string());
}
}
}
},
SendingScreen::Files(fmode) => {
if *fmode == FileMode::Fuzzy {
match code {
KeyCode::Tab => *sending_screen = SendingScreen::Peers,
KeyCode::Enter => self.chdir_or_send_file().await, KeyCode::Enter => self.chdir_or_send_file().await,
KeyCode::Esc => { KeyCode::Esc => {
self.file_finder.reset_fuzzy(); self.file_finder.reset_fuzzy();
*fmode = FileMode::Picking; self.sending_files();
} }
KeyCode::Up | KeyCode::Down => { KeyCode::Up | KeyCode::Down => {
if let Err(e) = self.file_finder.handle(&event) { if let Err(e) = self.file_finder.handle(&event) {
@ -119,12 +111,27 @@ impl App {
self.file_finder.explorer.set_selected_idx(id); self.file_finder.explorer.set_selected_idx(id);
} }
} }
}
}
}
SendingScreen::Peers => unreachable!(),
}, },
CurrentScreen::Stopping => {} SendingScreen::Text => match code {
KeyCode::Tab => self.sending_peers(),
KeyCode::Enter => self.send_text().await,
KeyCode::Esc => {
self.text = None;
self.input.reset();
self.sending_files();
}
_ => {
if let Some(changed) = self.input.handle_event(&event)
&& changed.value
{
if self.input.value().is_empty() {
self.text = None;
} else {
self.text = Some(self.input.to_string());
}
}
}
},
} }
} }
@ -184,6 +191,41 @@ impl App {
} }
} }
fn sending_peers(&mut self) {
if let CurrentScreen::Sending(mode) = self.screen_mut() {
*mode = SendingScreen::Peers;
}
}
fn sending_text(&mut self) {
if let CurrentScreen::Sending(mode) = self.screen_mut() {
*mode = SendingScreen::Text;
}
}
fn sending_fuzzy(&mut self) {
if let CurrentScreen::Sending(mode) = self.screen_mut() {
*mode = SendingScreen::Files(FileMode::Fuzzy);
}
}
fn sending_files(&mut self) {
let doing_files = self.text.is_none();
let doing_picking = self.file_finder.input.value().is_empty();
let screen = self.screen_mut();
if let CurrentScreen::Sending(mode) = screen {
if doing_files {
if doing_picking {
*mode = SendingScreen::Files(FileMode::Picking);
} else {
*mode = SendingScreen::Files(FileMode::Fuzzy);
}
} else {
*mode = SendingScreen::Text;
}
}
}
// accept a content receive request // accept a content receive request
fn accept(&mut self) { fn accept(&mut self) {
let Some(idx) = self.receiving_state.selected() else { let Some(idx) = self.receiving_state.selected() else {
@ -231,6 +273,7 @@ impl App {
error!("could not list directory {file:?}: {e}"); error!("could not list directory {file:?}: {e}");
return; return;
} else if file.is_dir() { } else if file.is_dir() {
self.file_finder.input.reset();
return; return;
} }

View file

@ -128,6 +128,9 @@ impl Widget for &mut App {
let [header_left, header_right] = header_layout.areas(top); let [header_left, header_right] = header_layout.areas(top);
let header_margin = Margin::new(1, 2); let header_margin = Margin::new(1, 2);
let top_heavy = Layout::vertical([Constraint::Percentage(66), Constraint::Percentage(34)]);
let [heavy_top, _skinny_bottom] = top_heavy.areas(area);
let subscreen_margin = Margin::new(1, 2); let subscreen_margin = Margin::new(1, 2);
// it's safe to call `unwrap()` here because we ensure there's always at least // it's safe to call `unwrap()` here because we ensure there's always at least
@ -190,7 +193,9 @@ impl Widget for &mut App {
let file_area = header_left.inner(header_margin); let file_area = header_left.inner(header_margin);
match sending_screen { match sending_screen {
SendingScreen::Files(FileMode::Picking) => { SendingScreen::Files(FileMode::Picking)
| SendingScreen::Peers
| SendingScreen::Text => {
self.file_finder.widget().render(file_area, buf); self.file_finder.widget().render(file_area, buf);
} }
SendingScreen::Files(FileMode::Fuzzy) => { SendingScreen::Files(FileMode::Fuzzy) => {
@ -199,7 +204,6 @@ impl Widget for &mut App {
text_popup(self.file_finder.input.value(), "fuzzy search", input, buf); text_popup(self.file_finder.input.value(), "fuzzy search", input, buf);
self.file_finder.widget().render(files, buf); self.file_finder.widget().render(files, buf);
} }
_ => {}
} }
logger(header_right.inner(header_margin), buf); logger(header_right.inner(header_margin), buf);
@ -212,7 +216,8 @@ impl Widget for &mut App {
); );
if sending_screen == SendingScreen::Text { if sending_screen == SendingScreen::Text {
let rect = centered_rect(area, Constraint::Percentage(80), Constraint::Max(10)); let rect =
centered_rect(heavy_top, Constraint::Percentage(80), Constraint::Max(6));
let text = if let Some(text) = self.text.as_ref() { let text = if let Some(text) = self.text.as_ref() {
text text
} else { } else {