diff --git a/src/rustfmt.toml b/rustfmt.toml similarity index 100% rename from src/rustfmt.toml rename to rustfmt.toml diff --git a/src/border.rs b/src/border.rs deleted file mode 100644 index 01683fe..0000000 --- a/src/border.rs +++ /dev/null @@ -1,91 +0,0 @@ -use clap::ArgEnum; -use prettytable::format::{self, FormatBuilder, LinePosition, LineSeparator}; - -#[derive(Debug, Copy, Clone, ArgEnum)] -pub enum Border { - None, - Ascii, - AsciiGrid, - Sharp, - DoubleSharp, - Rounded, - Reinforced, - Markdown, - Grid, -} - -impl From for format::TableFormat { - fn from(style: Border) -> Self { - match style { - Border::None => *format::consts::FORMAT_CLEAN, - Border::Ascii => *format::consts::FORMAT_NO_LINESEP_WITH_TITLE, - Border::AsciiGrid => *format::consts::FORMAT_DEFAULT, - Border::Sharp => FormatBuilder::new() - .column_separator('│') - .borders('│') - .separators(&[LinePosition::Top], LineSeparator::new('─', '┬', '┌', '┐')) - .separators( - &[LinePosition::Title], - LineSeparator::new('─', '┼', '├', '┤'), - ) - .separators( - &[LinePosition::Bottom], - LineSeparator::new('─', '┴', '└', '┘'), - ) - .padding(1, 1) - .build(), - Border::DoubleSharp => FormatBuilder::new() - .column_separator('║') - .borders('║') - .separators(&[LinePosition::Top], LineSeparator::new('═', '╦', '╔', '╗')) - .separators( - &[LinePosition::Title], - LineSeparator::new('═', '╠', '├', '╣'), - ) - .separators( - &[LinePosition::Bottom], - LineSeparator::new('═', '╩', '╚', '╝'), - ) - .padding(1, 1) - .build(), - Border::Rounded => FormatBuilder::new() - .column_separator('│') - .borders('│') - .separators(&[LinePosition::Top], LineSeparator::new('─', '┬', '╭', '╮')) - .separators( - &[LinePosition::Title], - LineSeparator::new('─', '┼', '├', '┤'), - ) - .separators( - &[LinePosition::Bottom], - LineSeparator::new('─', '┴', '╰', '╯'), - ) - .padding(1, 1) - .build(), - Border::Reinforced => FormatBuilder::new() - .column_separator('│') - .borders('│') - .separators(&[LinePosition::Top], LineSeparator::new('─', '┬', '┏', '┓')) - .separators( - &[LinePosition::Title], - LineSeparator::new('─', '┼', '├', '┤'), - ) - .separators( - &[LinePosition::Bottom], - LineSeparator::new('─', '┴', '┗', '┛'), - ) - .padding(1, 1) - .build(), - Border::Markdown => FormatBuilder::new() - .column_separator('|') - .borders('|') - .separators( - &[LinePosition::Title], - LineSeparator::new('-', '|', '|', '|'), - ) - .padding(1, 1) - .build(), - Border::Grid => *format::consts::FORMAT_BOX_CHARS, - } - } -} diff --git a/src/input.rs b/src/input.rs deleted file mode 100644 index 192b837..0000000 --- a/src/input.rs +++ /dev/null @@ -1,27 +0,0 @@ -//! Only compiled when the feature `readline` is not enabled - -pub fn input(msg: &str) -> Result { - let mut s = String::new(); - print!("{}", msg); - stdout() - .flush() - .context("Failed to flush stdout to allow input")?; - stdin() - .read_line(&mut s) - .context("Failed to get input from user")?; - Ok(s) -} - -pub fn read_parse_loop(prompt: &str) -> Result -where - T: FromStr, - E: fmt::Display, -{ - loop { - let s = input(prompt)?; - match s.parse() { - Ok(t) => break Ok(t), - Err(e) => print::err_display(e), - } - } -} diff --git a/src/range_syntax.rs b/src/range_syntax.rs index 267a40d..16b7158 100644 --- a/src/range_syntax.rs +++ b/src/range_syntax.rs @@ -69,100 +69,5 @@ mod tests { parse_fail("..1234"); parse_fail(".1234"); parse_fail(""); - // parse_fail("1234"); } - - // #[should_panic] - // #[test] - // fn missing_beginning_test() { - // "-123434".parse::().unwrap(); - // } - - // #[should_panic] - // #[test] - // fn too_many_dashes_test() { - // "123---1234".parse::().unwrap(); - // } - - // #[should_panic] - // #[test] - // fn not_a_number_test() { - // "hello".parse::().unwrap(); - // } - - // #[should_panic] - // #[test] - // fn parse_nothing_test() { - // "".parse::().unwrap(); - // } - - // #[test] - // fn is_overlapping_same_range_test() { - // assert!((1..1).is_overlapping(&(1..1))); - // } - - // #[test] - // fn is_overlapping2_range_test() { - // assert!((1..10).is_overlapping(&(1..4))); - // } - - // #[test] - // fn is_not_overlapping_range_test() { - // assert!(!(1..4).is_overlapping(&(10..1234))); - // } - - // #[test] - // fn is_overlapping_test() { - // assert!(Range(1..3).is_overlapping(&Range(1..3))); - // } - - // #[test] - // fn is_overlapping2_test() { - // assert!(Range(1..9).is_overlapping(&Range(3..6))); - // } - - // #[test] - // fn is_not_overlapping_test() { - // assert!(!Range(1..3).is_overlapping(&Range(5..10))); - // } - - // #[test] - // fn is_overlapping_same_test() { - // assert!(Range(1..1).is_overlapping(&Range(1..1))); - // } - - // #[test] - // fn is_overlapping_different_test() { - // assert!(Point(5).is_overlapping(&(Range(1..15)))); - // } - - // #[test] - // fn is_overlapping_different2_test() { - // assert!(Range(3..8).is_overlapping(&Point(4))); - // } - - // #[test] - // fn is_overlapping_points_test() { - // assert!(Point(4).is_overlapping(&Point(4))); - // } - - // #[test] - // fn get_multiple_test() { - // assert_eq!( - // "4 40 3 9-12".parse::().unwrap().0, - // vec![Point(3), Point(39), Point(2), Range(8..12),] - // ); - // } - - // #[should_panic] - // #[test] - // fn get_multiple_overlapping_test() { - // "4 30 5-13 7-8 9".parse::().unwrap(); - // } - - // #[should_panic] - // #[test] - // fn get_multiple_none() { - // "".parse::().unwrap(); - // } } diff --git a/src/table.rs b/src/table.rs deleted file mode 100644 index 4a31298..0000000 --- a/src/table.rs +++ /dev/null @@ -1,210 +0,0 @@ -use std::borrow::Cow; - -use clap::{ArgEnum, Clap}; -use eyre::{eyre, Result}; -use log::{debug, trace}; -use prettytable::{cell, row, Cell, Row, Table}; -use terminal_size::{terminal_size, Width}; - -use crate::border::Border; -use crate::utils::{date, get_metadata, path, Pair}; - -pub struct SizedTable { - opt: Opt, - table: Table, -} - -impl SizedTable { - pub fn new(opt: Opt) -> Result { - let title_row = if !opt.no_title { - Some(opt.size.get_title_row()) - } else { - None - }; - - let table = create_table(title_row, opt.border); - let sized_table = SizedTable { opt, table }; - Ok(sized_table) - } - - pub fn add_row(&mut self, pair: &Pair) -> Result<()> { - let row = self.get_row(pair)?; - self.table.add_row(row); - Ok(()) - } - - fn get_row(&self, pair: &Pair) -> Result { - let Pair(ref trash_entry, ref trash_info) = pair; - - let path = trash_info.percent_path(); - trace!("Path before decoded: {}", path); - let path = path.decoded()?; - trace!("After decoded path: {}", path); - - let mut displayed_path = if !self.opt.absolute { - let path = path.as_ref(); - Cow::from(path::shorten(path).unwrap()) - } else { - path - }; - trace!("After displayed path (shorten stuff): {}", displayed_path); - - displayed_path = if !self.opt.dont_colorize { - let metadata = get_metadata(&trash_entry)?; - Cow::from(path::colorize(displayed_path.as_ref(), &metadata)) - } else { - displayed_path - }; - trace!("After colorized path: {}", displayed_path); - - trace!("Add adding size {:?} row", self.opt.size); - let row = match self.opt.size { - TableSize::Minimal => row![displayed_path], - TableSize::Compact => { - let mut res = date::format(trash_info.deletion_date()); - res.push(Cell::new(&displayed_path)); - Row::new(res) - } - TableSize::Full => { - let mut res = date::format(trash_info.deletion_date()); - res.push(Cell::new(&displayed_path)); - Row::new(res) - } - }; - Ok(row) - } - - pub fn print(&self) -> usize { - self.table.printstd() - } -} - -pub struct IndexedTable(SizedTable); - -impl IndexedTable { - pub fn new(opt: Opt) -> Result { - let title_row = if !opt.no_title { - Some(opt.size.get_title_row_index()) - } else { - None - }; - let table = create_table(title_row, opt.border); - Ok(IndexedTable(SizedTable { opt, table })) - } - - pub fn add_row(&mut self, pair: &Pair) -> Result<()> { - let mut row = self.0.get_row(pair)?; - // insert the index - let index = self.0.table.len() + 1; - debug!("current index (1 based): {}", index); - row.insert_cell(0, cell!(index)); - self.0.table.add_row(row); - Ok(()) - } - - pub fn print(&self) -> usize { - self.0.print() - } -} - -fn get_terminal_width() -> Result { - let width = terminal_size() - .ok_or_else(|| eyre!("Unable to get terminal size"))? - .0; - Ok(width) -} - -#[derive(Debug, Copy, Clone, ArgEnum)] -pub enum TableSize { - /// only displays path - Minimal, - - /// displays all informations in compact size - Compact, - - /// displays all information fully - Full, -} - -impl From for TableSize { - fn from(value: Width) -> TableSize { - let Width(w) = value; - match w { - 0..=45 => TableSize::Minimal, - 46..=90 => TableSize::Compact, - _ => TableSize::Full, - } - } -} - -impl TableSize { - fn try_new() -> Result { - Ok(get_terminal_width()?.into()) - } - - fn get_title_row(self) -> Row { - match self { - TableSize::Minimal => row!["Path"], - TableSize::Compact => row!["Month", "Date", "Time", "Path"], - TableSize::Full => row!["Month", "Day", "Time", "Path"], - } - } - - fn get_title_row_index(self) -> Row { - let mut row = self.get_title_row(); - row.insert_cell(0, Cell::new("Index")); - row - } - - fn create_table(self, border: Border) -> Table { - let mut table = Table::new(); - table.set_titles(self.get_title_row()); - table.set_format(border.into()); - table - } -} - -fn create_table(title_row: Option, border: Border) -> Table { - let mut table = Table::new(); - if let Some(title) = title_row { - table.set_titles(title); - } - table.set_format(border.into()); - table -} - -#[derive(Clap, Debug)] -pub struct Opt { - /// The border to use - #[clap( - arg_enum, - short, - long, - default_value = "Sharp", - case_insensitive = true, - env = "TRASHY_BORDER" - )] - border: Border, - - /// The size of the table to use - #[clap( - arg_enum, - default_value = "Full", - case_insensitive = true, - short, - long, - env = "TRASHY_SIZE" - )] - size: TableSize, - - /// Wheather to colorize output - #[clap(short = 'c', long)] - dont_colorize: bool, - - /// Wheater to get the absolute path instead of shortening the output - #[clap(short, long)] - absolute: bool, - - #[clap(short = 't', long, env = "TRASHY_NO_TITLE")] - no_title: bool, -}