Refact temp-dir (#749)

This commit is contained in:
JEEVITHA KANNAN K S 2024-10-04 00:50:50 +05:30 committed by GitHub
parent 3c4a5dcecd
commit 26d0adc829
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 43 additions and 87 deletions

71
Cargo.lock generated
View File

@ -328,12 +328,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.15" version = "0.2.15"
@ -496,7 +490,7 @@ dependencies = [
"ego-tree", "ego-tree",
"include_dir", "include_dir",
"serde", "serde",
"tempdir", "temp-dir",
"toml", "toml",
"which", "which",
] ]
@ -514,8 +508,9 @@ dependencies = [
"linutil_core", "linutil_core",
"oneshot", "oneshot",
"portable-pty", "portable-pty",
"rand 0.8.5", "rand",
"ratatui", "ratatui",
"temp-dir",
"tree-sitter-bash", "tree-sitter-bash",
"tree-sitter-highlight", "tree-sitter-highlight",
"tui-term", "tui-term",
@ -716,19 +711,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.8.5" version = "0.8.5"
@ -737,7 +719,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [ dependencies = [
"libc", "libc",
"rand_chacha", "rand_chacha",
"rand_core 0.6.4", "rand_core",
] ]
[[package]] [[package]]
@ -747,24 +729,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [ dependencies = [
"ppv-lite86", "ppv-lite86",
"rand_core 0.6.4", "rand_core",
] ]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.6.4" version = "0.6.4"
@ -795,15 +762,6 @@ dependencies = [
"unicode-width 0.1.14", "unicode-width 0.1.14",
] ]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.4" version = "0.5.4"
@ -842,15 +800,6 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.37" version = "0.38.37"
@ -1063,14 +1012,10 @@ dependencies = [
] ]
[[package]] [[package]]
name = "tempdir" name = "temp-dir"
version = "0.3.7" version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" checksum = "bc1ee6eef34f12f765cb94725905c6312b6610ab2b0940889cfe58dae7bc3c72"
dependencies = [
"rand 0.4.6",
"remove_dir_all",
]
[[package]] [[package]]
name = "termios" name = "termios"

View File

@ -13,7 +13,7 @@ include = [
[dependencies] [dependencies]
include_dir = "0.7.4" include_dir = "0.7.4"
tempdir = "0.3.7" temp-dir = "0.1.14"
serde = { version = "1.0.205", features = ["derive"], default-features = false } serde = { version = "1.0.205", features = ["derive"], default-features = false }
toml = { version = "0.8.19", features = ["parse"], default-features = false } toml = { version = "0.8.19", features = ["parse"], default-features = false }
which = "6.0.3" which = "6.0.3"

View File

@ -10,24 +10,29 @@ use crate::{Command, ListNode, Tab};
use ego_tree::{NodeMut, Tree}; use ego_tree::{NodeMut, Tree};
use include_dir::{include_dir, Dir}; use include_dir::{include_dir, Dir};
use serde::Deserialize; use serde::Deserialize;
use tempdir::TempDir; 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) -> Vec<Tab> { pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
let tab_files = TabList::get_tabs(); let (temp_dir, tab_files) = TabList::get_tabs();
let tabs = tab_files.into_iter().map(|path| {
let directory = path.parent().unwrap().to_owned();
let data = std::fs::read_to_string(path).expect("Failed to read tab data");
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
if validate { let tabs: Vec<_> = tab_files
filter_entries(&mut tab_data.data); .into_iter()
} .map(|path| {
(tab_data, directory) let directory = path.parent().unwrap().to_owned();
}); let data = std::fs::read_to_string(path).expect("Failed to read tab data");
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
if validate {
filter_entries(&mut tab_data.data);
}
(tab_data, directory)
})
.collect();
let tabs: Vec<Tab> = tabs let tabs: Vec<Tab> = tabs
.into_iter()
.map( .map(
|( |(
TabEntry { TabEntry {
@ -57,7 +62,7 @@ pub fn get_tabs(validate: bool) -> Vec<Tab> {
if tabs.is_empty() { if tabs.is_empty() {
panic!("No tabs found"); panic!("No tabs found");
} }
tabs (temp_dir, tabs)
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -248,19 +253,20 @@ fn is_executable(path: &Path) -> bool {
} }
impl TabList { impl TabList {
fn get_tabs() -> Vec<PathBuf> { fn get_tabs() -> (TempDir, Vec<PathBuf>) {
let temp_dir = TempDir::new("linutil_scripts").unwrap().into_path(); let temp_dir = TempDir::new().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");
let tab_files = let tab_files = std::fs::read_to_string(temp_dir.path().join("tabs.toml"))
std::fs::read_to_string(temp_dir.join("tabs.toml")).expect("Failed to read tabs.toml"); .expect("Failed to read tabs.toml");
let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml"); let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");
let tab_paths = data
data.directories .directories
.iter() .iter()
.map(|path| temp_dir.join(path).join("tab_data.toml")) .map(|path| temp_dir.path().join(path).join("tab_data.toml"))
.collect() .collect();
(temp_dir, tab_paths)
} }
} }

View File

@ -22,6 +22,7 @@ oneshot = "0.1.8"
portable-pty = "0.8.1" portable-pty = "0.8.1"
ratatui = "0.28.1" ratatui = "0.28.1"
tui-term = "0.1.12" tui-term = "0.1.12"
temp-dir = "0.1.14"
unicode-width = "0.2.0" unicode-width = "0.2.0"
rand = { version = "0.8.5", optional = true } rand = { version = "0.8.5", optional = true }
linutil_core = { path = "../core", version = "24.9.28" } linutil_core = { path = "../core", version = "24.9.28" }

View File

@ -21,6 +21,7 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Paragraph}, widgets::{Block, Borders, List, ListState, Paragraph},
Frame, Frame,
}; };
use temp_dir::TempDir;
const MIN_WIDTH: u16 = 77; const MIN_WIDTH: u16 = 77;
const MIN_HEIGHT: u16 = 19; const MIN_HEIGHT: u16 = 19;
@ -40,6 +41,8 @@ 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
@ -78,10 +81,11 @@ pub struct ListEntry {
impl AppState { impl AppState {
pub fn new(theme: Theme, override_validation: bool) -> Self { pub fn new(theme: Theme, override_validation: bool) -> Self {
let tabs = linutil_core::get_tabs(!override_validation); let (temp_dir, 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,