Skip to content

Commit dbf6f91

Browse files
jeevithakannan2afonsofrancof
authored andcommitted
Refact temp-dir (ChrisTitusTech#749)
1 parent f27852b commit dbf6f91

File tree

5 files changed

+43
-87
lines changed

5 files changed

+43
-87
lines changed

Cargo.lock

Lines changed: 8 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include = [
1313

1414
[dependencies]
1515
include_dir = "0.7.4"
16-
tempdir = "0.3.7"
16+
temp-dir = "0.1.14"
1717
serde = { version = "1.0.205", features = ["derive"], default-features = false }
1818
toml = { version = "0.8.19", features = ["parse"], default-features = false }
1919
which = "6.0.3"

core/src/inner.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@ use crate::{Command, ListNode, Tab};
1010
use ego_tree::{NodeMut, Tree};
1111
use include_dir::{include_dir, Dir};
1212
use serde::Deserialize;
13-
use tempdir::TempDir;
13+
use temp_dir::TempDir;
1414

1515
const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs");
1616

17-
pub fn get_tabs(validate: bool) -> Vec<Tab> {
18-
let tab_files = TabList::get_tabs();
19-
let tabs = tab_files.into_iter().map(|path| {
20-
let directory = path.parent().unwrap().to_owned();
21-
let data = std::fs::read_to_string(path).expect("Failed to read tab data");
22-
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
17+
pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
18+
let (temp_dir, tab_files) = TabList::get_tabs();
2319

24-
if validate {
25-
filter_entries(&mut tab_data.data);
26-
}
27-
(tab_data, directory)
28-
});
20+
let tabs: Vec<_> = tab_files
21+
.into_iter()
22+
.map(|path| {
23+
let directory = path.parent().unwrap().to_owned();
24+
let data = std::fs::read_to_string(path).expect("Failed to read tab data");
25+
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
26+
27+
if validate {
28+
filter_entries(&mut tab_data.data);
29+
}
30+
(tab_data, directory)
31+
})
32+
.collect();
2933

3034
let tabs: Vec<Tab> = tabs
35+
.into_iter()
3136
.map(
3237
|(
3338
TabEntry {
@@ -57,7 +62,7 @@ pub fn get_tabs(validate: bool) -> Vec<Tab> {
5762
if tabs.is_empty() {
5863
panic!("No tabs found");
5964
}
60-
tabs
65+
(temp_dir, tabs)
6166
}
6267

6368
#[derive(Deserialize)]
@@ -248,19 +253,20 @@ fn is_executable(path: &Path) -> bool {
248253
}
249254

250255
impl TabList {
251-
fn get_tabs() -> Vec<PathBuf> {
252-
let temp_dir = TempDir::new("linutil_scripts").unwrap().into_path();
256+
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
257+
let temp_dir = TempDir::new().unwrap();
253258
TAB_DATA
254259
.extract(&temp_dir)
255260
.expect("Failed to extract the saved directory");
256261

257-
let tab_files =
258-
std::fs::read_to_string(temp_dir.join("tabs.toml")).expect("Failed to read tabs.toml");
262+
let tab_files = std::fs::read_to_string(temp_dir.path().join("tabs.toml"))
263+
.expect("Failed to read tabs.toml");
259264
let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");
260-
261-
data.directories
265+
let tab_paths = data
266+
.directories
262267
.iter()
263-
.map(|path| temp_dir.join(path).join("tab_data.toml"))
264-
.collect()
268+
.map(|path| temp_dir.path().join(path).join("tab_data.toml"))
269+
.collect();
270+
(temp_dir, tab_paths)
265271
}
266272
}

tui/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ oneshot = "0.1.8"
2222
portable-pty = "0.8.1"
2323
ratatui = "0.28.1"
2424
tui-term = "0.1.12"
25+
temp-dir = "0.1.14"
2526
unicode-width = "0.2.0"
2627
rand = { version = "0.8.5", optional = true }
2728
linutil_core = { path = "../core", version = "24.9.28" }

tui/src/state.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ratatui::{
2121
widgets::{Block, Borders, List, ListState, Paragraph},
2222
Frame,
2323
};
24+
use temp_dir::TempDir;
2425

2526
const MIN_WIDTH: u16 = 77;
2627
const MIN_HEIGHT: u16 = 19;
@@ -33,13 +34,15 @@ FM - file modification
3334
I - installation (privileged)
3435
MP - package manager actions
3536
SI - full system installation
36-
SS - systemd actions (privileged)
37+
SS - systemd actions (privileged)
3738
RP - package removal
3839
3940
P* - privileged *
4041
";
4142

4243
pub struct AppState {
44+
/// This must be passed to retain the temp dir until the end of the program
45+
_temp_dir: TempDir,
4346
/// Selected theme
4447
theme: Theme,
4548
/// Currently focused area
@@ -78,10 +81,11 @@ pub struct ListEntry {
7881

7982
impl AppState {
8083
pub fn new(theme: Theme, override_validation: bool) -> Self {
81-
let tabs = linutil_core::get_tabs(!override_validation);
84+
let (temp_dir, tabs) = linutil_core::get_tabs(!override_validation);
8285
let root_id = tabs[0].tree.root().id();
8386

8487
let mut state = Self {
88+
_temp_dir: temp_dir,
8589
theme,
8690
focus: Focus::List,
8791
tabs,

0 commit comments

Comments
 (0)