Skip to content

Commit

Permalink
more restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Mar 11, 2023
1 parent 30ca917 commit 5ea5730
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/fs/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::context;
use crate::render::context;
use std::{
convert::From,
error::Error as StdError,
Expand Down
3 changes: 0 additions & 3 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/// Operations that decide how to present info about disk usage.
pub mod disk_usage;

/// Errors related to filesystem traversal.
pub mod error;

Expand Down
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::process::ExitCode;

use clap::{CommandFactory, FromArgMatches};
use context::Context;
use render::tree::{self, Tree};

/// CLI rules and definitions and context wherein [Tree] will operate.
mod context;
use render::{
context::Context,
tree::{self, Tree},
};

/// Filesystem operations.
mod fs;
Expand Down
6 changes: 1 addition & 5 deletions src/context/config.rs → src/render/context/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
env,
fs,
path::PathBuf,
};
use std::{env, fs, path::PathBuf};

pub const CONFIG_ENV_VAR: &str = "ERDTREE_CONFIG_PATH";
pub const CONFIG_NAME: &str = ".erdtreerc";
Expand Down
43 changes: 6 additions & 37 deletions src/context/mod.rs → src/render/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{FromArgMatches, Parser, ValueEnum};
use super::{disk_usage::DiskUsage, order::SortType};
use clap::Parser;
use ignore::{
overrides::{Override, OverrideBuilder},
WalkBuilder, WalkParallel,
Expand Down Expand Up @@ -27,7 +28,7 @@ pub struct Context {

/// Print physical or logical file size
#[arg(short, long, value_enum, default_value_t = DiskUsage::Logical)]
disk_usage: DiskUsage,
pub disk_usage: DiskUsage,

/// Include or exclude files using glob patterns
#[arg(short, long)]
Expand Down Expand Up @@ -66,8 +67,8 @@ pub struct Context {
pub scale: usize,

/// Sort-order to display directory content
#[arg(short, long, value_enum, default_value_t = Order::None)]
sort: Order,
#[arg(short, long, value_enum)]
sort: Option<SortType>,

/// Always sorts directories above files
#[arg(long)]
Expand All @@ -86,33 +87,6 @@ pub struct Context {
pub suppress_size: bool,
}

/// Order in which to print nodes.
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum Order {
/// Sort entries by file name
Name,

/// Sort entries by size smallest to largest, top to bottom
Size,

/// Sort entries by size largest to smallest, bottom to top
SizeRev,

/// No sorting
None,
}

/// Display disk usage output as either logical size or physical size.
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum DiskUsage {
/// How many bytes does a file contain
Logical,

/// How much actual space on disk based on blocks allocated, taking into account sparse files
/// and compression.
Physical,
}

impl Context {
/// Returns reference to the path of the root directory to be traversed.
pub fn dir(&self) -> &Path {
Expand All @@ -122,7 +96,7 @@ impl Context {
}

/// The sort-order used for printing.
pub fn sort(&self) -> Order {
pub fn sort(&self) -> Option<SortType> {
self.sort
}

Expand All @@ -131,11 +105,6 @@ impl Context {
self.dirs_first
}

/// Getter for `disk_usage` field.
pub fn disk_usage(&self) -> &DiskUsage {
&self.disk_usage
}

/// The max depth to print. Note that all directories are fully traversed to compute file
/// sizes; this just determines how much to print.
pub fn level(&self) -> Option<usize> {
Expand Down
14 changes: 2 additions & 12 deletions src/fs/disk_usage.rs → src/render/disk_usage.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::context;
use ansi_term::Color;
use clap::ValueEnum;
use filesize::PathExt;
use std::{
convert::From,
fmt::{self, Display},
fs::Metadata,
ops::AddAssign,
path::Path,
};

/// Determines between logical or physical size for display
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum DiskUsage {
/// How many bytes does a file contain
Logical,
Expand Down Expand Up @@ -101,15 +100,6 @@ impl Display for FileSize {
}
}

impl From<&context::DiskUsage> for DiskUsage {
fn from(du: &context::DiskUsage) -> Self {
match du {
context::DiskUsage::Logical => Self::Logical,
context::DiskUsage::Physical => Self::Physical,
}
}
}

impl Display for Prefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
8 changes: 8 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/// CLI rules and definitions and context wherein [Tree] will operate.
///
/// [`Tree`]: tree::Tree
pub mod context;

/// Operations that decide how to present info about disk usage.
pub mod disk_usage;

/// Contains components of the [`Tree`] data structure that derive from [`DirEntry`].
///
/// [`Tree`]: tree::Tree
Expand Down
6 changes: 2 additions & 4 deletions src/render/node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use super::get_ls_colors;
use crate::{
fs::inode::Inode,
icons::{self, icon_from_ext, icon_from_file_name, icon_from_file_type},
fs::{
disk_usage::{DiskUsage, FileSize},
inode::Inode,
},
render::disk_usage::{DiskUsage, FileSize},
};
use ansi_term::Color;
use ansi_term::Style;
Expand Down
30 changes: 10 additions & 20 deletions src/render/order.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use super::node::Node;
use crate::context;
use clap::ValueEnum;
use std::{cmp::Ordering, convert::From};

/// Order in which to print nodes.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]
pub enum SortType {
/// Sort entries by file name
Name,

/// Sort entries by size smallest to largest, top to bottom
Size,

/// Sort entries by size largest to smallest, bottom to top
SizeRev,
None,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -74,22 +78,8 @@ impl SortType {
}
}

impl From<(context::Order, bool)> for Order {
fn from((order, dir_first): (context::Order, bool)) -> Self {
Order {
sort: order.into(),
dir_first,
}
}
}

impl From<context::Order> for SortType {
fn from(ord: context::Order) -> Self {
match ord {
context::Order::Name => SortType::Name,
context::Order::Size => SortType::Size,
context::Order::SizeRev => SortType::SizeRev,
context::Order::None => SortType::None,
}
impl From<(SortType, bool)> for Order {
fn from((sort, dir_first): (SortType, bool)) -> Self {
Order { sort, dir_first }
}
}
27 changes: 14 additions & 13 deletions src/render/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use super::{
order::Order,
};
use crate::{
context::Context,
fs::{
fs::error::Error,
render::{
context::Context,
disk_usage::{DiskUsage, FileSize},
error::Error,
},
};
use crossbeam::channel::{self, Sender};
Expand All @@ -33,7 +33,7 @@ pub struct Tree {
icons: bool,
pub level: Option<usize>,
#[allow(dead_code)]
order: Order,
order: Option<Order>,
root: Node,
#[allow(dead_code)]
scale: usize,
Expand All @@ -49,14 +49,14 @@ impl Tree {
/// Initializes a [Tree].
pub fn new(
walker: WalkParallel,
order: Order,
order: Option<Order>,
level: Option<usize>,
icons: bool,
disk_usage: DiskUsage,
scale: usize,
suppress_size: bool,
) -> TreeResult<Self> {
let root = Self::traverse(walker, &order, icons, &disk_usage, scale, suppress_size)?;
let root = Self::traverse(walker, order, icons, &disk_usage, scale, suppress_size)?;

Ok(Self {
disk_usage,
Expand All @@ -81,7 +81,7 @@ impl Tree {
/// outside of the parallel traversal step please report an issue.
fn traverse(
walker: WalkParallel,
order: &Order,
order: Option<Order>,
icons: bool,
disk_usage: &DiskUsage,
scale: usize,
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Tree {
fn assemble_tree(
current_dir: &mut Node,
branches: &mut Branches,
order: &Order,
order: Option<Order>,
disk_usage: &DiskUsage,
scale: usize,
) {
Expand Down Expand Up @@ -192,9 +192,10 @@ impl Tree {
current_node.set_file_size(dir_size)
}

order
.comparator()
.map(|func| current_node.children_mut().map(|nodes| nodes.sort_by(func)));
if let Some(ord) = order {
ord.comparator()
.map(|func| current_node.children_mut().map(|nodes| nodes.sort_by(func)));
}
}
}

Expand All @@ -203,8 +204,8 @@ impl TryFrom<Context> for Tree {

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());
let order = clargs.sort().map(|s| Order::from((s, clargs.dirs_first())));
let du = clargs.disk_usage;
let scale = clargs.scale;
let suppress_size = clargs.suppress_size;
let tree = Tree::new(
Expand Down

0 comments on commit 5ea5730

Please sign in to comment.