From 12ba829d49ed6dd3ac47989c5806c36ced7610b6 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:37:23 -0700 Subject: [PATCH 1/3] refactor: Handle temporary directories entirely within core --- Cargo.lock | 1 - core/src/inner.rs | 31 +++++++++++++++++++++++++------ core/src/lib.rs | 2 +- tui/Cargo.toml | 1 - tui/src/state.rs | 10 +++------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81750251..7c2d2148 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,7 +510,6 @@ dependencies = [ "portable-pty", "rand", "ratatui", - "temp-dir", "tree-sitter-bash", "tree-sitter-highlight", "tui-term", diff --git a/core/src/inner.rs b/core/src/inner.rs index a2274dfa..80a11459 100644 --- a/core/src/inner.rs +++ b/core/src/inner.rs @@ -1,6 +1,7 @@ use std::{ fs::File, io::{BufRead, BufReader, Read}, + ops::{Deref, DerefMut}, os::unix::fs::PermissionsExt, path::{Path, PathBuf}, rc::Rc, @@ -14,8 +15,26 @@ use temp_dir::TempDir; const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs"); -pub fn get_tabs(validate: bool) -> (TempDir, Vec) { - let (temp_dir, tab_files) = TabList::get_tabs(); +// Allow the unused TempDir to be stored for later destructor call +#[allow(dead_code)] +pub struct TabList(pub Vec, TempDir); + +// Implement deref to allow Vec methods to be called on TabList +impl Deref for TabList { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl DerefMut for TabList { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +pub fn get_tabs(validate: bool) -> TabList { + let (temp_dir, tab_files) = TabDirectories::get_tabs(); let tabs: Vec<_> = tab_files .into_iter() @@ -62,11 +81,11 @@ pub fn get_tabs(validate: bool) -> (TempDir, Vec) { if tabs.is_empty() { panic!("No tabs found"); } - (temp_dir, tabs) + TabList(tabs, temp_dir) } #[derive(Deserialize)] -struct TabList { +struct TabDirectories { directories: Vec, } @@ -252,9 +271,9 @@ fn is_executable(path: &Path) -> bool { .unwrap_or(false) } -impl TabList { +impl TabDirectories { fn get_tabs() -> (TempDir, Vec) { - let temp_dir = TempDir::new().unwrap(); + let temp_dir = TempDir::with_prefix("linutil_scripts").unwrap(); TAB_DATA .extract(&temp_dir) .expect("Failed to extract the saved directory"); diff --git a/core/src/lib.rs b/core/src/lib.rs index b7cd631e..0ebd7a17 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -5,7 +5,7 @@ use std::rc::Rc; use ego_tree::Tree; use std::path::PathBuf; -pub use inner::get_tabs; +pub use inner::{get_tabs, TabList}; #[derive(Clone, Hash, Eq, PartialEq)] pub enum Command { diff --git a/tui/Cargo.toml b/tui/Cargo.toml index 662b7a17..6d77e3be 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -22,7 +22,6 @@ oneshot = "0.1.8" portable-pty = "0.8.1" ratatui = "0.28.1" tui-term = "0.1.12" -temp-dir = "0.1.14" unicode-width = "0.2.0" rand = { version = "0.8.5", optional = true } linutil_core = { path = "../core", version = "24.9.28" } diff --git a/tui/src/state.rs b/tui/src/state.rs index 36865557..8da5c468 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -11,7 +11,7 @@ use crate::{ }; use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; use ego_tree::NodeId; -use linutil_core::{ListNode, Tab}; +use linutil_core::{ListNode, TabList}; #[cfg(feature = "tips")] use rand::Rng; use ratatui::{ @@ -21,7 +21,6 @@ use ratatui::{ widgets::{Block, Borders, List, ListState, Paragraph}, Frame, }; -use temp_dir::TempDir; const MIN_WIDTH: u16 = 77; const MIN_HEIGHT: u16 = 19; @@ -41,14 +40,12 @@ P* - privileged * "; pub struct AppState { - /// This must be passed to retain the temp dir until the end of the program - _temp_dir: TempDir, /// Selected theme theme: Theme, /// Currently focused area pub focus: Focus, /// List of tabs - tabs: Vec, + tabs: TabList, /// Current tab current_tab: ListState, /// This stack keeps track of our "current directory". You can think of it as `pwd`. but not @@ -81,11 +78,10 @@ pub struct ListEntry { impl AppState { pub fn new(theme: Theme, override_validation: bool) -> Self { - let (temp_dir, tabs) = linutil_core::get_tabs(!override_validation); + let tabs = linutil_core::get_tabs(!override_validation); let root_id = tabs[0].tree.root().id(); let mut state = Self { - _temp_dir: temp_dir, theme, focus: Focus::List, tabs, From d392c985f2f6ffbfa053f1d4e8237c4c7b9b8444 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Fri, 25 Oct 2024 07:29:42 -0700 Subject: [PATCH 2/3] fix (xtask): Handle modified tablist struct --- core/src/inner.rs | 8 ++++++++ xtask/src/docgen.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/core/src/inner.rs b/core/src/inner.rs index 80a11459..d98e7efe 100644 --- a/core/src/inner.rs +++ b/core/src/inner.rs @@ -32,6 +32,14 @@ impl DerefMut for TabList { &mut self.0 } } +impl IntoIterator for TabList { + type Item = Tab; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} pub fn get_tabs(validate: bool) -> TabList { let (temp_dir, tab_files) = TabDirectories::get_tabs(); diff --git a/xtask/src/docgen.rs b/xtask/src/docgen.rs index 992b83aa..fe930be2 100644 --- a/xtask/src/docgen.rs +++ b/xtask/src/docgen.rs @@ -11,7 +11,7 @@ pub fn userguide() -> Result { let mut md = String::new(); md.push_str("\n# Walkthrough\n"); - let tabs = linutil_core::get_tabs(false).1; + let tabs = linutil_core::get_tabs(false); for tab in tabs { #[cfg(debug_assertions)] From 142cfa68dffc94ca7c264a31c869c4a69b25232a Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Fri, 25 Oct 2024 07:31:44 -0700 Subject: [PATCH 3/3] refactor (xtask): Apply Clippy suggested changes --- xtask/src/docgen.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/xtask/src/docgen.rs b/xtask/src/docgen.rs index fe930be2..385c1134 100644 --- a/xtask/src/docgen.rs +++ b/xtask/src/docgen.rs @@ -24,7 +24,7 @@ pub fn userguide() -> Result { #[cfg(debug_assertions)] println!(" Directory: {}", entry.name); - if entry.name != "root".to_string() { + if entry.name != "root" { md.push_str(&format!("\n### {}\n\n", entry.name)); } @@ -36,18 +36,16 @@ pub fn userguide() -> Result { current_dir )); } */ // Commenting this for now, might be a good idea later - } else { - if !entry.description.is_empty() { - #[cfg(debug_assertions)] - println!(" Entry: {}", entry.name); - #[cfg(debug_assertions)] - println!(" Description: {}", entry.description); + } else if !entry.description.is_empty() { + #[cfg(debug_assertions)] + println!(" Entry: {}", entry.name); + #[cfg(debug_assertions)] + println!(" Description: {}", entry.description); - md.push_str(&format!("- **{}**: {}\n", entry.name, entry.description)); - } /* else { - md.push_str(&format!("- **{}**\n", entry.name)); - } */ // https://github.com/ChrisTitusTech/linutil/pull/753 - } + md.push_str(&format!("- **{}**: {}\n", entry.name, entry.description)); + } /* else { + md.push_str(&format!("- **{}**\n", entry.name)); + } */ // https://github.com/ChrisTitusTech/linutil/pull/753 } }