Skip to content

Commit

Permalink
Moved 'interactive' portion of code into binary - break unit tests fo…
Browse files Browse the repository at this point in the history
…r now
  • Loading branch information
Sebastian Thiel committed Jun 5, 2019
1 parent c7ee6b5 commit 80f01db
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 53 deletions.
31 changes: 25 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
use crate::{
interactive::SortMode,
traverse::{EntryData, Tree, TreeIndex},
};
use crate::traverse::{EntryData, Tree, TreeIndex};
use byte_unit::{n_gb_bytes, n_gib_bytes, n_mb_bytes, n_mib_bytes, ByteUnit};
use itertools::Itertools;
use jwalk::WalkDir;
use petgraph::Direction;
use std::{fmt, path::Path, path::PathBuf};

pub(crate) fn path_of(tree: &Tree, mut node_idx: TreeIndex) -> PathBuf {
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
pub enum SortMode {
SizeDescending,
SizeAscending,
}

impl SortMode {
pub fn toggle_size(&mut self) {
use SortMode::*;
*self = match self {
SizeAscending => SizeDescending,
SizeDescending => SizeAscending,
}
}
}

impl Default for SortMode {
fn default() -> Self {
SortMode::SizeDescending
}
}

pub fn path_of(tree: &Tree, mut node_idx: TreeIndex) -> PathBuf {
const THE_ROOT: usize = 1;
let mut entries = Vec::new();

Expand Down Expand Up @@ -36,7 +55,7 @@ pub(crate) fn get_size_or_panic(tree: &Tree, node_idx: TreeIndex) -> u64 {
get_entry_or_panic(tree, node_idx).size
}

pub(crate) fn sorted_entries(
pub fn sorted_entries(
tree: &Tree,
node_idx: TreeIndex,
sorting: SortMode,
Expand Down
27 changes: 4 additions & 23 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::{
interactive::widgets::{DrawState, HelpPaneState, MainWindow},
ByteFormat,
};
use dua::{
path_of, sorted_entries,
traverse::{Traversal, TreeIndex},
ByteFormat, WalkOptions, WalkResult,
SortMode, WalkOptions, WalkResult,
};
use failure::Error;
use itertools::Itertools;
Expand All @@ -11,28 +14,6 @@ use std::{fmt, io, path::PathBuf};
use termion::input::{Keys, TermReadEventsAndRaw};
use tui::{backend::Backend, widgets::Widget, Terminal};

#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
pub enum SortMode {
SizeDescending,
SizeAscending,
}

impl SortMode {
pub fn toggle_size(&mut self) {
use SortMode::*;
*self = match self {
SizeAscending => SizeDescending,
SizeDescending => SizeAscending,
}
}
}

impl Default for SortMode {
fn default() -> Self {
SortMode::SizeDescending
}
}

#[derive(Clone, Copy)]
pub enum ByteVisualization {
Percentage,
Expand Down
13 changes: 7 additions & 6 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
interactive::{
widgets::{fill_background_to_right, List, ListState},
DisplayOptions, SortMode,
},
use crate::interactive::{
widgets::{fill_background_to_right, List, ListState},
DisplayOptions,
};
use dua::{
sorted_entries,
traverse::{Tree, TreeIndex},
SortMode,
};
use itertools::Itertools;
use std::path::Path;
Expand Down Expand Up @@ -43,7 +44,7 @@ impl<'a, 'b> Widget for Entries<'a, 'b> {
.next()
.is_none()
};
let path_of = |node_idx| crate::common::path_of(tree, node_idx);
let path_of = |node_idx| dua::path_of(tree, node_idx);

let entries = sorted_entries(tree, *root, *sorting);
let total: u64 = entries.iter().map(|(_, w)| w.size).sum();
Expand Down
10 changes: 4 additions & 6 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{
interactive::{
widgets::{Entries, Footer, Header, HelpPane, ListState},
AppState, DisplayOptions, FocussedPane,
},
traverse::Traversal,
use crate::interactive::{
widgets::{Entries, Footer, Header, HelpPane, ListState},
AppState, DisplayOptions, FocussedPane,
};
use dua::traverse::Traversal;
use tui::style::{Color, Style};
use tui::{
buffer::Buffer,
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ extern crate jwalk;
mod aggregate;
mod common;

pub mod interactive;
pub mod traverse;

pub use aggregate::aggregate;
Expand Down
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ extern crate failure;
extern crate failure_tools;
extern crate structopt;

use structopt::StructOpt;

use crate::interactive::TerminalApp;
use dua::{ByteFormat, Color, TraversalSorting};
use failure::{Error, ResultExt};
use failure_tools::ok_or_exit;
use std::{fs, io, io::Write, path::PathBuf, process};
use termion::input::TermRead;
use termion::{raw::IntoRawMode, screen::AlternateScreen};
use structopt::StructOpt;
use termion::{input::TermRead, raw::IntoRawMode, screen::AlternateScreen};
use tui::{backend::TermionBackend, Terminal};

mod interactive;
mod options;

fn run() -> Result<(), Error> {
Expand Down Expand Up @@ -39,11 +39,7 @@ fn run() -> Result<(), Error> {
Terminal::new(backend)
.with_context(|_| "Interactive mode requires a connected terminal")?
};
let mut app = dua::interactive::TerminalApp::initialize(
&mut terminal,
walk_options,
paths_from(input)?,
)?;
let mut app = TerminalApp::initialize(&mut terminal, walk_options, paths_from(input)?)?;
app.process_events(&mut terminal, io::stdin().keys())?
}
Some(Aggregate {
Expand Down
4 changes: 2 additions & 2 deletions tests/interactive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod app {
use dua::{
interactive::{SortMode, TerminalApp},
interactive::TerminalApp,
traverse::{EntryData, Tree, TreeIndex},
ByteFormat, Color, TraversalSorting, WalkOptions,
ByteFormat, Color, SortMode, TraversalSorting, WalkOptions,
};
use failure::Error;
use petgraph::prelude::NodeIndex;
Expand Down

0 comments on commit 80f01db

Please sign in to comment.