diff options
author | Canop <cano.petrole@gmail.com> | 2022-02-25 17:05:23 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2022-02-25 17:05:23 +0100 |
commit | 0f28211ae13e98789488eeeab6e449f3b6fb4a91 (patch) | |
tree | d7b57526ca6e72ecc731c1446ec2bba4ffaa421f | |
parent | 625ddc8211a3d2f7d2a310b2ef4501e11084ede8 (diff) |
new launch argument: `--list-cols`
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/args.rs | 11 | ||||
-rw-r--r-- | src/col.rs | 87 | ||||
-rw-r--r-- | src/list_cols.rs | 45 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | website/docs/table.md | 3 |
8 files changed, 128 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 00323a6..b826ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### next +- `--list-cols` launch argument + <a name="v2.1.0"></a> ### v2.1.0 - 2022/02/23 - fix failure in parsing `--cols` arguments with underscore @@ -243,7 +243,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "lfs" -version = "2.1.0" +version = "2.1.1-dev" dependencies = [ "argh", "crossterm", @@ -1,6 +1,6 @@ [package] name = "lfs" -version = "2.1.0" +version = "2.1.1-dev" authors = ["dystroy <denys.seguret@gmail.com>"] edition = "2021" keywords = ["linux", "filesystem", "fs"] diff --git a/src/args.rs b/src/args.rs index 98c28f3..8195815 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,6 +3,7 @@ use { cols::Cols, units::Units, }, + crossterm::tty::IsTty, argh::FromArgs, std::path::PathBuf, }; @@ -24,6 +25,10 @@ pub struct Args { #[argh(switch, short = 'a')] pub all: bool, + /// list the available column names + #[argh(switch)] + pub list_cols: bool, + /// columns, eg `-c +inodes` or `-c id+dev+default` #[argh(option, default = "Default::default()", short = 'c')] pub cols: Cols, @@ -71,3 +76,9 @@ impl argh::FromArgValue for Units { } } } +impl Args { + pub fn color(&self) -> bool { + self.color.value() + .unwrap_or_else(|| std::io::stdout().is_tty()) + } +} @@ -7,7 +7,10 @@ use { }; macro_rules! col_enum { - ($($variant:ident $name:literal $($alias:literal)* : $title:literal ,)*) => { + (@just_variant $variant:ident $discarded:ident) => { + Col::$variant + }; + ($($variant:ident $name:literal $($alias:literal)* : $title:literal $($def:ident)*,)*) => { /// A column of the lfs table. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Col { @@ -16,6 +19,11 @@ macro_rules! col_enum { pub static ALL_COLS: &[Col] = &[ $(Col::$variant,)* ]; + pub static DEFAULT_COLS: &[Col] = &[ + $( + $(col_enum!(@just_variant $variant $def),)* + )* + ]; impl FromStr for Col { type Err = ParseColError; fn from_str(s: &str) -> Result<Self, ParseColError> { @@ -54,44 +62,44 @@ macro_rules! col_enum { )* } } + pub fn aliases(self) -> &'static [&'static str] { + match self { + $( + Self::$variant => &[$($alias,)*], + )* + } + } + pub fn is_default(self) -> bool { + DEFAULT_COLS.contains(&self) + } } - } + }; } // definition of all columns and their names // in the --cols definition col_enum!( - // syntax: Variant name [alias] : title + // syntax: + // Variant name [aliases]: title [default] Id "id": "id", - Dev "dev": "dev", - Filesystem "fs" "filesystem": "filesystem", + Dev "dev" "device" "device_id": "dev", + Filesystem "fs" "filesystem": "filesystem" default, Label "label": "label", - Type "type": "type", - Disk "disk": "disk", - Used "used": "used", - Use "use": "use%", + Type "type": "type" default, + Disk "disk" "dsk": "disk" default, + Used "used": "used" default, + Use "use": "use%" default, UsePercent "use_percent": "use%", - Free "free": "free", - Size "size": "size", - InodesUsed "inodes_used": "used inodes", - InodesUse "inodes" "inodes_use": "inodes%", - InodesUsePercent "inodes_use_percent": "inodes%", - InodesFree "inodes_free": "free inodes", - InodesCount "inodes_total" "inodes_count": "inodes total", - MountPoint "mount" "mount_point": "mount point", + Free "free": "free" default, + Size "size": "size" default, + InodesUsed "inodes_used" "iused": "used inodes", + InodesUse "inodes" "ino" "inodes_use" "iuse": "inodes%", + InodesUsePercent "inodes_use_percent" "iuse_percent": "inodes%", + InodesFree "inodes_free" "ifree": "free inodes", + InodesCount "inodes_total" "inodes_count" "itotal": "inodes total", + MountPoint "mount" "mount_point" "mp": "mount point" default, ); -pub static DEFAULT_COLS: &[Col] = &[ - Col::Filesystem, - Col::Type, - Col::Disk, - Col::Used, - Col::Use, - Col::Free, - Col::Size, - Col::MountPoint, -]; - impl Col { pub fn header_align(self) -> Alignment { match self { @@ -106,8 +114,8 @@ impl Col { Self::Dev => Alignment::Center, Self::Filesystem => Alignment::Left, Self::Label => Alignment::Left, - Self::Disk => Alignment::Center, Self::Type => Alignment::Center, + Self::Disk => Alignment::Center, Self::Used => Alignment::Right, Self::Use => Alignment::Right, Self::UsePercent => Alignment::Right, @@ -121,6 +129,27 @@ impl Col { Self::MountPoint => Alignment::Left, } } + pub fn description(self) -> &'static str { + match self { + Self::Id => "mount point id", + Self::Dev => "device id", + Self::Filesystem => "filesystem", + Self::Label => "volume label", + Self::Type => "filesystem type", + Self::Disk => "storage type", + Self::Used => "size used", + Self::Use => "usage graphical view", + Self::UsePercent => "percentage of blocks used", + Self::Free => "free bytes", + Self::Size => "total size", + Self::InodesUsed => "number of inodes used", + Self::InodesUse => "graphical view of inodes usage", + Self::InodesUsePercent => "percentage of inodes used", + Self::InodesFree => "number of free inodes", + Self::InodesCount => "total count of inodes", + Self::MountPoint => "mount point", + } + } } diff --git a/src/list_cols.rs b/src/list_cols.rs new file mode 100644 index 0000000..542a6a4 --- /dev/null +++ b/src/list_cols.rs @@ -0,0 +1,45 @@ +use { + crate::col::ALL_COLS, + termimad::{ + minimad::OwningTemplateExpander, + MadSkin, + }, +}; + +static MD: &str = r#" +The `--cols` launch argument lets you specify the columns of the **lfs** table. + +You can give the explicit list of all columns: `lfs -c dev+fs` + +You can add columns to the default ones: `lfs -c +dev+size` + +Complete syntax at https://dystroy.org/lfs/table + +|:-:|:-:|:-:|:- +|column | aliases | default | content +|:-:|:-:|:-:|- +${column +|${name}|${aliases}|${default}|${description} +} +|- +"#; + +/// Print an help text describing columns +pub fn print(color: bool) { + let mut expander = OwningTemplateExpander::new(); + expander.set_default(""); + for &col in ALL_COLS { + expander.sub("column") + .set("name", col.name()) + .set("aliases", col.aliases().join(", ")) + .set("default", if col.is_default() { "x" } else { "" }) + .set("description", col.description()); + } + let skin = if color { + MadSkin::default() + } else { + MadSkin::no_style() + }; + skin.print_owning_expander_md(&expander, MD); +} + diff --git a/src/main.rs b/src/main.rs index 554f049..fd0eb9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,12 @@ mod args; mod col; mod cols; mod json; +mod list_cols; mod table; mod units; use { crate::args::*, - crossterm::tty::IsTty, std::{ cmp::Reverse, fs, @@ -21,6 +21,10 @@ fn main() { println!("lfs {}", env!("CARGO_PKG_VERSION")); return; } + if args.list_cols { + list_cols::print(args.color()); + return; + } let mut mounts = match lfs_core::read_mounts() { Ok(mounts) => mounts, Err(e) => { @@ -60,9 +64,7 @@ fn main() { println!("no disk was found - try\n lfs -a"); } else { mounts.sort_by_key(|m| Reverse(m.size())); - let color = args.color.value() - .unwrap_or_else(|| std::io::stdout().is_tty()); - table::print(&mounts, color, &args); + table::print(&mounts, args.color(), &args); } } diff --git a/website/docs/table.md b/website/docs/table.md index 5cefa65..a29a02a 100644 --- a/website/docs/table.md +++ b/website/docs/table.md @@ -4,6 +4,8 @@ You can modify it easily. # Columns +You can run `lfs --list-cols` for the list of all columns. + ## All columns name | default | meaning @@ -26,6 +28,7 @@ inodes_use_percent | | inodes use share, in percents inodescount | | total number of inodes in the filesystem mount | ✓ | mounting path + ## --cols argument With the `--cols` launch argument, shortened as `-c`, you can change the displayed columns or their order. |