diff options
author | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-05 08:20:55 +0530 |
---|---|---|
committer | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-05 08:20:55 +0530 |
commit | e9a8614152b6f719cc748c377ffe863b19a50b7e (patch) | |
tree | a52230f6bab839ad8c0b8530fcd36f1bc342a552 /src | |
parent | 50438ef584d5f2ade0a0501ebca151c99893580f (diff) |
make app.rs into module directory, incl. further splits
Diffstat (limited to 'src')
-rw-r--r-- | src/interactive/app/bytevis.rs | 90 | ||||
-rw-r--r-- | src/interactive/app/eventloop.rs | 101 | ||||
-rw-r--r-- | src/interactive/app/mod.rs | 2 |
3 files changed, 97 insertions, 96 deletions
diff --git a/src/interactive/app/bytevis.rs b/src/interactive/app/bytevis.rs new file mode 100644 index 0000000..b27da6d --- /dev/null +++ b/src/interactive/app/bytevis.rs @@ -0,0 +1,90 @@ +use dua::{ByteFormat, WalkOptions}; +use std::fmt; + +#[derive(Clone, Copy)] +pub enum ByteVisualization { + Percentage, + Bar, + LongBar, + PercentageAndBar, +} + +pub struct DisplayByteVisualization { + format: ByteVisualization, + percentage: f32, +} + +impl Default for ByteVisualization { + fn default() -> Self { + ByteVisualization::PercentageAndBar + } +} + +impl ByteVisualization { + pub fn cycle(&mut self) { + use ByteVisualization::*; + *self = match self { + Bar => LongBar, + LongBar => PercentageAndBar, + PercentageAndBar => Percentage, + Percentage => Bar, + } + } + pub fn display(&self, percentage: f32) -> DisplayByteVisualization { + DisplayByteVisualization { + format: *self, + percentage, + } + } +} + +impl fmt::Display for DisplayByteVisualization { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use ByteVisualization::*; + let Self { format, percentage } = self; + + const BAR_SIZE: usize = 10; + match format { + Percentage => Self::make_percentage(f, percentage), + PercentageAndBar => { + Self::make_percentage(f, percentage)?; + f.write_str(" ")?; + Self::make_bar(f, percentage, BAR_SIZE) + } + Bar => Self::make_bar(f, percentage, BAR_SIZE), + LongBar => Self::make_bar(f, percentage, 20), + } + } +} + +impl DisplayByteVisualization { + fn make_bar(f: &mut fmt::Formatter, percentage: &f32, length: usize) -> Result<(), fmt::Error> { + let block_length = (length as f32 * percentage).round() as usize; + for _ in 0..block_length { + f.write_str(tui::symbols::block::FULL)?; + } + for _ in 0..length - block_length { + f.write_str(" ")?; + } + Ok(()) + } + fn make_percentage(f: &mut fmt::Formatter, percentage: &f32) -> Result<(), fmt::Error> { + write!(f, " {:>5.02}% ", percentage * 100.0) + } +} + +/// Options to configure how we display things +#[derive(Clone, Copy)] +pub struct DisplayOptions { + pub byte_format: ByteFormat, + pub byte_vis: ByteVisualization, +} + +impl From<WalkOptions> for DisplayOptions { + fn from(WalkOptions { byte_format, .. }: WalkOptions) -> Self { + DisplayOptions { + byte_format, + byte_vis: ByteVisualization::default(), + } + } +} diff --git a/src/interactive/app/eventloop.rs b/src/interactive/app/eventloop.rs index c05ca02..6bdf14b 100644 --- a/src/interactive/app/eventloop.rs +++ b/src/interactive/app/eventloop.rs @@ -1,10 +1,7 @@ -use crate::{ - interactive::{ - sorted_entries, - widgets::{DrawState, HelpPaneState, MainWindow}, - SortMode, - }, - ByteFormat, +use crate::interactive::{ + sorted_entries, + widgets::{DrawState, HelpPaneState, MainWindow}, + ByteVisualization, DisplayOptions, SortMode, }; use dua::{ path_of, @@ -14,98 +11,10 @@ use dua::{ use failure::Error; use itertools::Itertools; use petgraph::Direction; -use std::{fmt, io, path::PathBuf}; +use std::{io, path::PathBuf}; use termion::input::{Keys, TermReadEventsAndRaw}; use tui::{backend::Backend, widgets::Widget, Terminal}; -#[derive(Clone, Copy)] -pub enum ByteVisualization { - Percentage, - Bar, - LongBar, - PercentageAndBar, -} - -pub struct DisplayByteVisualization { - format: ByteVisualization, - percentage: f32, -} - -impl Default for ByteVisualization { - fn default() -> Self { - ByteVisualization::PercentageAndBar - } -} - -impl ByteVisualization { - pub fn cycle(&mut self) { - use ByteVisualization::*; - *self = match self { - Bar => LongBar, - LongBar => PercentageAndBar, - PercentageAndBar => Percentage, - Percentage => Bar, - } - } - pub fn display(&self, percentage: f32) -> DisplayByteVisualization { - DisplayByteVisualization { - format: *self, - percentage, - } - } -} - -impl fmt::Display for DisplayByteVisualization { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use ByteVisualization::*; - let Self { format, percentage } = self; - - const BAR_SIZE: usize = 10; - match format { - Percentage => Self::make_percentage(f, percentage), - PercentageAndBar => { - Self::make_percentage(f, percentage)?; - f.write_str(" ")?; - Self::make_bar(f, percentage, BAR_SIZE) - } - Bar => Self::make_bar(f, percentage, BAR_SIZE), - LongBar => Self::make_bar(f, percentage, 20), - } - } -} - -impl DisplayByteVisualization { - fn make_bar(f: &mut fmt::Formatter, percentage: &f32, length: usize) -> Result<(), fmt::Error> { - let block_length = (length as f32 * percentage).round() as usize; - for _ in 0..block_length { - f.write_str(tui::symbols::block::FULL)?; - } - for _ in 0..length - block_length { - f.write_str(" ")?; - } - Ok(()) - } - fn make_percentage(f: &mut fmt::Formatter, percentage: &f32) -> Result<(), fmt::Error> { - write!(f, " {:>5.02}% ", percentage * 100.0) - } -} - -/// Options to configure how we display things -#[derive(Clone, Copy)] -pub struct DisplayOptions { - pub byte_format: ByteFormat, - pub byte_vis: ByteVisualization, -} - -impl From<WalkOptions> for DisplayOptions { - fn from(WalkOptions { byte_format, .. }: WalkOptions) -> Self { - DisplayOptions { - byte_format, - byte_vis: ByteVisualization::default(), - } - } -} - #[derive(Copy, Clone)] pub enum FocussedPane { Main, diff --git a/src/interactive/app/mod.rs b/src/interactive/app/mod.rs index 912120e..cc8ecd0 100644 --- a/src/interactive/app/mod.rs +++ b/src/interactive/app/mod.rs @@ -1,5 +1,7 @@ +mod bytevis; mod common; mod eventloop; +pub use bytevis::*; pub use common::*; pub use eventloop::*; |