24 releases (breaking)

0.20.0 Oct 1, 2024
0.18.0 Jun 23, 2024
0.17.0 Jan 24, 2024
0.16.0 Nov 29, 2023
0.3.0 Dec 15, 2018

#23 in Command-line interface

Download history 15751/week @ 2024-08-20 15187/week @ 2024-08-27 11294/week @ 2024-09-03 13485/week @ 2024-09-10 11058/week @ 2024-09-17 10688/week @ 2024-09-24 9977/week @ 2024-10-01 9337/week @ 2024-10-08 12345/week @ 2024-10-15 9885/week @ 2024-10-22 9683/week @ 2024-10-29 8321/week @ 2024-11-05 12275/week @ 2024-11-12 10637/week @ 2024-11-19 14053/week @ 2024-11-26 9822/week @ 2024-12-03

48,077 downloads per month
Used in 123 crates (22 directly)

MIT/Apache

81KB
1.5K SLoC

lscolors

CICD Crates.io Documentation

A cross-platform library for colorizing paths according to the LS_COLORS environment variable (like ls).

Usage

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));

Command-line application

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

Features

// 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"] }

License

Licensed under either of

at your option.

References

Information about the LS_COLORS environment variable is sparse. Here is a short list of useful references:

Dependencies

~0.6–9.5MB
~93K SLoC