Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: Handle temporary directories entirely within core #754

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

31 changes: 25 additions & 6 deletions core/src/inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::File,
io::{BufRead, BufReader, Read},
ops::{Deref, DerefMut},
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
rc::Rc,
Expand All @@ -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<Tab>) {
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<Tab>, TempDir);

// Implement deref to allow Vec<Tab> methods to be called on TabList
lj3954 marked this conversation as resolved.
Show resolved Hide resolved
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
}
}

pub fn get_tabs(validate: bool) -> TabList {
let (temp_dir, tab_files) = TabDirectories::get_tabs();

let tabs: Vec<_> = tab_files
.into_iter()
Expand Down Expand Up @@ -62,11 +81,11 @@ pub fn get_tabs(validate: bool) -> (TempDir, Vec<Tab>) {
if tabs.is_empty() {
panic!("No tabs found");
}
(temp_dir, tabs)
TabList(tabs, temp_dir)
}

#[derive(Deserialize)]
struct TabList {
struct TabDirectories {
directories: Vec<PathBuf>,
}

Expand Down Expand Up @@ -252,9 +271,9 @@ fn is_executable(path: &Path) -> bool {
.unwrap_or(false)
}

impl TabList {
impl TabDirectories {
fn get_tabs() -> (TempDir, Vec<PathBuf>) {
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");
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 3 additions & 7 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand All @@ -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<Tab>,
tabs: TabList,
/// Current tab
current_tab: ListState,
/// This stack keeps track of our "current directory". You can think of it as `pwd`. but not
Expand Down Expand Up @@ -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,
Expand Down