Skip to content

Commit

Permalink
TTY style disabling
Browse files Browse the repository at this point in the history
either with `--color no` or when output is piped
  • Loading branch information
Canop committed Jul 1, 2021
1 parent eb407eb commit 85e763e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
54 changes: 54 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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)),
}
}
}
20 changes: 14 additions & 6 deletions src/fmt_mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(())
}
}
38 changes: 11 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 85e763e

Please sign in to comment.