From 0e83c15906d33c0eaaabf8b087ddc9b68435392f Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 2 Nov 2020 20:20:53 -0600 Subject: [PATCH] Print file list in a table --- Cargo.lock | 210 ++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/lib.rs | 1 + src/list.rs | 32 +++++++ src/ops/list.rs | 12 +-- src/ops/restore.rs | 13 +-- 6 files changed, 247 insertions(+), 22 deletions(-) create mode 100644 src/list.rs diff --git a/Cargo.lock b/Cargo.lock index eac9d19..cfd54de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,18 @@ version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1fd36ffbb1fb7c834eac128ea8d0e310c5aeb635548f9d58861e1308d46e71c" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" + [[package]] name = "atty" version = "0.2.14" @@ -32,12 +44,47 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + +[[package]] +name = "bstr" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "473fc6b38233f9af7baa94fb5852dca389e3d95b8e21c8e3719301462c5d9faf" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + [[package]] name = "cc" version = "1.0.61" @@ -87,6 +134,62 @@ dependencies = [ "vec_map", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "csv" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00affe7f6ab566df61b4be3ce8cf16bc2576bca0963ceb0955e45d514bf9a279" +dependencies = [ + "bstr", + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + +[[package]] +name = "dirs" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "garbage" version = "0.2.2" @@ -99,12 +202,24 @@ dependencies = [ "libmount", "log", "percent-encoding", + "prettytable-rs", "structopt", "thiserror", "walkdir", "xdg", ] +[[package]] +name = "getrandom" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "heck" version = "0.3.1" @@ -123,6 +238,12 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + [[package]] name = "lazy_static" version = "1.4.0" @@ -155,6 +276,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + [[package]] name = "nix" version = "0.14.1" @@ -193,6 +320,20 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "prettytable-rs" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fd04b170004fa2daccf418a7f8253aaf033c27760b5f225889024cf66d7ac2e" +dependencies = [ + "atty", + "csv", + "encode_unicode", + "lazy_static", + "term", + "unicode-width", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -241,6 +382,50 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "redox_users" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", +] + +[[package]] +name = "rust-argon2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dab61250775933275e84053ac235621dfb739556d5c54a2f2e9313b7cf43a19" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "same-file" version = "1.0.6" @@ -250,6 +435,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" + [[package]] name = "strsim" version = "0.8.0" @@ -291,6 +482,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "term" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" +dependencies = [ + "byteorder", + "dirs", + "winapi", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -327,7 +529,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] @@ -378,6 +580,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index c4489fa..d4b129b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ libc = "0.2" libmount = "0.1" log = "0.4" percent-encoding = "2.1" +prettytable-rs = "0.8.0" structopt = "0.3" thiserror = "1.0" walkdir = "2.3" diff --git a/src/lib.rs b/src/lib.rs index 2890f61..2d057b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ mod utils; mod dir; mod errors; mod info; +mod list; mod mounts; pub mod ops; diff --git a/src/list.rs b/src/list.rs new file mode 100644 index 0000000..cfa08b3 --- /dev/null +++ b/src/list.rs @@ -0,0 +1,32 @@ +use std::ops::Deref; + +use chrono_humanize::HumanTime; +use prettytable::{cell, format::consts::FORMAT_CLEAN, Row, Table}; + +use crate::utils; +use crate::TrashInfo; + +pub fn print_files_list(files: F, with_index: bool) +where + I: Deref, + F: Iterator, +{ + let mut table = Table::new(); + table.set_format(*FORMAT_CLEAN); + + for (i, info) in files.enumerate() { + let mut row = Row::empty(); + + if with_index { + row.add_cell(cell!(format!("[{}]", i))); + } + + row.add_cell(cell!(info.deletion_date)); + row.add_cell(cell!(HumanTime::from(info.deletion_date))); + row.add_cell(cell!(utils::percent_encode(&info.path))); + + table.add_row(row); + } + + table.printstd(); +} diff --git a/src/ops/list.rs b/src/ops/list.rs index 885db4f..b04ab6b 100644 --- a/src/ops/list.rs +++ b/src/ops/list.rs @@ -1,9 +1,8 @@ use std::path::PathBuf; use anyhow::Result; -use chrono_humanize::HumanTime; -use crate::utils; +use crate::list; use crate::TrashDir; /// Options to pass to list @@ -32,14 +31,7 @@ pub fn list(options: ListOptions) -> Result<()> { .collect::>(); files.sort_unstable_by_key(|info| info.deletion_date); - for info in files { - println!( - "{}\t{}\t{}", - info.deletion_date, - HumanTime::from(info.deletion_date), - utils::percent_encode(info.path) - ); - } + list::print_files_list(files.iter(), false); Ok(()) } diff --git a/src/ops/restore.rs b/src/ops/restore.rs index bd6977a..4bb21f7 100644 --- a/src/ops/restore.rs +++ b/src/ops/restore.rs @@ -4,9 +4,8 @@ use std::io; use std::path::PathBuf; use anyhow::Result; -use chrono_humanize::HumanTime; -use crate::utils; +use crate::list; use crate::TrashDir; /// Options to pass to restore @@ -59,15 +58,7 @@ pub fn restore(options: RestoreOptions) -> Result<()> { bail!("No files in this trash directory."); } - for (i, info) in files.iter().enumerate() { - println!( - "[{}]\t{}\t{}\t{}", - i, - info.deletion_date, - HumanTime::from(info.deletion_date), - utils::percent_encode(&info.path) - ); - } + list::print_files_list(files.iter(), true); let stdin = io::stdin(); let mut s = String::new();