mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-12-25 12:59:45 +00:00
refact: Handle temporary directories entirely within core (#754)
* refactor: Handle temporary directories entirely within core * fix (xtask): Handle modified tablist struct * refactor (xtask): Apply Clippy suggested changes * Fix size_bypass --------- Co-authored-by: Chris Titus <contact@christitus.com>
This commit is contained in:
parent
c36879e22f
commit
6728e7ee9b
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufRead, BufReader, Read},
|
io::{BufRead, BufReader, Read},
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
os::unix::fs::PermissionsExt,
|
os::unix::fs::PermissionsExt,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
@ -14,8 +15,34 @@ use temp_dir::TempDir;
|
||||||
|
|
||||||
const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs");
|
const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs");
|
||||||
|
|
||||||
pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
|
// Allow the unused TempDir to be stored for later destructor call
|
||||||
let (temp_dir, tab_files) = TabList::get_tabs();
|
#[allow(dead_code)]
|
||||||
|
pub struct TabList(pub Vec<Tab>, TempDir);
|
||||||
|
|
||||||
|
// Implement deref to allow Vec<Tab> methods to be called on TabList
|
||||||
|
impl Deref for TabList {
|
||||||
|
type Target = Vec<Tab>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl DerefMut for TabList {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl IntoIterator for TabList {
|
||||||
|
type Item = Tab;
|
||||||
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
let tabs: Vec<_> = tab_files
|
let tabs: Vec<_> = tab_files
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -50,11 +77,11 @@ pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
|
||||||
if tabs.is_empty() {
|
if tabs.is_empty() {
|
||||||
panic!("No tabs found");
|
panic!("No tabs found");
|
||||||
}
|
}
|
||||||
(temp_dir, tabs)
|
TabList(tabs, temp_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TabList {
|
struct TabDirectories {
|
||||||
directories: Vec<PathBuf>,
|
directories: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,9 +273,9 @@ fn is_executable(path: &Path) -> bool {
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TabList {
|
impl TabDirectories {
|
||||||
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
|
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
|
||||||
let temp_dir = TempDir::new().unwrap();
|
let temp_dir = TempDir::with_prefix("linutil_scripts").unwrap();
|
||||||
TAB_DATA
|
TAB_DATA
|
||||||
.extract(&temp_dir)
|
.extract(&temp_dir)
|
||||||
.expect("Failed to extract the saved directory");
|
.expect("Failed to extract the saved directory");
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::rc::Rc;
|
||||||
use ego_tree::Tree;
|
use ego_tree::Tree;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub use inner::get_tabs;
|
pub use inner::{get_tabs, TabList};
|
||||||
|
|
||||||
#[derive(Clone, Hash, Eq, PartialEq)]
|
#[derive(Clone, Hash, Eq, PartialEq)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
use ego_tree::NodeId;
|
use ego_tree::NodeId;
|
||||||
use linutil_core::{ListNode, Tab};
|
use linutil_core::{ListNode, TabList};
|
||||||
#[cfg(feature = "tips")]
|
#[cfg(feature = "tips")]
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
|
@ -20,7 +20,6 @@ use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use temp_dir::TempDir;
|
|
||||||
|
|
||||||
const MIN_WIDTH: u16 = 100;
|
const MIN_WIDTH: u16 = 100;
|
||||||
const MIN_HEIGHT: u16 = 25;
|
const MIN_HEIGHT: u16 = 25;
|
||||||
|
@ -40,14 +39,12 @@ P* - privileged *
|
||||||
";
|
";
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
/// This must be passed to retain the temp dir until the end of the program
|
|
||||||
_temp_dir: TempDir,
|
|
||||||
/// Selected theme
|
/// Selected theme
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
/// Currently focused area
|
/// Currently focused area
|
||||||
pub focus: Focus,
|
pub focus: Focus,
|
||||||
/// List of tabs
|
/// List of tabs
|
||||||
tabs: Vec<Tab>,
|
tabs: TabList,
|
||||||
/// Current tab
|
/// Current tab
|
||||||
current_tab: ListState,
|
current_tab: ListState,
|
||||||
/// This stack keeps track of our "current directory". You can think of it as `pwd`. but not
|
/// This stack keeps track of our "current directory". You can think of it as `pwd`. but not
|
||||||
|
@ -88,11 +85,10 @@ enum SelectedItem {
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new(theme: Theme, override_validation: bool, size_bypass: bool) -> Self {
|
pub fn new(theme: Theme, override_validation: bool, size_bypass: 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 root_id = tabs[0].tree.root().id();
|
||||||
|
|
||||||
let mut state = Self {
|
let mut state = Self {
|
||||||
_temp_dir: temp_dir,
|
|
||||||
theme,
|
theme,
|
||||||
focus: Focus::List,
|
focus: Focus::List,
|
||||||
tabs,
|
tabs,
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub fn userguide() -> Result<String, DynError> {
|
||||||
let mut md = String::new();
|
let mut md = String::new();
|
||||||
md.push_str("<!-- THIS FILE IS GENERATED BY cargo xtask docgen -->\n# Walkthrough\n");
|
md.push_str("<!-- THIS FILE IS GENERATED BY cargo xtask docgen -->\n# Walkthrough\n");
|
||||||
|
|
||||||
let tabs = linutil_core::get_tabs(false).1;
|
let tabs = linutil_core::get_tabs(false);
|
||||||
|
|
||||||
for tab in tabs {
|
for tab in tabs {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
@ -24,7 +24,7 @@ pub fn userguide() -> Result<String, DynError> {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!(" Directory: {}", entry.name);
|
println!(" Directory: {}", entry.name);
|
||||||
|
|
||||||
if entry.name != "root".to_string() {
|
if entry.name != "root" {
|
||||||
md.push_str(&format!("\n### {}\n\n", entry.name));
|
md.push_str(&format!("\n### {}\n\n", entry.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +36,7 @@ pub fn userguide() -> Result<String, DynError> {
|
||||||
current_dir
|
current_dir
|
||||||
));
|
));
|
||||||
} */ // Commenting this for now, might be a good idea later
|
} */ // Commenting this for now, might be a good idea later
|
||||||
} else {
|
} else if !entry.description.is_empty() {
|
||||||
if !entry.description.is_empty() {
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!(" Entry: {}", entry.name);
|
println!(" Entry: {}", entry.name);
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
@ -49,7 +48,6 @@ pub fn userguide() -> Result<String, DynError> {
|
||||||
} */ // https://github.com/ChrisTitusTech/linutil/pull/753
|
} */ // https://github.com/ChrisTitusTech/linutil/pull/753
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(md)
|
Ok(md)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user