Clean up.

This commit is contained in:
Shaun Reed 2026-01-29 18:18:32 -05:00
parent b607a6daaa
commit c2c66ab7c0
6 changed files with 21 additions and 18 deletions

View File

@ -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."

View File

@ -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<CrosstermBackend<Stdout>>,

View File

@ -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)

View File

@ -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::<Vec<char>>())
.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)

View File

@ -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)

View File

@ -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<Action> {
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);
}
}