From dde72977cbe6f7256d1bcae1245ba2f080a4b331 Mon Sep 17 00:00:00 2001 From: Scott-Nx <158021536+Scott-Nx@users.noreply.github.com> Date: Fri, 4 Sep 2026 13:39:23 +0700 Subject: [PATCH] build(release): adopt Rust 2024 Use let chains and apply updated formatting throughout. --- Cargo.toml | 2 +- src/graph.rs | 49 +++++++++++++++++++++++-------------------------- src/input.rs | 9 +++++---- src/main.rs | 15 +++++++-------- src/ui.rs | 23 +++++++++++++++++------ 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 05b2fae..4810bb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "kmutnb-phylab-plotter" version = "0.1.0" -edition = "2021" +edition = "2024" license = "Apache-2.0" description = "Terminal graph-paper plotter for physics lab data" diff --git a/src/graph.rs b/src/graph.rs index 1db10c6..a7fd42d 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -6,7 +6,7 @@ use ratatui::{ use crate::{ app::ScaleSettings, data::LabPoint, - stats::{linear_regression, Regression}, + stats::{Regression, linear_regression}, }; const BRAILLE_WIDTH: usize = 2; @@ -457,28 +457,28 @@ impl PlotRenderer { } graph.set(axis_col, axis_row, '+', Layer::Axis); - if self.options.show_fit { - if let Some(regression) = linear_regression(&self.points) { - for col in self.area.x..self.area.x + self.area.width { - let x = self.cell_to_x(col); - let y = regression.slope * x + regression.intercept; - if let Some(row) = self.y_to_cell(y) { - graph.set(col, row, '.', Layer::FitLine); - } + if self.options.show_fit + && let Some(regression) = linear_regression(&self.points) + { + for col in self.area.x..self.area.x + self.area.width { + let x = self.cell_to_x(col); + let y = regression.slope * x + regression.intercept; + if let Some(row) = self.y_to_cell(y) { + graph.set(col, row, '.', Layer::FitLine); } } } - if let Some((x, y)) = self.options.crosshair { - if let (Some(col), Some(row)) = (self.x_to_cell(x), self.y_to_cell(y)) { - for c in self.area.x..self.area.x + self.area.width { - graph.set(c, row, '-', Layer::Crosshair); - } - for r in self.area.y..self.area.y + self.area.height { - graph.set(col, r, '|', Layer::Crosshair); - } - graph.set(col, row, 'X', Layer::Crosshair); + if let Some((x, y)) = self.options.crosshair + && let (Some(col), Some(row)) = (self.x_to_cell(x), self.y_to_cell(y)) + { + for c in self.area.x..self.area.x + self.area.width { + graph.set(c, row, '-', Layer::Crosshair); } + for r in self.area.y..self.area.y + self.area.height { + graph.set(col, r, '|', Layer::Crosshair); + } + graph.set(col, row, 'X', Layer::Crosshair); } for point in &self.points { @@ -901,11 +901,7 @@ fn nice_step(raw: f64) -> f64 { fn clean_float(value: f64) -> f64 { let rounded = (value * 1e12).round() / 1e12; - if rounded.abs() < 1e-12 { - 0.0 - } else { - rounded - } + if rounded.abs() < 1e-12 { 0.0 } else { rounded } } fn major_values(min: f64, max: f64, step: f64) -> Vec { @@ -989,9 +985,10 @@ mod tests { ); let text = lines.join("\n"); - assert!(text - .chars() - .any(|ch| ('\u{2800}'..='\u{28ff}').contains(&ch))); + assert!( + text.chars() + .any(|ch| ('\u{2800}'..='\u{28ff}').contains(&ch)) + ); assert!(!text.contains("|||||")); assert!(!text.contains("!!!!!")); assert!(!text.contains(":::::")); diff --git a/src/input.rs b/src/input.rs index 60d6f7a..a1b07eb 100644 --- a/src/input.rs +++ b/src/input.rs @@ -172,10 +172,11 @@ fn handle_scale_input(app: &mut App, key: KeyEvent) -> bool { } } KeyCode::Char(ch) => { - if let Some(editor) = &mut app.scale_editor { - if !key.modifiers.contains(KeyModifiers::CONTROL) && is_scale_char(ch) { - editor.buffers[editor.selected].push(ch); - } + if let Some(editor) = &mut app.scale_editor + && !key.modifiers.contains(KeyModifiers::CONTROL) + && is_scale_char(ch) + { + editor.buffers[editor.selected].push(ch); } } _ => {} diff --git a/src/main.rs b/src/main.rs index 989bdc3..c48c9ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,10 @@ use anyhow::Result; use crossterm::{ event::{self, Event}, execute, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, + terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode}, }; use kmutnb_phylab_plotter::{app::App, file_io, input, ui}; -use ratatui::{backend::CrosstermBackend, Terminal}; +use ratatui::{Terminal, backend::CrosstermBackend}; fn main() -> Result<()> { let arg_path = env::args_os().nth(1).map(PathBuf::from); @@ -53,12 +53,11 @@ fn run(terminal: &mut Terminal>, app: &mut App) -> Resu loop { terminal.draw(|frame| ui::render(frame, app))?; - if event::poll(Duration::from_millis(200))? { - if let Event::Key(key) = event::read()? { - if !input::handle_key(app, key) { - break; - } - } + if event::poll(Duration::from_millis(200))? + && let Event::Key(key) = event::read()? + && !input::handle_key(app, key) + { + break; } } diff --git a/src/ui.rs b/src/ui.rs index 5b72abd..6701b84 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,9 +1,9 @@ use ratatui::{ + Frame, layout::{Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, text::{Line, Span}, widgets::{Block, Borders, Cell, Clear, Paragraph, Row, Table, Wrap}, - Frame, }; use crate::{ @@ -236,7 +236,9 @@ fn render_status(frame: &mut Frame, app: &App, area: Rect) { .map(|path| path.display().to_string()) .unwrap_or_else(|| "unsaved: lab_data.csv".to_string()); let help = match app.mode { - Mode::Normal => "?: help | i edit | a row | A col | d delete | X swap axes | s save | o open | S scale | G paper | q quit", + Mode::Normal => { + "?: help | i edit | a row | A col | d delete | X swap axes | s save | o open | S scale | G paper | q quit" + } Mode::Scale => "Enter apply | u auto scale | Esc cancel | j/k field", _ => "Enter confirm | Esc cancel", }; @@ -256,15 +258,24 @@ fn render_help(frame: &mut Frame, area: Rect) { let popup = centered_rect(area, 76, 24); frame.render_widget(Clear, popup); let text = vec![ - Line::styled("Physics Lab Plotter Help", Style::default().add_modifier(Modifier::BOLD)), + Line::styled( + "Physics Lab Plotter Help", + Style::default().add_modifier(Modifier::BOLD), + ), Line::from(""), - Line::from("Navigation: h/j/k/l move table selection or graph crosshair, t focus table, g focus graph"), + Line::from( + "Navigation: h/j/k/l move table selection or graph crosshair, t focus table, g focus graph", + ), Line::from("Data: i edit cell, a add row, A add column, d delete row, r rename column"), Line::from("Files: s save CSV, o open CSV, q quit"), - Line::from("Graph: f toggle best-fit line, X swap axes, G graph paper mode, c toggle crosshair"), + Line::from( + "Graph: f toggle best-fit line, X swap axes, G graph paper mode, c toggle crosshair", + ), Line::from("Scale: S set manual scale, u return to auto scale while in scale dialog"), Line::from(""), - Line::from("Invalid or blank x/y values are shown in red in the table and ignored by fit/plot."), + Line::from( + "Invalid or blank x/y values are shown in red in the table and ignored by fit/plot.", + ), Line::from( "Manual scale fields: x min, x max, y min, y max, x major, y major, minor divisions.", ),