๐Ÿ“ฆ sharkdp / lscolors

A Rust library and tool to colorize paths using LS_COLORS

โ˜… 285 stars โ‘‚ 21 forks ๐Ÿ‘ 285 watching โš–๏ธ Apache License 2.0
ansi-colorscommand-linefilesystemhacktoberfestls-colorsrustshellterminal
๐Ÿ“ฅ Clone https://github.com/sharkdp/lscolors.git
HTTPS git clone https://github.com/sharkdp/lscolors.git
SSH git clone git@github.com:sharkdp/lscolors.git
CLI gh repo clone sharkdp/lscolors
David Peter David Peter Bump version d080abd 3 months ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ src
๐Ÿ“„ .gitignore
๐Ÿ“„ Cargo.toml
๐Ÿ“„ LICENSE-APACHE
๐Ÿ“„ LICENSE-MIT
๐Ÿ“„ README.md
๐Ÿ“„ README.md

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 = "0.21", features = ["ansi_term"] }
// use crossterm coloring
lscolors = { version = "0.21", features = ["crossterm"] }
// use nu-ansi-term coloring
lscolors = { version = "0.21", features = ["nu-ansi-term"] }
// use nu-ansi-term coloring in gnu legacy mode with double digit styles
lscolors = { version = "0.21", features = ["gnu_legacy"] }

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.

References

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