Skip to content

Commit

Permalink
restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Mar 11, 2023
1 parent afe372a commit 30ca917
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

24 changes: 24 additions & 0 deletions src/context/config.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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()
})
}
13 changes: 8 additions & 5 deletions src/cli.rs → src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser, ValueEnum};
use clap::{FromArgMatches, Parser, ValueEnum};
use ignore::{
overrides::{Override, OverrideBuilder},
WalkBuilder, WalkParallel,
Expand All @@ -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. <[email protected]>")]
#[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<PathBuf>,

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Self, Self::Error> {
fn try_from(clargs: &Context) -> Result<Self, Self::Error> {
let root = fs::canonicalize(clargs.dir())?;

fs::metadata(&root).map_err(|e| Error::DirNotFound(format!("{}: {e}", root.display())))?;
Expand Down
10 changes: 5 additions & 5 deletions src/fs/disk_usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli;
use crate::context;
use ansi_term::Color;
use filesize::PathExt;
use std::{
Expand Down Expand Up @@ -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,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/fs/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli;
use crate::context;
use std::{
convert::From,
error::Error as StdError,
Expand All @@ -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,
}
Expand All @@ -25,8 +25,8 @@ impl Display for Error {

impl StdError for Error {}

impl From<cli::Error> for Error {
fn from(e: cli::Error) -> Self {
impl From<context::Error> for Error {
fn from(e: context::Error) -> Self {
Self::CliError(e)
}
}
3 changes: 0 additions & 3 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
18 changes: 11 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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;

/// Dev icons.
mod icons;

/// Tools and operations to display root-directory interact with ANSI configs.
mod render;

/// Common utilities.
mod utils;

Expand All @@ -26,9 +29,10 @@ fn main() -> ExitCode {
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
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}");
Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions src/fs/erdtree/node.rs → src/render/node.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/fs/erdtree/order.rs → src/render/order.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -74,22 +74,22 @@ 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,
}
}
}

impl From<cli::Order> for SortType {
fn from(ord: cli::Order) -> Self {
impl From<context::Order> 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,
}
}
}
16 changes: 9 additions & 7 deletions src/fs/erdtree/tree/mod.rs → src/render/tree/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -29,7 +31,7 @@ pub struct Tree {
disk_usage: DiskUsage,
#[allow(dead_code)]
icons: bool,
level: Option<usize>,
pub level: Option<usize>,
#[allow(dead_code)]
order: Order,
root: Node,
Expand Down Expand Up @@ -196,10 +198,10 @@ impl Tree {
}
}

impl TryFrom<Clargs> for Tree {
impl TryFrom<Context> for Tree {
type Error = Error;

fn try_from(clargs: Clargs) -> Result<Self, Self::Error> {
fn try_from(clargs: Context) -> Result<Self, Self::Error> {
let walker = WalkParallel::try_from(&clargs)?;
let order = Order::from((clargs.sort(), clargs.dirs_first()));
let du = DiskUsage::from(clargs.disk_usage());
Expand Down
File renamed without changes.

0 comments on commit 30ca917

Please sign in to comment.