diff options
author | Canop <cano.petrole@gmail.com> | 2021-07-01 06:33:40 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-07-01 06:33:40 +0200 |
commit | 85e763eece12269733409ff94d805527cebb4d44 (patch) | |
tree | 8f9cd6bdbb89652f8b9c812e17c3e1f4e2ba6dd2 | |
parent | eb407eb95042d93a1baa1dabf384600ae9b3e25d (diff) |
TTY style disablingv0.7.4
either with `--color no` or when output is piped
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/args.rs | 54 | ||||
-rw-r--r-- | src/fmt_mount.rs | 20 | ||||
-rw-r--r-- | src/main.rs | 38 |
6 files changed, 86 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2599000..a9e19f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +<a name="v0.7.4"></a> +### v0.7.4 - 2021/07/01 +* `--color` option with values yes|no|auto (auto being default) +* no tty style when `--color` is default and the output is piped + <a name="v0.7.3"></a> ### v0.7.3 - 2021/06/30 * fix disk not found for BTRFS filesystems - Fix #11 @@ -212,7 +212,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "lfs" -version = "0.7.3" +version = "0.7.4" dependencies = [ "argh", "crossterm", @@ -1,6 +1,6 @@ [package] name = "lfs" -version = "0.7.3" +version = "0.7.4" authors = ["dystroy <denys.seguret@gmail.com>"] edition = "2018" keywords = ["linux", "filesystem", "fs"] diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..6828fac --- /dev/null +++ b/src/args.rs @@ -0,0 +1,54 @@ + +use { + argh::FromArgs, + std::path::PathBuf, +}; + +#[derive(FromArgs)] +/// List your filesystems. +/// +/// All units are SI. +/// +/// Source at https://github.com/Canop/lfs +pub struct Args { + /// print the version + #[argh(switch, short = 'v')] + pub version: bool, + + #[argh(option, default = "Default::default()")] + /// color: 'yes', 'no' or 'auto' (auto should be good in most cases) + pub color: BoolArg, + + /// whether to show all mount points + #[argh(switch, short = 'a')] + pub all: bool, + + /// output as JSON + #[argh(switch, short = 'j')] + pub json: bool, + + #[argh(positional)] + /// if a path is provided, only the device holding this path will be shown + pub path: Option<PathBuf>, +} + +/// An optional boolean for use in Argh +#[derive(Debug, Clone, Copy, Default)] +pub struct BoolArg(Option<bool>); + +impl BoolArg { + pub fn value(self) -> Option<bool> { + self.0 + } +} + +impl argh::FromArgValue for BoolArg { + fn from_arg_value(value: &str) -> Result<Self, String> { + match value.to_lowercase().as_ref() { + "auto" => Ok(BoolArg(None)), + "yes" => Ok(BoolArg(Some(true))), + "no" => Ok(BoolArg(Some(false))), + _ => Err(format!("Illegal value: {:?}", value)), + } + } +} diff --git a/src/fmt_mount.rs b/src/fmt_mount.rs index 9f3a029..4ed47bc 100644 --- a/src/fmt_mount.rs +++ b/src/fmt_mount.rs @@ -25,7 +25,7 @@ ${mount-points |-: "#; -pub fn print(mounts: &[Mount]) -> Result<()> { +pub fn print(mounts: &[Mount], color: bool) -> Result<()> { let mut expander = OwningTemplateExpander::new(); expander.set_default(""); for mount in mounts { @@ -51,14 +51,22 @@ pub fn print(mounts: &[Mount]) -> Result<()> { let (width, _) = terminal_size(); let template = TextTemplate::from(MD); let text = expander.expand(&template); - let skin = MadSkin { + let skin = if color { + make_colored_skin() + } else { + MadSkin::no_style() + }; + let fmt_text = FmtText::from_text(&skin, text, Some(width as usize)); + print!("{}", fmt_text); + Ok(()) +} + +fn make_colored_skin() -> MadSkin { + MadSkin { bold: CompoundStyle::with_fg(AnsiValue(SIZE_COLOR)), // size inline_code: CompoundStyle::with_fg(AnsiValue(USED_COLOR)), // use% strikeout: CompoundStyle::with_fgbg(AnsiValue(USED_COLOR), AnsiValue(AVAI_COLOR)), // use bar italic: CompoundStyle::with_fg(AnsiValue(AVAI_COLOR)), // available ..Default::default() - }; - let fmt_text = FmtText::from_text(&skin, text, Some(width as usize)); - print!("{}", fmt_text); - Ok(()) + } } diff --git a/src/main.rs b/src/main.rs index 577e77f..76d3d66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,17 @@ +mod args; mod fmt_mount; mod json; use { - argh::FromArgs, - std::{cmp::Reverse, fs, os::unix::fs::MetadataExt, path::PathBuf}, + crate::args::*, + crossterm::tty::IsTty, + std::{ + cmp::Reverse, + fs, + os::unix::fs::MetadataExt, + }, }; -#[derive(FromArgs)] -/// List your filesystems. -/// -/// All units are SI. -/// -/// Source at https://github.com/Canop/lfs -struct Args { - /// print the version - #[argh(switch, short = 'v')] - version: bool, - - /// whether to show all mount points - #[argh(switch, short = 'a')] - all: bool, - - /// output as JSON - #[argh(switch, short = 'j')] - json: bool, - - #[argh(positional)] - /// if a path is provided, only the device holding this path will be shown - pub path: Option<PathBuf>, -} - fn main() -> lfs_core::Result<()> { let args: Args = argh::from_env(); if args.version { @@ -59,6 +41,8 @@ fn main() -> lfs_core::Result<()> { Ok(()) } else { mounts.sort_by_key(|m| Reverse(m.size())); - fmt_mount::print(&mounts) + let color = args.color.value() + .unwrap_or_else(|| std::io::stdout().is_tty()); + fmt_mount::print(&mounts, color) } } |