Skip to content

Commit

Permalink
feat: improve styling, and disable if not interactive
Browse files Browse the repository at this point in the history
This makes the headers more obvious. It also drops styling so the
output isn't cluttered with color codes if we are not in an interactive
terminal (or `NO_COLOR` is set).

Ref: https://docs.rs/yansi/latest/yansi/fn.whenever.html
  • Loading branch information
lukehsiao committed Apr 19, 2024
1 parent c027dce commit d5a6209
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
20 changes: 20 additions & 0 deletions 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
Expand Up @@ -18,4 +18,4 @@ clap = { version = "4.5.4", features = ["wrap_help", "derive"] }
rayon = "1.10.0"
tabled = { version = "0.15.0", features = ["ansi"] }
xshell = "0.2.6"
yansi = "1.0.1"
yansi = { version = "1.0.1", features = ["detect-tty", "detect-env"] }
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tabled::{
},
Table, Tabled,
};
use yansi::Paint;
use yansi::{Condition, Paint};

use xshell::{cmd, Shell};

Expand Down Expand Up @@ -85,6 +85,18 @@ fn main() -> Result<()> {
let cli = Cli::parse();
let sh = Shell::new()?;

yansi::whenever(Condition::STDOUT_IS_TTY);

// On each styling, check if we have TTYs.
yansi::whenever(Condition::STDOUTERR_ARE_TTY_LIVE);
// Check `NO_COLOR`, `CLICOLOR`, and if we have TTYs.
const HAVE_COLOR: Condition = Condition(|| {
std::env::var_os("NO_COLOR").is_none()
&& (Condition::CLICOLOR_LIVE)()
&& Condition::stdouterr_are_tty_live()
});
yansi::whenever(Condition::cached((HAVE_COLOR)()));

let rev_range = cli.rev_range;
let raw_shortlog = if cli.email {
cmd!(sh, "git shortlog -sen {rev_range}").read()?
Expand Down Expand Up @@ -169,6 +181,10 @@ fn main() -> Result<()> {
.with(Style::empty())
.modify(Columns::new(1..=5), Alignment::right())
.modify(Rows::last(), Alignment::right())
.modify(
Rows::first(),
Format::content(|s| s.bold().underline().to_string()),
)
.modify(Rows::last(), Format::content(|s| s.bold().to_string()));

println!("{table}");
Expand Down

0 comments on commit d5a6209

Please sign in to comment.