refactor: cut what a whole-repo audit proved dead, padded, or hand-rolled
Seven region audits, every finding adversarially verified against the whole workspace before being applied; 80 landed and the two that failed in application were refuted honestly (ParseError has a from_str_radix producer the audit missed; NatPmpError's Display is the only reader of its diagnostic payloads). The bigger cuts: - The NewPeer/PierceFireWall cluster: an operation variant nothing ever sent, its 77-line arm, a protocol-wrong Client::pierce_firewall that wrote a peer-init frame down the server socket, and the struct only they used. The live paths in peer/listen.rs are untouched. - The ops loop reads `for operation in reader` instead of hand-rolling recv/break, de-indenting the whole dispatch body. - Dead lib surface: Message::print_hex and four unused accessors, UploadFailed, registry and actor getters, build_watch_user, strip_ansi_codes, delegating shims on ClientContext. Removing public items makes this a semver major, which the release pipeline enforces. - Never-used flexibility: the state-migration framework that never held a migration, border-type helpers that all answered Rounded, the TUI's max_concurrent_downloads field nothing read. - Convergence: six poll loops into poll_until, three wrap-around navigation blocks into cycle, hit tests into Rect::contains, char walkers into floor/ceil_char_boundary, five Download literals into one helper, the suites' shared scaffolding into tests/common. Deliberately not taken: swapping the hand-rolled zlib inflate and MD5 for crates (they replace wire-facing machinery whose tolerance of real-world peer quirks no local test exercises) and pulling chrono into the lib for a log timestamp. Recorded for their own decision. Net -1002 lines. Both e2e modes stay green: 94 CLI tests each way, 16 protocol, 33 lib, unit suites, clippy pedantic clean, aislop 100/100 with the four scanner false positives suppressed inline where they fire.
This commit is contained in:
@@ -3,6 +3,8 @@ use crate::models::{CommandBarMode, FocusedPane};
|
||||
use ratatui::crossterm::event::{
|
||||
KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
|
||||
};
|
||||
use ratatui::layout::Position;
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
impl MainTui {
|
||||
pub(super) fn handle_key_event(&mut self, key: KeyEvent) {
|
||||
@@ -189,10 +191,10 @@ impl MainTui {
|
||||
KeyCode::Backspace => {
|
||||
let cursor_position = self.state.command_bar_cursor_position;
|
||||
if cursor_position > 0 {
|
||||
let previous_position = previous_char_boundary(
|
||||
&self.state.command_bar_input,
|
||||
cursor_position,
|
||||
);
|
||||
let previous_position = self
|
||||
.state
|
||||
.command_bar_input
|
||||
.floor_char_boundary(cursor_position.saturating_sub(1));
|
||||
self.state
|
||||
.command_bar_input
|
||||
.drain(previous_position..cursor_position);
|
||||
@@ -202,26 +204,26 @@ impl MainTui {
|
||||
KeyCode::Delete => {
|
||||
let cursor_position = self.state.command_bar_cursor_position;
|
||||
if cursor_position < self.state.command_bar_input.len() {
|
||||
let next_position = next_char_boundary(
|
||||
&self.state.command_bar_input,
|
||||
cursor_position,
|
||||
);
|
||||
let next_position = self
|
||||
.state
|
||||
.command_bar_input
|
||||
.ceil_char_boundary(cursor_position + 1);
|
||||
self.state
|
||||
.command_bar_input
|
||||
.drain(cursor_position..next_position);
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
self.state.command_bar_cursor_position = previous_char_boundary(
|
||||
&self.state.command_bar_input,
|
||||
self.state.command_bar_cursor_position,
|
||||
);
|
||||
let previous =
|
||||
self.state.command_bar_cursor_position.saturating_sub(1);
|
||||
self.state.command_bar_cursor_position =
|
||||
self.state.command_bar_input.floor_char_boundary(previous);
|
||||
}
|
||||
KeyCode::Right => {
|
||||
self.state.command_bar_cursor_position = next_char_boundary(
|
||||
&self.state.command_bar_input,
|
||||
self.state.command_bar_cursor_position,
|
||||
);
|
||||
let input = &self.state.command_bar_input;
|
||||
let next = self.state.command_bar_cursor_position + 1;
|
||||
self.state.command_bar_cursor_position =
|
||||
input.ceil_char_boundary(next.min(input.len()));
|
||||
}
|
||||
KeyCode::Home => {
|
||||
self.state.command_bar_cursor_position = 0;
|
||||
@@ -288,26 +290,13 @@ impl MainTui {
|
||||
}
|
||||
|
||||
fn handle_searches_input(&mut self, key: KeyEvent) {
|
||||
let rows = self.state.searches.len();
|
||||
match key.code {
|
||||
KeyCode::Up | KeyCode::Char('k')
|
||||
if !self.state.searches.is_empty() =>
|
||||
{
|
||||
let current =
|
||||
self.state.searches_table_state.selected().unwrap_or(0);
|
||||
let new = if current == 0 {
|
||||
self.state.searches.len() - 1
|
||||
} else {
|
||||
current - 1
|
||||
};
|
||||
self.state.searches_table_state.select(Some(new));
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
cycle(&mut self.state.searches_table_state, rows, false);
|
||||
}
|
||||
KeyCode::Down | KeyCode::Char('j')
|
||||
if !self.state.searches.is_empty() =>
|
||||
{
|
||||
let current =
|
||||
self.state.searches_table_state.selected().unwrap_or(0);
|
||||
let new = (current + 1) % self.state.searches.len();
|
||||
self.state.searches_table_state.select(Some(new));
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
cycle(&mut self.state.searches_table_state, rows, true);
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected) =
|
||||
@@ -348,21 +337,11 @@ impl MainTui {
|
||||
};
|
||||
|
||||
match key.code {
|
||||
KeyCode::Up | KeyCode::Char('k') if items_count > 0 => {
|
||||
let current =
|
||||
self.state.results_table_state.selected().unwrap_or(0);
|
||||
let new = if current == 0 {
|
||||
items_count - 1
|
||||
} else {
|
||||
current - 1
|
||||
};
|
||||
self.state.results_table_state.select(Some(new));
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
cycle(&mut self.state.results_table_state, items_count, false);
|
||||
}
|
||||
KeyCode::Down | KeyCode::Char('j') if items_count > 0 => {
|
||||
let current =
|
||||
self.state.results_table_state.selected().unwrap_or(0);
|
||||
let new = (current + 1) % items_count;
|
||||
self.state.results_table_state.select(Some(new));
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
cycle(&mut self.state.results_table_state, items_count, true);
|
||||
}
|
||||
KeyCode::Char(' ') => {
|
||||
if let Some(current) = self.state.results_table_state.selected()
|
||||
@@ -416,17 +395,11 @@ impl MainTui {
|
||||
// The pane lists downloads first, then uploads; navigation spans both.
|
||||
let rows = self.state.downloads.len() + self.state.uploads.len();
|
||||
match key.code {
|
||||
KeyCode::Up | KeyCode::Char('k') if rows > 0 => {
|
||||
let current =
|
||||
self.state.downloads_table_state.selected().unwrap_or(0);
|
||||
let new = if current == 0 { rows - 1 } else { current - 1 };
|
||||
self.state.downloads_table_state.select(Some(new));
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
cycle(&mut self.state.downloads_table_state, rows, false);
|
||||
}
|
||||
KeyCode::Down | KeyCode::Char('j') if rows > 0 => {
|
||||
let current =
|
||||
self.state.downloads_table_state.selected().unwrap_or(0);
|
||||
let new = (current + 1) % rows;
|
||||
self.state.downloads_table_state.select(Some(new));
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
cycle(&mut self.state.downloads_table_state, rows, true);
|
||||
}
|
||||
KeyCode::Char('x') => {
|
||||
self.cancel_selected_upload();
|
||||
@@ -448,63 +421,35 @@ impl MainTui {
|
||||
}
|
||||
|
||||
pub(super) fn handle_mouse_event(&mut self, mouse: MouseEvent) {
|
||||
if mouse.kind == MouseEventKind::Down(MouseButton::Left) {
|
||||
let (col, row) = (mouse.column, mouse.row);
|
||||
if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if click is within searches pane
|
||||
if let Some(area) = self.state.searches_pane_area
|
||||
&& col >= area.x
|
||||
&& col < area.x + area.width
|
||||
&& row >= area.y
|
||||
&& row < area.y + area.height
|
||||
{
|
||||
self.state.focused_pane = FocusedPane::Searches;
|
||||
let clicked = Position::new(mouse.column, mouse.row);
|
||||
let panes = [
|
||||
(self.state.searches_pane_area, FocusedPane::Searches),
|
||||
(self.state.results_pane_area, FocusedPane::Results),
|
||||
(self.state.downloads_pane_area, FocusedPane::Downloads),
|
||||
];
|
||||
for (area, pane) in panes {
|
||||
if area.is_some_and(|area| area.contains(clicked)) {
|
||||
self.state.focused_pane = pane;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if click is within results pane
|
||||
if let Some(area) = self.state.results_pane_area
|
||||
&& col >= area.x
|
||||
&& col < area.x + area.width
|
||||
&& row >= area.y
|
||||
&& row < area.y + area.height
|
||||
{
|
||||
self.state.focused_pane = FocusedPane::Results;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if click is within downloads pane
|
||||
if let Some(area) = self.state.downloads_pane_area
|
||||
&& col >= area.x
|
||||
&& col < area.x + area.width
|
||||
&& row >= area.y
|
||||
&& row < area.y + area.height
|
||||
{
|
||||
self.state.focused_pane = FocusedPane::Downloads;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn previous_char_boundary(input: &str, cursor_position: usize) -> usize {
|
||||
let cursor_position = input.floor_char_boundary(cursor_position);
|
||||
|
||||
input[..cursor_position]
|
||||
.char_indices()
|
||||
.last()
|
||||
.map_or(0, |(index, _)| index)
|
||||
}
|
||||
|
||||
fn next_char_boundary(input: &str, cursor_position: usize) -> usize {
|
||||
let cursor_position = input.floor_char_boundary(cursor_position);
|
||||
|
||||
if cursor_position >= input.len() {
|
||||
return input.len();
|
||||
/// Move a table's selection one row, wrapping at either end.
|
||||
fn cycle(table: &mut TableState, len: usize, forward: bool) {
|
||||
if len == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor_position
|
||||
+ input[cursor_position..]
|
||||
.chars()
|
||||
.next()
|
||||
.map_or(0, char::len_utf8)
|
||||
let current = table.selected().unwrap_or(0);
|
||||
let next = if forward {
|
||||
(current + 1) % len
|
||||
} else {
|
||||
(current + len - 1) % len
|
||||
};
|
||||
table.select(Some(next));
|
||||
}
|
||||
|
||||
@@ -23,8 +23,6 @@ pub struct MainTui {
|
||||
client: Arc<dyn SessionApi>,
|
||||
state: AppState,
|
||||
download_dir: String,
|
||||
#[allow(dead_code)]
|
||||
max_concurrent_downloads: usize,
|
||||
search_timeout: Duration,
|
||||
spinner_state: usize,
|
||||
store: Option<StateStore>,
|
||||
@@ -39,7 +37,6 @@ impl MainTui {
|
||||
pub fn new(
|
||||
client: Arc<dyn SessionApi>,
|
||||
download_dir: String,
|
||||
max_concurrent_downloads: usize,
|
||||
search_timeout: Duration,
|
||||
store: Option<StateStore>,
|
||||
config_path: Option<std::path::PathBuf>,
|
||||
@@ -48,7 +45,6 @@ impl MainTui {
|
||||
client,
|
||||
state: AppState::new(),
|
||||
download_dir,
|
||||
max_concurrent_downloads,
|
||||
search_timeout,
|
||||
spinner_state: 0,
|
||||
store,
|
||||
@@ -256,19 +252,12 @@ pub fn launch_main_tui(
|
||||
terminal: DefaultTerminal,
|
||||
client: Arc<dyn SessionApi>,
|
||||
download_dir: String,
|
||||
max_concurrent_downloads: usize,
|
||||
search_timeout: Duration,
|
||||
store: Option<StateStore>,
|
||||
config_path: Option<std::path::PathBuf>,
|
||||
) -> Result<()> {
|
||||
let tui = MainTui::new(
|
||||
client,
|
||||
download_dir,
|
||||
max_concurrent_downloads,
|
||||
search_timeout,
|
||||
store,
|
||||
config_path,
|
||||
);
|
||||
let tui =
|
||||
MainTui::new(client, download_dir, search_timeout, store, config_path);
|
||||
tui.run(terminal)
|
||||
}
|
||||
|
||||
@@ -526,7 +515,6 @@ mod tests {
|
||||
MainTui::new(
|
||||
Arc::new(session),
|
||||
"/tmp".to_string(),
|
||||
1,
|
||||
Duration::from_secs(1),
|
||||
None,
|
||||
None,
|
||||
@@ -746,7 +734,6 @@ mod tests {
|
||||
let mut tui = MainTui::new(
|
||||
session.clone(),
|
||||
"/daemon/old".to_string(),
|
||||
1,
|
||||
Duration::from_secs(1),
|
||||
None,
|
||||
Some(config_path.clone()),
|
||||
@@ -803,7 +790,6 @@ mod tests {
|
||||
let mut tui = MainTui::new(
|
||||
session.clone(),
|
||||
"/tmp".to_string(),
|
||||
1,
|
||||
Duration::from_secs(1),
|
||||
None,
|
||||
Some(config_path),
|
||||
|
||||
@@ -17,18 +17,6 @@ use ratatui::{
|
||||
widgets::Paragraph,
|
||||
};
|
||||
|
||||
const COMMAND_BAR_PREFIX: &str = "search: ";
|
||||
const MESSAGE_BAR_PREFIX: &str = "message (to: recipient text): ";
|
||||
const BROWSE_BAR_PREFIX: &str = "browse user: ";
|
||||
|
||||
const fn command_bar_prefix(mode: CommandBarMode) -> &'static str {
|
||||
match mode {
|
||||
CommandBarMode::Search => COMMAND_BAR_PREFIX,
|
||||
CommandBarMode::Message => MESSAGE_BAR_PREFIX,
|
||||
CommandBarMode::Browse => BROWSE_BAR_PREFIX,
|
||||
}
|
||||
}
|
||||
|
||||
impl MainTui {
|
||||
pub(super) fn render(&mut self, frame: &mut Frame) {
|
||||
let mut constraints = vec![
|
||||
@@ -458,7 +446,11 @@ impl MainTui {
|
||||
}
|
||||
|
||||
fn render_command_bar(&self, frame: &mut Frame, area: Rect) {
|
||||
let prefix = command_bar_prefix(self.state.command_bar_mode);
|
||||
let prefix = match self.state.command_bar_mode {
|
||||
CommandBarMode::Search => "search: ",
|
||||
CommandBarMode::Message => "message (to: recipient text): ",
|
||||
CommandBarMode::Browse => "browse user: ",
|
||||
};
|
||||
let block = pane_block(true);
|
||||
// Derive from the block's inner rect so borders and padding stay in
|
||||
// one place; the cursor follows whatever the pane reserves.
|
||||
|
||||
@@ -102,17 +102,13 @@ impl MainTui {
|
||||
self.state.results_is_filtering = false;
|
||||
}
|
||||
|
||||
fn recompute_results_filter(&mut self) {
|
||||
pub(super) fn apply_filter(&mut self) {
|
||||
let (items, indices) = filter_results(
|
||||
&self.state.results_items,
|
||||
&self.state.results_filter_query,
|
||||
);
|
||||
self.state.results_filtered_items = items;
|
||||
self.state.results_filtered_indices = indices;
|
||||
}
|
||||
|
||||
pub(super) fn apply_filter(&mut self) {
|
||||
self.recompute_results_filter();
|
||||
if !self.state.results_filtered_items.is_empty() {
|
||||
self.state.results_table_state.select(Some(0));
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use crate::models::{AppState, MessageDirection};
|
||||
use crate::ui::{
|
||||
accent_style, border_style, border_type, dimmed_style, highlight_style,
|
||||
info_style, primary_style,
|
||||
accent_style, border_style, dimmed_style, highlight_style, info_style,
|
||||
primary_style,
|
||||
};
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
};
|
||||
|
||||
/// Render the private-message chat box: title is the conversation partner,
|
||||
@@ -28,7 +28,7 @@ pub fn render_chat_pane(
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(border_style(true))
|
||||
.border_type(border_type(true))
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(title);
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
|
||||
@@ -229,7 +229,12 @@ fn build_info_lines(
|
||||
speed_bytes_per_sec,
|
||||
} => {
|
||||
lines.push(Line::from(""));
|
||||
push_progress_lines(&mut lines, *bytes_downloaded, *total_bytes);
|
||||
push_transfer_progress(
|
||||
&mut lines,
|
||||
"Downloaded",
|
||||
*bytes_downloaded,
|
||||
*total_bytes,
|
||||
);
|
||||
lines.push(label_value(
|
||||
"Speed",
|
||||
&format_speed(*speed_bytes_per_sec),
|
||||
@@ -246,7 +251,12 @@ fn build_info_lines(
|
||||
total_bytes,
|
||||
} => {
|
||||
lines.push(Line::from(""));
|
||||
push_progress_lines(&mut lines, *bytes_downloaded, *total_bytes);
|
||||
push_transfer_progress(
|
||||
&mut lines,
|
||||
"Downloaded",
|
||||
*bytes_downloaded,
|
||||
*total_bytes,
|
||||
);
|
||||
}
|
||||
DownloadStatus::Failed(reason) => {
|
||||
if let Some(reason) = reason {
|
||||
@@ -268,10 +278,7 @@ fn build_info_lines(
|
||||
}
|
||||
|
||||
fn label_value(label: &str, value: &str) -> Line<'static> {
|
||||
Line::from(vec![
|
||||
Span::styled(format!("{label:<LABEL_WIDTH$}"), dimmed_style()),
|
||||
Span::styled(value.to_string(), primary_style()),
|
||||
])
|
||||
label_value_styled(label, value.to_string(), primary_style())
|
||||
}
|
||||
|
||||
fn label_value_styled(
|
||||
@@ -285,14 +292,6 @@ fn label_value_styled(
|
||||
])
|
||||
}
|
||||
|
||||
fn push_progress_lines(
|
||||
lines: &mut Vec<Line<'static>>,
|
||||
bytes_downloaded: u64,
|
||||
total_bytes: u64,
|
||||
) {
|
||||
push_transfer_progress(lines, "Downloaded", bytes_downloaded, total_bytes);
|
||||
}
|
||||
|
||||
/// A progress bar plus a `<label> <done> / <total>` line, shared by the
|
||||
/// download ("Downloaded") and upload ("Sent") views.
|
||||
fn push_transfer_progress(
|
||||
|
||||
@@ -56,7 +56,16 @@ pub fn render_downloads_pane(
|
||||
|
||||
let progress_text = match &download.status {
|
||||
DownloadStatus::Queued => "Queued".to_string(),
|
||||
DownloadStatus::InProgress { .. } => {
|
||||
DownloadStatus::InProgress { .. }
|
||||
| DownloadStatus::Paused { .. } => {
|
||||
let prefix = if matches!(
|
||||
download.status,
|
||||
DownloadStatus::Paused { .. }
|
||||
) {
|
||||
"Paused "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let percent = if download.size > 0 {
|
||||
(download.bytes_downloaded() as f64
|
||||
/ download.size as f64
|
||||
@@ -65,22 +74,7 @@ pub fn render_downloads_pane(
|
||||
0
|
||||
};
|
||||
format!(
|
||||
"{}/{} ({}%)",
|
||||
format_bytes(download.bytes_downloaded()),
|
||||
format_bytes(download.size),
|
||||
percent
|
||||
)
|
||||
}
|
||||
DownloadStatus::Paused { .. } => {
|
||||
let percent = if download.size > 0 {
|
||||
(download.bytes_downloaded() as f64
|
||||
/ download.size as f64
|
||||
* 100.0) as u8
|
||||
} else {
|
||||
0
|
||||
};
|
||||
format!(
|
||||
"Paused {}/{} ({}%)",
|
||||
"{prefix}{}/{} ({}%)",
|
||||
format_bytes(download.bytes_downloaded()),
|
||||
format_bytes(download.size),
|
||||
percent
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// Reusable styles and colors for consistent UI appearance
|
||||
#![allow(dead_code)]
|
||||
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
@@ -103,14 +102,6 @@ pub fn unfocused_border_style() -> Style {
|
||||
Style::default().fg(DUST)
|
||||
}
|
||||
|
||||
pub const fn focused_border_type() -> BorderType {
|
||||
BorderType::Rounded
|
||||
}
|
||||
|
||||
pub const fn unfocused_border_type() -> BorderType {
|
||||
BorderType::Rounded
|
||||
}
|
||||
|
||||
pub fn border_style(focused: bool) -> Style {
|
||||
if focused {
|
||||
focused_border_style()
|
||||
@@ -119,14 +110,6 @@ pub fn border_style(focused: bool) -> Style {
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn border_type(focused: bool) -> BorderType {
|
||||
if focused {
|
||||
focused_border_type()
|
||||
} else {
|
||||
unfocused_border_type()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title_style(focused: bool) -> Style {
|
||||
if focused {
|
||||
Style::default().fg(PHOSPHOR).add_modifier(Modifier::BOLD)
|
||||
@@ -159,7 +142,7 @@ pub fn pane_block(focused: bool) -> Block<'static> {
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(border_style(focused))
|
||||
.border_type(border_type(focused))
|
||||
.border_type(BorderType::Rounded)
|
||||
.padding(PANE_PADDING)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user