A cross-platform library for colorizing paths according to the LS_COLORS
environment variable (like ls
).
use lscolors::{LsColors, Style};
let lscolors = LsColors::from_env().unwrap_or_default();
let path = "some/folder/test.tar.gz";
let style = lscolors.style_for_path(path);
// If you want to use `ansi_term`:
let ansi_style = style.map(Style::to_ansi_term_style)
.unwrap_or_default();
println!("{}", ansi_style.paint(path));
// If you want to use `nu-ansi-term` (fork of ansi_term) or `gnu_legacy`:
let nu_ansi_style = style.map(Style::to_nu_ansi_term_style)
.unwrap_or_default();
println!("{}", nu_ansi_style.paint(path));
// If you want to use `crossterm`:
let crossterm_style = style.map(Style::to_crossterm_style)
.unwrap_or_default();
println!("{}", crossterm_style.apply(path));
This crate also comes with a small command-line program lscolors
that
can be used to colorize the output of other commands:
> find . -maxdepth 2 | lscolors
> rg foo -l | lscolors
You can install it by running cargo install lscolors
or by downloading one
of the prebuilt binaries from the release page.
If you want to build the application from source, you can run
cargo build --release --features=nu-ansi-term --locked
// Cargo.toml
[dependencies]
// use ansi-term coloring
lscolors = { version = "v0.14.0", features = ["ansi_term"] }
// use crossterm coloring
lscolors = { version = "v0.14.0", features = ["crossterm"] }
// use nu-ansi-term coloring
lscolors = { version = "v0.14.0", features = ["nu-ansi-term"] }
// use nu-ansi-term coloring in gnu legacy mode with double digit styles
lscolors = { version = "v0.14.0", features = ["gnu_legacy"] }
Licensed under either of
at your option.
Information about the LS_COLORS
environment variable is sparse. Here is a short list of useful references:
LS_COLORS
implementation in the GNU coreutils version of ls
(the reference implementation)LS_COLORS
implementation in bfs
by @tavianatorDIR_COLORS(5)
man page