Skip to content

Commit

Permalink
Refact temp-dir (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 authored Oct 3, 2024
1 parent 3c4a5dc commit 26d0adc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 87 deletions.
71 changes: 8 additions & 63 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include = [

[dependencies]
include_dir = "0.7.4"
tempdir = "0.3.7"
temp-dir = "0.1.14"
serde = { version = "1.0.205", features = ["derive"], default-features = false }
toml = { version = "0.8.19", features = ["parse"], default-features = false }
which = "6.0.3"
Expand Down
48 changes: 27 additions & 21 deletions core/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ use crate::{Command, ListNode, Tab};
use ego_tree::{NodeMut, Tree};
use include_dir::{include_dir, Dir};
use serde::Deserialize;
use tempdir::TempDir;
use temp_dir::TempDir;

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

pub fn get_tabs(validate: bool) -> Vec<Tab> {
let 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");
pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
let (temp_dir, tab_files) = TabList::get_tabs();

if validate {
filter_entries(&mut tab_data.data);
}
(tab_data, directory)
});
let tabs: Vec<_> = 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 {
filter_entries(&mut tab_data.data);
}
(tab_data, directory)
})
.collect();

let tabs: Vec<Tab> = tabs
.into_iter()
.map(
|(
TabEntry {
Expand Down Expand Up @@ -57,7 +62,7 @@ pub fn get_tabs(validate: bool) -> Vec<Tab> {
if tabs.is_empty() {
panic!("No tabs found");
}
tabs
(temp_dir, tabs)
}

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

impl TabList {
fn get_tabs() -> Vec<PathBuf> {
let temp_dir = TempDir::new("linutil_scripts").unwrap().into_path();
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
let temp_dir = TempDir::new().unwrap();
TAB_DATA
.extract(&temp_dir)
.expect("Failed to extract the saved directory");

let tab_files =
std::fs::read_to_string(temp_dir.join("tabs.toml")).expect("Failed to read tabs.toml");
let tab_files = std::fs::read_to_string(temp_dir.path().join("tabs.toml"))
.expect("Failed to read tabs.toml");
let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");

data.directories
let tab_paths = data
.directories
.iter()
.map(|path| temp_dir.join(path).join("tab_data.toml"))
.collect()
.map(|path| temp_dir.path().join(path).join("tab_data.toml"))
.collect();
(temp_dir, tab_paths)
}
}
1 change: 1 addition & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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" }
Expand Down
8 changes: 6 additions & 2 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Paragraph},
Frame,
};
use temp_dir::TempDir;

const MIN_WIDTH: u16 = 77;
const MIN_HEIGHT: u16 = 19;
Expand All @@ -33,13 +34,15 @@ FM - file modification
I - installation (privileged)
MP - package manager actions
SI - full system installation
SS - systemd actions (privileged)
SS - systemd actions (privileged)
RP - package removal
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
Expand Down Expand Up @@ -78,10 +81,11 @@ pub struct ListEntry {

impl AppState {
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 mut state = Self {
_temp_dir: temp_dir,
theme,
focus: Focus::List,
tabs,
Expand Down

0 comments on commit 26d0adc

Please sign in to comment.