Skip to content

Commit

Permalink
Merge pull request #163 from solidiquis/refactor/render-modules
Browse files Browse the repository at this point in the history
Refactor/render modules
  • Loading branch information
solidiquis authored May 14, 2023
2 parents c593044 + 74277fb commit 74f4b81
Show file tree
Hide file tree
Showing 39 changed files with 746 additions and 850 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions src/context/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use clap::ValueEnum;

/// Which layout to use when rendering the tree.
#[derive(Copy, Clone, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Type {
/// Outputs the tree with the root node at the bottom of the output
#[default]
Regular,

/// Outputs the tree with the root node at the top of the output
Inverted,

/// Outputs a flat layout using paths rather than an ASCII tree.
Flat,
}
15 changes: 7 additions & 8 deletions src/render/context/mod.rs → src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub mod error;
/// Common cross-platform file-types.
pub mod file;

/// For determining the output layout.
pub mod layout;

/// Utilities to print output.
pub mod output;

Expand Down Expand Up @@ -67,10 +70,6 @@ pub struct Context {
#[arg(short = 'f', long)]
pub follow: bool,

/// Print disk usage information in plain format without the ASCII tree
#[arg(short = 'F', long)]
pub flat: bool,

/// Print disk usage in human-readable format
#[arg(short = 'H', long)]
pub human: bool,
Expand Down Expand Up @@ -154,9 +153,9 @@ pub struct Context {
#[arg(long)]
pub dirs_only: bool,

/// Print tree with the root directory at the topmost position
#[arg(long)]
pub inverted: bool,
/// Which kind of layout to use when rendering the output
#[arg(long, value_enum, default_value_t = layout::Type::default())]
pub layout: layout::Type,

/// Don't read configuration file
#[arg(long)]
Expand Down Expand Up @@ -346,7 +345,7 @@ impl Context {
/// files, directories will always be included since matched files will need to be bridged back
/// to the root node somehow. Empty sets not producing an output is handled by [`Tree`].
///
/// [`Tree`]: crate::render::tree::Tree
/// [`Tree`]: crate::tree::Tree
pub fn regex_predicate(&self) -> Predicate {
let Some(pattern) = self.pattern.as_ref() else {
return Err(Error::PatternNotProvided);
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/render/context/sort.rs → src/context/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub enum Type {

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

// Sort entries by newer to older Accessing Date
//Access,

Expand Down
2 changes: 1 addition & 1 deletion src/render/context/test.rs → src/context/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::render::context::sort;
use crate::context::sort;
use clap::{CommandFactory, FromArgMatches};

use super::{config, Context};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::units::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix};
use crate::{
render::styles::{self, get_du_theme, get_placeholder_style},
styles::{self, get_du_theme, get_placeholder_style},
utils, Context,
};
use ansi_term::Style;
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions src/fs/permissions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub struct FileMode {
other_permissions: class::Permissions,
}

/// Implements [Display] which presents symbolic notation of file permissions with the extended
/// attributes.
pub struct FileModeXAttrs<'a>(pub &'a FileMode);

impl FileMode {
/// Constructor for [`FileMode`].
pub const fn new(
Expand Down Expand Up @@ -96,6 +100,14 @@ impl Display for FileMode {
}
}

/// For representing file permissions with extended attributes in symbolic notation.
impl Display for FileModeXAttrs<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mode = self.0;
write!(f, "{mode}@")
}
}

/// For the octal representation of permissions
impl Octal for FileMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
58 changes: 38 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@
)]

use clap::CommandFactory;
use render::{
context::Context,
tree::{
display::{Flat, Inverted, Regular},
Tree,
},
};
use context::{layout, Context};
use render::{Engine, Flat, Inverted, Regular};
use std::{error::Error, io::stdout};
use tree::Tree;

/// Operations to wrangle ANSI escaped strings.
mod ansi;

/// CLI rules and definitions as well as context to be injected throughout the entire program.
mod context;

/// Operations relevant to the computation and presentation of disk usage.
mod disk_usage;

/// Filesystem operations.
mod fs;

/// Dev icons.
/// All things related to icons on how to map certain files to the appropriate icons.
mod icons;

/// Tools and operations to display root-directory.
/// Concerned with taking an initialized [`Tree`] and its [`Node`]s and rendering the output.
///
/// [`Tree`]: tree::Tree
/// [`Node`]: tree::node::Node
mod render;

/// Global used throughout the program to paint the output.
mod styles;

/// Houses the primary data structures that are used to virtualize the filesystem, containing also
/// information on how the tree output should be ultimately rendered.
mod tree;

/// Utilities relating to interacting with tty properties.
mod tty;

Expand All @@ -53,17 +65,23 @@ fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}

render::styles::init(ctx.no_color());

if ctx.flat {
let tree = Tree::<Flat>::try_init(ctx)?;
println!("{tree}");
} else if ctx.inverted {
let tree = Tree::<Inverted>::try_init(ctx)?;
println!("{tree}");
} else {
let tree = Tree::<Regular>::try_init(ctx)?;
println!("{tree}");
styles::init(ctx.no_color());

let (tree, ctx) = Tree::try_init_and_update_context(ctx)?;

match ctx.layout {
layout::Type::Flat => {
let render = Engine::<Flat>::new(tree, ctx);
println!("{render}");
}
layout::Type::Inverted => {
let render = Engine::<Inverted>::new(tree, ctx);
println!("{render}");
}
layout::Type::Regular => {
let render = Engine::<Regular>::new(tree, ctx);
println!("{render}");
}
}

Ok(())
Expand Down
5 changes: 0 additions & 5 deletions src/render/display/mod.rs

This file was deleted.

Loading

0 comments on commit 74f4b81

Please sign in to comment.