21 releases (breaking)

0.17.0 Jan 24, 2024
0.16.0 Nov 29, 2023
0.15.0 Jul 24, 2023
0.13.0 Nov 20, 2022
0.3.0 Dec 15, 2018

#26 in Command-line interface

Download history 8909/week @ 2023-12-23 8886/week @ 2023-12-30 11862/week @ 2024-01-06 12602/week @ 2024-01-13 10696/week @ 2024-01-20 12901/week @ 2024-01-27 12180/week @ 2024-02-03 11239/week @ 2024-02-10 11722/week @ 2024-02-17 13877/week @ 2024-02-24 11604/week @ 2024-03-02 14221/week @ 2024-03-09 12252/week @ 2024-03-16 13911/week @ 2024-03-23 14326/week @ 2024-03-30 10118/week @ 2024-04-06

52,461 downloads per month
Used in 83 crates (20 directly)

MIT/Apache

64KB
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.1–9MB
~55K SLoC