Skip to content

Commit

Permalink
Merge pull request #274 from lj3954/restructure_linutil
Browse files Browse the repository at this point in the history
refactor: Split linutil into TUI and Core crates
  • Loading branch information
ChrisTitusTech authored Sep 12, 2024
2 parents c905aee + 82b89ef commit 7104991
Show file tree
Hide file tree
Showing 56 changed files with 167 additions and 139 deletions.
64 changes: 36 additions & 28 deletions Cargo.lock

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

32 changes: 7 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
[package]
name = "tui"
[workspace.package]
license = "MIT"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.16", features = ["derive"] }
crossterm = "0.28.1"
[workspace.dependencies]
ego-tree = "0.6.2"
oneshot = "0.1.8"
portable-pty = "0.8.1"
ratatui = "0.28.1"
tui-term = "0.1.12"
include_dir = "0.7.4"
tempdir = "0.3.7"
serde = { version = "1.0.205", features = ["derive"] }
toml = "0.8.19"
which = "6.0.3"
unicode-width = "0.1.13"

[build-dependencies]
chrono = "0.4.33"

[[bin]]
name = "linutil"
path = "src/main.rs"


[workspace]
members = ["tui", "core"]
resolver = "2"

[profile.release]
opt-level = 3
opt-level = "z"
debug = false
lto = true
codegen-units = 1
Expand Down
12 changes: 0 additions & 12 deletions build.rs

This file was deleted.

13 changes: 13 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "linutil_core"
edition = "2021"
version.workspace = true
license.workspace = true

[dependencies]
include_dir = "0.7.4"
tempdir = "0.3.7"
serde = { version = "1.0.205", features = ["derive"] }
toml = "0.8.19"
which = "6.0.3"
ego-tree = { workspace = true }
96 changes: 47 additions & 49 deletions src/tabs.rs → core/src/inner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
use crate::running_command::Command;
use crate::{Command, ListNode, Tab};
use ego_tree::{NodeMut, Tree};
use include_dir::{include_dir, Dir};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use tempdir::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");

if validate {
filter_entries(&mut tab_data.data);
}
(tab_data, directory)
});

let tabs: Vec<Tab> = tabs
.map(|(TabEntry { name, data }, directory)| {
let mut tree = Tree::new(ListNode {
name: "root".to_string(),
command: Command::None,
});
let mut root = tree.root_mut();
create_directory(data, &mut root, &directory);
Tab { name, tree }
})
.collect();

if tabs.is_empty() {
panic!("No tabs found");
}
tabs
}

#[derive(Deserialize)]
struct TabList {
Expand Down Expand Up @@ -79,49 +114,6 @@ enum SystemDataType {
CommandExists,
}

#[derive(Hash, Eq, PartialEq)]
pub struct Tab {
pub name: String,
pub tree: Tree<ListNode>,
}

#[derive(Clone, Hash, Eq, PartialEq)]
pub struct ListNode {
pub name: String,
pub command: Command,
}

pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec<Tab> {
let tab_files = TabList::get_tabs(command_dir);
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 {
filter_entries(&mut tab_data.data);
}
(tab_data, directory)
});

let tabs: Vec<Tab> = tabs
.map(|(TabEntry { name, data }, directory)| {
let mut tree = Tree::new(ListNode {
name: "root".to_string(),
command: Command::None,
});
let mut root = tree.root_mut();
create_directory(data, &mut root, &directory);
Tab { name, tree }
})
.collect();

if tabs.is_empty() {
panic!("No tabs found");
}
tabs
}

fn filter_entries(entries: &mut Vec<Entry>) {
entries.retain_mut(|entry| {
if !entry.is_supported() {
Expand Down Expand Up @@ -176,15 +168,21 @@ fn create_directory(data: Vec<Entry>, node: &mut NodeMut<ListNode>, command_dir:
}
}
}

impl TabList {
fn get_tabs(command_dir: &Path) -> Vec<PathBuf> {
let tab_files = std::fs::read_to_string(command_dir.join("tabs.toml"))
.expect("Failed to read tabs.toml");
fn get_tabs() -> Vec<PathBuf> {
let temp_dir = TempDir::new("linutil_scripts").unwrap().into_path();
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 data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");

data.directories
.into_iter()
.map(|path| command_dir.join(path).join("tab_data.toml"))
.iter()
.map(|path| temp_dir.join(path).join("tab_data.toml"))
.collect()
}
}
25 changes: 25 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod inner;

use ego_tree::Tree;
use std::path::PathBuf;

pub use inner::get_tabs;

#[derive(Clone, Hash, Eq, PartialEq)]
pub enum Command {
Raw(String),
LocalFile(PathBuf),
None, // Directory
}

#[derive(Clone, Hash, Eq, PartialEq)]
pub struct Tab {
pub name: String,
pub tree: Tree<ListNode>,
}

#[derive(Clone, Hash, Eq, PartialEq)]
pub struct ListNode {
pub name: String,
pub command: Command,
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "linutil_tui"
edition = "2021"
version.workspace = true
license.workspace = true

[dependencies]
clap = { version = "4.5.16", features = ["derive"] }
crossterm = "0.28.1"
ego-tree = { workspace = true }
oneshot = "0.1.8"
portable-pty = "0.8.1"
ratatui = "0.28.1"
tui-term = "0.1.12"
unicode-width = "0.1.13"
linutil_core = { path = "../core" }

[build-dependencies]
chrono = "0.4.33"

[[bin]]
name = "linutil"
path = "src/main.rs"
7 changes: 7 additions & 0 deletions tui/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
// Add current date as a variable to be displayed in the 'Linux Toolbox' text.
println!(
"cargo:rustc-env=BUILD_DATE={}",
chrono::Local::now().format("%Y-%m-%d")
);
}
3 changes: 2 additions & 1 deletion src/filter.rs → tui/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{state::ListEntry, tabs::Tab, theme::Theme};
use crate::{state::ListEntry, theme::Theme};
use crossterm::event::{KeyCode, KeyEvent};
use ego_tree::NodeId;
use linutil_core::Tab;
use ratatui::{
layout::{Position, Rect},
style::Style,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/floating_text.rs → tui/src/floating_text.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
float::FloatContent,
hint::{Shortcut, ShortcutList},
running_command::Command,
};
use crossterm::event::{KeyCode, KeyEvent};
use linutil_core::Command;
use ratatui::{
layout::Rect,
style::{Style, Stylize},
Expand Down
File renamed without changes.
Loading

0 comments on commit 7104991

Please sign in to comment.