From 30ca91765a33d83b2010bfcc60d12d7f2332709a Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Fri, 10 Mar 2023 18:03:37 -0800 Subject: [PATCH] restructuring --- Cargo.lock | 4 ++-- src/context/config.rs | 24 ++++++++++++++++++++++++ src/{cli.rs => context/mod.rs} | 13 ++++++++----- src/fs/disk_usage.rs | 10 +++++----- src/fs/error.rs | 8 ++++---- src/fs/mod.rs | 3 --- src/main.rs | 18 +++++++++++------- src/{fs/erdtree => render}/mod.rs | 0 src/{fs/erdtree => render}/node.rs | 11 +++++++---- src/{fs/erdtree => render}/order.rs | 18 +++++++++--------- src/{fs/erdtree => render}/tree/mod.rs | 16 +++++++++------- src/{fs/erdtree => render}/tree/ui.rs | 0 12 files changed, 79 insertions(+), 46 deletions(-) create mode 100644 src/context/config.rs rename src/{cli.rs => context/mod.rs} (95%) rename src/{fs/erdtree => render}/mod.rs (100%) rename src/{fs/erdtree => render}/node.rs (98%) rename src/{fs/erdtree => render}/order.rs (84%) rename src/{fs/erdtree => render}/tree/mod.rs (98%) rename src/{fs/erdtree => render}/tree/ui.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 3557cfcc..12354cc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,9 +508,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.152" +version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +checksum = "8cdd151213925e7f1ab45a9bbfb129316bd00799784b174b7cc7bcd16961c49e" [[package]] name = "strip-ansi-escapes" diff --git a/src/context/config.rs b/src/context/config.rs new file mode 100644 index 00000000..4bc58220 --- /dev/null +++ b/src/context/config.rs @@ -0,0 +1,24 @@ +use std::{ + env, + fs, + path::PathBuf, +}; + +pub const CONFIG_ENV_VAR: &str = "ERDTREE_CONFIG_PATH"; +pub const CONFIG_NAME: &str = ".erdtreerc"; + +pub fn read_config_to_string() -> Option { + env::var_os(CONFIG_ENV_VAR) + .map(PathBuf::from) + .map(fs::read_to_string) + .map(Result::ok) + .flatten() + .or_else(|| { + env::var_os("HOME") + .map(PathBuf::from) + .map(|p| p.join(CONFIG_NAME)) + .map(fs::read_to_string) + .map(Result::ok) + .flatten() + }) +} diff --git a/src/cli.rs b/src/context/mod.rs similarity index 95% rename from src/cli.rs rename to src/context/mod.rs index 2e532a47..13ed28ef 100644 --- a/src/cli.rs +++ b/src/context/mod.rs @@ -1,4 +1,4 @@ -use clap::{Parser, ValueEnum}; +use clap::{FromArgMatches, Parser, ValueEnum}; use ignore::{ overrides::{Override, OverrideBuilder}, WalkBuilder, WalkParallel, @@ -12,13 +12,16 @@ use std::{ usize, }; +/// Operations to load in `.erdtree.toml` defaults. +pub mod config; + /// Defines the CLI. #[derive(Parser, Debug)] #[command(name = "erdtree")] #[command(author = "Benjamin Nguyen. ")] #[command(version = "1.3.0")] #[command(about = "erdtree (et) is a multi-threaded filetree visualizer and disk usage analyzer.", long_about = None)] -pub struct Clargs { +pub struct Context { /// Root directory to traverse; defaults to current working directory dir: Option, @@ -110,7 +113,7 @@ pub enum DiskUsage { Physical, } -impl Clargs { +impl Context { /// Returns reference to the path of the root directory to be traversed. pub fn dir(&self) -> &Path { self.dir @@ -169,10 +172,10 @@ impl Clargs { } } -impl TryFrom<&Clargs> for WalkParallel { +impl TryFrom<&Context> for WalkParallel { type Error = Error; - fn try_from(clargs: &Clargs) -> Result { + fn try_from(clargs: &Context) -> Result { let root = fs::canonicalize(clargs.dir())?; fs::metadata(&root).map_err(|e| Error::DirNotFound(format!("{}: {e}", root.display())))?; diff --git a/src/fs/disk_usage.rs b/src/fs/disk_usage.rs index cf2d9b52..3190ce78 100644 --- a/src/fs/disk_usage.rs +++ b/src/fs/disk_usage.rs @@ -1,4 +1,4 @@ -use crate::cli; +use crate::context; use ansi_term::Color; use filesize::PathExt; use std::{ @@ -101,11 +101,11 @@ impl Display for FileSize { } } -impl From<&cli::DiskUsage> for DiskUsage { - fn from(du: &cli::DiskUsage) -> Self { +impl From<&context::DiskUsage> for DiskUsage { + fn from(du: &context::DiskUsage) -> Self { match du { - cli::DiskUsage::Logical => Self::Logical, - cli::DiskUsage::Physical => Self::Physical, + context::DiskUsage::Logical => Self::Logical, + context::DiskUsage::Physical => Self::Physical, } } } diff --git a/src/fs/error.rs b/src/fs/error.rs index 2a99f814..9f280ee2 100644 --- a/src/fs/error.rs +++ b/src/fs/error.rs @@ -1,4 +1,4 @@ -use crate::cli; +use crate::context; use std::{ convert::From, error::Error as StdError, @@ -8,7 +8,7 @@ use std::{ /// Errors that may occur during filesystem traversal. #[derive(Debug)] pub enum Error { - CliError(cli::Error), + CliError(context::Error), ExpectedParent, MissingRoot, } @@ -25,8 +25,8 @@ impl Display for Error { impl StdError for Error {} -impl From for Error { - fn from(e: cli::Error) -> Self { +impl From for Error { + fn from(e: context::Error) -> Self { Self::CliError(e) } } diff --git a/src/fs/mod.rs b/src/fs/mod.rs index 137a852e..e36ca5f5 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -6,6 +6,3 @@ pub mod error; /// Operations pertaining to Inodes. pub mod inode; - -/// Tools and operations to display root-directory and its contents using ANSI colors. -pub mod erdtree; diff --git a/src/main.rs b/src/main.rs index 0c53864d..0d3a6d84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use std::process::ExitCode; use clap::{CommandFactory, FromArgMatches}; -use cli::Clargs; -use fs::erdtree::{self, tree::Tree}; +use context::Context; +use render::tree::{self, Tree}; -/// CLI rules and definitions. -mod cli; +/// CLI rules and definitions and context wherein [Tree] will operate. +mod context; /// Filesystem operations. mod fs; @@ -13,6 +13,9 @@ mod fs; /// Dev icons. mod icons; +/// Tools and operations to display root-directory interact with ANSI configs. +mod render; + /// Common utilities. mod utils; @@ -26,9 +29,10 @@ fn main() -> ExitCode { } fn run() -> Result<(), Box> { - erdtree::tree::ui::init(); - let matches = Clargs::command().args_override_self(true).get_matches(); - let clargs = Clargs::from_arg_matches(&matches)?; + tree::ui::init(); + let matches = Context::command().args_override_self(true).get_matches(); + let clargs = Context::from_arg_matches(&matches)?; + let tree = Tree::try_from(clargs)?; println!("{tree}"); diff --git a/src/fs/erdtree/mod.rs b/src/render/mod.rs similarity index 100% rename from src/fs/erdtree/mod.rs rename to src/render/mod.rs diff --git a/src/fs/erdtree/node.rs b/src/render/node.rs similarity index 98% rename from src/fs/erdtree/node.rs rename to src/render/node.rs index bc74ec5a..6ca6751b 100644 --- a/src/fs/erdtree/node.rs +++ b/src/render/node.rs @@ -1,8 +1,11 @@ -use super::{ - super::{disk_usage::{DiskUsage, FileSize}, inode::Inode}, - get_ls_colors, +use super::get_ls_colors; +use crate::{ + icons::{self, icon_from_ext, icon_from_file_name, icon_from_file_type}, + fs::{ + disk_usage::{DiskUsage, FileSize}, + inode::Inode, + }, }; -use crate::icons::{self, icon_from_ext, icon_from_file_name, icon_from_file_type}; use ansi_term::Color; use ansi_term::Style; use ignore::DirEntry; diff --git a/src/fs/erdtree/order.rs b/src/render/order.rs similarity index 84% rename from src/fs/erdtree/order.rs rename to src/render/order.rs index d33c7bae..9c423cbf 100644 --- a/src/fs/erdtree/order.rs +++ b/src/render/order.rs @@ -1,5 +1,5 @@ use super::node::Node; -use crate::cli; +use crate::context; use std::{cmp::Ordering, convert::From}; /// Order in which to print nodes. @@ -74,8 +74,8 @@ impl SortType { } } -impl From<(cli::Order, bool)> for Order { - fn from((order, dir_first): (cli::Order, bool)) -> Self { +impl From<(context::Order, bool)> for Order { + fn from((order, dir_first): (context::Order, bool)) -> Self { Order { sort: order.into(), dir_first, @@ -83,13 +83,13 @@ impl From<(cli::Order, bool)> for Order { } } -impl From for SortType { - fn from(ord: cli::Order) -> Self { +impl From for SortType { + fn from(ord: context::Order) -> Self { match ord { - cli::Order::Name => SortType::Name, - cli::Order::Size => SortType::Size, - cli::Order::SizeRev => SortType::SizeRev, - cli::Order::None => SortType::None, + context::Order::Name => SortType::Name, + context::Order::Size => SortType::Size, + context::Order::SizeRev => SortType::SizeRev, + context::Order::None => SortType::None, } } } diff --git a/src/fs/erdtree/tree/mod.rs b/src/render/tree/mod.rs similarity index 98% rename from src/fs/erdtree/tree/mod.rs rename to src/render/tree/mod.rs index a30b6e41..95ce3708 100644 --- a/src/fs/erdtree/tree/mod.rs +++ b/src/render/tree/mod.rs @@ -1,12 +1,14 @@ use super::{ - super::{ + node::{Node, NodePrecursor}, + order::Order, +}; +use crate::{ + context::Context, + fs::{ disk_usage::{DiskUsage, FileSize}, error::Error, }, - node::{Node, NodePrecursor}, - order::Order, }; -use crate::cli::Clargs; use crossbeam::channel::{self, Sender}; use ignore::{WalkParallel, WalkState}; use std::{ @@ -29,7 +31,7 @@ pub struct Tree { disk_usage: DiskUsage, #[allow(dead_code)] icons: bool, - level: Option, + pub level: Option, #[allow(dead_code)] order: Order, root: Node, @@ -196,10 +198,10 @@ impl Tree { } } -impl TryFrom for Tree { +impl TryFrom for Tree { type Error = Error; - fn try_from(clargs: Clargs) -> Result { + fn try_from(clargs: Context) -> Result { let walker = WalkParallel::try_from(&clargs)?; let order = Order::from((clargs.sort(), clargs.dirs_first())); let du = DiskUsage::from(clargs.disk_usage()); diff --git a/src/fs/erdtree/tree/ui.rs b/src/render/tree/ui.rs similarity index 100% rename from src/fs/erdtree/tree/ui.rs rename to src/render/tree/ui.rs