From c2c66ab7c04be5a7157ae7355abc7daccdc5c6da Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Thu, 29 Jan 2026 18:18:32 -0500 Subject: [PATCH] Clean up. --- src/main.rs | 2 +- src/tui.rs | 2 +- src/tui/app.rs | 6 +++--- src/tui/editor.rs | 9 +++++---- src/tui/editor_tab.rs | 10 ++++------ src/tui/explorer.rs | 10 +++++++--- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index c858e5e..917b90b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ impl Cli { self.tui.then(|| modes.push(RunMode::Tui)); self.gui.then(|| modes.push(RunMode::GuiAttached)); match &modes[..] { - [] => Ok(RunMode::Tui), + [] => Ok(RunMode::Gui), [mode] => Ok(*mode), multiple => Err(anyhow!( "More than one run mode found {multiple:?} please select one." diff --git a/src/tui.rs b/src/tui.rs index 0eb3235..d0569c5 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -7,6 +7,7 @@ mod explorer; mod logger; mod menu_bar; +use crate::AppContext; use anyhow::{Context, Result}; use log::{LevelFilter, debug, info, trace}; use ratatui::Terminal; @@ -22,7 +23,6 @@ use std::io::{Stdout, stdout}; use tui_logger::{ TuiLoggerFile, TuiLoggerLevelOutput, init_logger, set_default_level, set_log_file, }; -use crate::AppContext; struct Tui { terminal: Terminal>, diff --git a/src/tui/app.rs b/src/tui/app.rs index 9dfd72a..d925d2c 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -17,7 +17,7 @@ use ratatui::crossterm::event::{ use ratatui::layout::{Constraint, Direction, Layout, Rect}; use ratatui::prelude::{Color, Widget}; use ratatui::widgets::{Paragraph, Wrap}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::time::Duration; #[derive(Debug, Clone, Copy, PartialEq)] @@ -286,8 +286,8 @@ impl<'a> Component for App<'a> { }, Action::OpenTab => { if let Ok(path) = self.explorer.selected() { - let path_buf = PathBuf::from(path); - self.editor_tab.open_tab(&path_buf)?; + let path_buf = Path::new(&path); + self.editor_tab.open_tab(path_buf)?; Ok(Action::Handled) } else { Ok(Action::Noop) diff --git a/src/tui/editor.rs b/src/tui/editor.rs index c251ec0..a1f0081 100644 --- a/src/tui/editor.rs +++ b/src/tui/editor.rs @@ -9,6 +9,7 @@ use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use ratatui::layout::{Alignment, Rect}; use ratatui::prelude::{Color, Style}; use ratatui::widgets::{Block, Borders, Padding, Widget}; +use std::path::PathBuf; use syntect::parsing::SyntaxSet; pub struct Editor { @@ -22,7 +23,7 @@ pub struct Editor { impl Editor { pub const ID: &str = "Editor"; - pub fn new(path: &std::path::PathBuf) -> Self { + pub fn new(path: &std::path::Path) -> Self { trace!(target:Self::ID, "Building {}", Self::ID); Editor { state: EditorState::default(), @@ -47,14 +48,14 @@ impl Editor { } } - pub fn set_contents(&mut self, path: &std::path::PathBuf) -> Result<()> { + pub fn set_contents(&mut self, path: &std::path::Path) -> Result<()> { trace!(target:Self::ID, "Setting Editor contents from path {:?}", path); if let Ok(contents) = std::fs::read_to_string(path) { let lines: Vec<_> = contents .lines() .map(|line| line.chars().collect::>()) .collect(); - self.file_path = Some(path.clone()); + self.file_path = Some(PathBuf::from(path)); self.state.lines = Lines::new(lines); self.state.cursor.row = 0; self.state.cursor.col = 0; @@ -84,7 +85,7 @@ impl Widget for &mut Editor { .syntax_set .find_syntax_by_extension(lang) .map(|s| s.name.to_string()) - .unwrap_or("Unknown".to_string()); + .unwrap_or_else(|| String::from("Unknown")); EditorView::new(&mut self.state) .wrap(true) diff --git a/src/tui/editor_tab.rs b/src/tui/editor_tab.rs index f89f44c..76c2de4 100644 --- a/src/tui/editor_tab.rs +++ b/src/tui/editor_tab.rs @@ -103,7 +103,7 @@ impl EditorTab { } } - pub fn open_tab(&mut self, path: &std::path::PathBuf) -> Result<()> { + pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> { trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path); if self .editors @@ -130,9 +130,7 @@ impl EditorTab { let key = self .tab_order .get(index) - .ok_or(anyhow!( - "Failed to get tab order with invalid index {index}" - ))? + .ok_or_else(|| anyhow!("Failed to get tab order with invalid index {index}"))? .to_owned(); match self.editors.remove(&key) { None => { @@ -155,10 +153,10 @@ impl EditorTab { // TODO: Only file name is displayed in tab title, so files with the same name in different // directories will appear confusing. let tab_titles = self.tab_order.iter().map(|t| { - std::path::PathBuf::from(t) + std::path::Path::new(t) .file_name() .map(|f| f.to_string_lossy().to_string()) - .unwrap_or("Unknown".to_string()) + .unwrap_or_else(|| String::from("Unknown")) }); // Don't set border color based on ComponentState::focus, the Editor renders the border. Tabs::new(tab_titles) diff --git a/src/tui/explorer.rs b/src/tui/explorer.rs index f936626..a8b4c6c 100644 --- a/src/tui/explorer.rs +++ b/src/tui/explorer.rs @@ -7,8 +7,9 @@ use ratatui::layout::{Alignment, Position, Rect}; use ratatui::prelude::Style; use ratatui::style::{Color, Modifier}; use ratatui::widgets::{Block, Borders, StatefulWidget, Widget}; +use std::ffi::OsStr; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tui_tree_widget::{Tree, TreeItem, TreeState}; #[derive(Debug)] @@ -97,7 +98,10 @@ impl<'a> Explorer<'a> { impl<'a> Widget for &mut Explorer<'a> { fn render(self, area: Rect, buf: &mut Buffer) { if let Ok(tree) = Tree::new(&self.tree_items.children()) { - let file_name = self.root_path.file_name().unwrap_or("Unknown".as_ref()); + let file_name = self + .root_path + .file_name() + .unwrap_or_else(|| OsStr::new("Unknown")); StatefulWidget::render( tree.block( Block::default() @@ -143,7 +147,7 @@ impl<'a> Component for Explorer<'a> { fn handle_key_events(&mut self, key: KeyEvent) -> Result { if key.code == KeyCode::Enter { if let Ok(selected) = self.selected() { - if PathBuf::from(&selected).is_file() { + if Path::new(&selected).is_file() { return Ok(Action::OpenTab); } }