### Use lscolors command-line utility Source: https://github.com/sharkdp/lscolors/blob/master/README.md Examples of piping command output into the lscolors utility for colorized terminal display. ```bash > find . -maxdepth 2 | lscolors > rg foo -l | lscolors ``` -------------------------------- ### Colorize Output with lscolors CLI Source: https://context7.com/sharkdp/lscolors/llms.txt Examples of using the lscolors binary to colorize piped input or file path arguments. ```bash # Install the command-line tool cargo install lscolors --features=nu-ansi-term # Colorize find output find . -maxdepth 2 | lscolors # Colorize ripgrep file list rg "TODO" -l | lscolors # Colorize locate results locate "*.rs" | lscolors # Pass paths as arguments lscolors /etc/passwd /usr/bin/ls ~/.bashrc # Combine with other tools fd -t f | lscolors | head -20 ``` -------------------------------- ### Get Style by File Type Indicator Source: https://context7.com/sharkdp/lscolors/llms.txt Retrieve styles for specific file types like directories or symlinks using fallback logic similar to GNU ls. ```rust use lscolors::{Indicator, LsColors, Style}; let lscolors = LsColors::from_env().unwrap_or_default(); // Get styles for different file type indicators let indicators = [ (Indicator::Directory, "Directories"), (Indicator::SymbolicLink, "Symbolic links"), (Indicator::ExecutableFile, "Executables"), (Indicator::OrphanedSymbolicLink, "Broken symlinks"), (Indicator::FIFO, "Named pipes"), (Indicator::Socket, "Sockets"), (Indicator::BlockDevice, "Block devices"), (Indicator::CharacterDevice, "Character devices"), (Indicator::Setuid, "Setuid files"), (Indicator::Setgid, "Setgid files"), ]; for (indicator, description) in indicators { if let Some(style) = lscolors.style_for_indicator(indicator) { let ansi = style.to_nu_ansi_term_style(); println!("{}: {}", description, ansi.paint("example")); } } ``` -------------------------------- ### Get Style for a File Path with lscolors Source: https://context7.com/sharkdp/lscolors/llms.txt Retrieves the ANSI style for a given file path based on its type and extension. This method internally uses `Path::symlink_metadata`. Handles cases where no specific style is found. ```rust use lscolors::{LsColors, Style}; use std::path::Path; let lscolors = LsColors::from_env().unwrap_or_default(); // Style for various file types let files = [ "/usr/bin/ls", // executable "/home/user/docs", // directory "archive.tar.gz", // compressed file "photo.jpg", // image "broken_link", // potentially orphaned symlink ]; for file in files { match lscolors.style_for_path(file) { Some(style) => { let ansi = style.to_nu_ansi_term_style(); println!("{}", ansi.paint(file)); } None => { // No specific style - print without coloring println!("{}", file); } } } ``` -------------------------------- ### LsColors::style_for_str Source: https://context7.com/sharkdp/lscolors/llms.txt Get the style for a filename string based on suffix matching rules, without filesystem access. Useful for styling strings that may not correspond to actual files. ```APIDOC ## LsColors::style_for_str - Style by Filename String ### Description Get the style for a filename string based on suffix matching rules, without filesystem access. Useful for styling strings that may not correspond to actual files. ### Method (Implicitly called via iterator) ### Endpoint N/A (Library function) ### Request Example ```rust use lscolors::{LsColors, Style}; let lscolors = LsColors::from_string("*.rs=38;5;208:*.py=33:*.js=93:*.go=36"); let filenames = ["main.rs", "script.py", "app.js", "server.go", "README"]; for name in filenames { match lscolors.style_for_str(name) { Some(style) => { let ansi = style.to_nu_ansi_term_style(); println!("{}", ansi.paint(name)); } None => println!("{} (no style)", name), } } ``` ### Response Example ``` main.rs (styled orange) script.py (styled yellow) app.js (styled bright yellow) server.go (styled cyan) README (no style) ``` ``` -------------------------------- ### Colorize paths with lscolors in Rust Source: https://github.com/sharkdp/lscolors/blob/master/README.md Demonstrates how to initialize LsColors from the environment and apply styles using different terminal styling libraries. ```rust 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)); ``` -------------------------------- ### Configure lscolors dependencies in Cargo.toml Source: https://github.com/sharkdp/lscolors/blob/master/README.md Shows how to enable different coloring backends via Cargo features. ```toml // 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"] } ``` -------------------------------- ### LsColors::from_env Source: https://context7.com/sharkdp/lscolors/llms.txt Initializes an LsColors instance by parsing the LS_COLORS environment variable, with a fallback to default settings. ```APIDOC ## LsColors::from_env ### Description Creates an LsColors instance by parsing the LS_COLORS environment variable. This is the primary way to initialize the library, falling back to sensible defaults if the environment variable is not set. ### Method Static Method ### Request Example let lscolors = LsColors::from_env().unwrap_or_default(); ``` -------------------------------- ### Build lscolors from source Source: https://github.com/sharkdp/lscolors/blob/master/README.md Command to build the lscolors application with specific features enabled. ```rs cargo build --release --features=nu-ansi-term --locked ``` -------------------------------- ### Optimized Styling with Pre-fetched Metadata using lscolors Source: https://context7.com/sharkdp/lscolors/llms.txt Obtains the style for a path using pre-fetched file metadata, avoiding redundant filesystem calls. Requires metadata obtained via `Path::symlink_metadata` for correct symlink handling. ```rust use lscolors::{LsColors, Style}; use std::fs; use std::path::Path; let lscolors = LsColors::from_env().unwrap_or_default(); let path = Path::new("src/lib.rs"); // Pre-fetch metadata (use symlink_metadata for correct symlink handling) let metadata = path.symlink_metadata().ok(); // Get style using pre-fetched metadata let style = lscolors.style_for_path_with_metadata(path, metadata.as_ref()); if let Some(s) = style { let ansi = s.to_nu_ansi_term_style(); println!("{}", ansi.paint(path.display().to_string())); } ``` -------------------------------- ### Load Colors from Environment with lscolors Source: https://context7.com/sharkdp/lscolors/llms.txt Initializes LsColors by parsing the LS_COLORS environment variable. Falls back to defaults if the variable is not set. Demonstrates styling a path and applying it using nu-ansi-term. ```rust use lscolors::{LsColors, Style}; // Load colors from LS_COLORS environment variable with fallback to defaults let lscolors = LsColors::from_env().unwrap_or_default(); // Get style for a file path let path = "project/src/main.rs"; let style = lscolors.style_for_path(path); // Apply styling with nu-ansi-term (default feature) let ansi_style = style.map(Style::to_nu_ansi_term_style).unwrap_or_default(); println!("{}", ansi_style.paint(path)); // Output: colored path based on LS_COLORS settings ``` -------------------------------- ### Configure Cargo Feature Flags Source: https://context7.com/sharkdp/lscolors/llms.txt Shows how to configure terminal styling backends and legacy modes in Cargo.toml. ```toml # Cargo.toml examples # Use nu-ansi-term (default, recommended) [dependencies] lscolors = "0.21" # Use ansi_term [dependencies] lscolors = { version = "0.21", default-features = false, features = ["ansi_term"] } # Use crossterm (cross-platform) [dependencies] lscolors = { version = "0.21", default-features = false, features = ["crossterm"] } # Use owo-colors [dependencies] lscolors = { version = "0.21", default-features = false, features = ["owo-colors"] } # Use gnu_legacy mode (double-digit style codes like GNU ls) [dependencies] lscolors = { version = "0.21", features = ["gnu_legacy"] } ``` -------------------------------- ### Style Filename Strings by Suffix Source: https://context7.com/sharkdp/lscolors/llms.txt Apply styles to filename strings based on suffix matching rules without requiring filesystem access. ```rust use lscolors::{LsColors, Style}; let lscolors = LsColors::from_string("*.rs=38;5;208:*.py=33:*.js=93:*.go=36"); let filenames = ["main.rs", "script.py", "app.js", "server.go", "README"]; for name in filenames { match lscolors.style_for_str(name) { Some(style) => { let ansi = style.to_nu_ansi_term_style(); println!("{}", ansi.paint(name)); } None => println!("{} (no style)", name), } } ``` -------------------------------- ### Style Each Path Component with LsColors Source: https://context7.com/sharkdp/lscolors/llms.txt Iterate through path components and apply styles based on the environment configuration. ```rust use lscolors::{LsColors, Style}; use std::path::Path; let lscolors = LsColors::from_env().unwrap_or_default(); let path = Path::new("/home/user/projects/myapp/src/main.rs"); // Print each path component with its own color for (component, style) in lscolors.style_for_path_components(path) { let component_str = component.to_string_lossy(); match style { Some(s) => { let ansi = s.to_nu_ansi_term_style(); print!("{}", ansi.paint(component_str)); } None => print!("{}", component_str), } } println!(); // Output: each directory/file colored according to its type ``` -------------------------------- ### Create lscolors from Custom String Source: https://context7.com/sharkdp/lscolors/llms.txt Initializes LsColors from a custom LS_COLORS-formatted string. Useful for testing or specific color configurations. Iterates through different file types and applies custom styles. ```rust use lscolors::{LsColors, Style}; // Define custom colors: directories=blue+bold, symlinks=cyan+bold, .rs files=orange let custom_colors = "di=01;34:ln=01;36:*.rs=38;5;208:*.md=32:*.toml=33"; let lscolors = LsColors::from_string(custom_colors); // Test with different file types let paths = vec![ "src/", // directory "README.md", // markdown file "lib.rs", // rust file "Cargo.toml", // toml file ]; for path in paths { if let Some(style) = lscolors.style_for_path(path) { let ansi_style = style.to_nu_ansi_term_style(); println!("{}", ansi_style.paint(path)); } } ``` -------------------------------- ### LsColors::style_for Source: https://context7.com/sharkdp/lscolors/llms.txt Retrieves the style for filesystem DirEntry objects. ```APIDOC ## LsColors::style_for ### Description Get the style for filesystem entries returned by std::fs::read_dir, useful when iterating over directory contents. ### Method Instance Method ### Parameters - **entry** (DirEntry) - Required - The directory entry to style. ``` -------------------------------- ### Convert Style to Terminal Backends Source: https://context7.com/sharkdp/lscolors/llms.txt Convert custom Style structs to various terminal styling library formats by enabling the corresponding Cargo features. ```rust use lscolors::{Color, FontStyle, Style}; // Create a custom style programmatically let style = Style { foreground: Some(Color::RGB(255, 128, 0)), // Orange background: Some(Color::Fixed(236)), // Dark gray font_style: FontStyle { bold: true, italic: false, underline: true, ..Default::default() }, underline: None, }; // Convert to nu-ansi-term (default feature) #[cfg(feature = "nu-ansi-term")] { let nu_style = style.to_nu_ansi_term_style(); println!("{}", nu_style.paint("nu-ansi-term styled")); } // Convert to ansi_term #[cfg(feature = "ansi_term")] { let ansi_style = style.to_ansi_term_style(); println!("{}", ansi_style.paint("ansi_term styled")); } // Convert to crossterm #[cfg(feature = "crossterm")] { let cross_style = style.to_crossterm_style(); println!("{}", cross_style.apply("crossterm styled")); } // Convert to owo-colors #[cfg(feature = "owo-colors")] { use owo_colors::OwoColorize; let owo_style = style.to_owo_colors_style(); println!("{}", "owo-colors styled".style(owo_style)); } ``` -------------------------------- ### LsColors::style_for_path_with_metadata Source: https://context7.com/sharkdp/lscolors/llms.txt Retrieves the style for a path using pre-fetched metadata to avoid redundant filesystem calls. ```APIDOC ## LsColors::style_for_path_with_metadata ### Description Get the style for a path when you already have the file's metadata, avoiding redundant filesystem calls. The metadata must be obtained via Path::symlink_metadata to correctly identify symbolic links. ### Method Instance Method ### Parameters - **path** (Path) - Required - The file path. - **metadata** (Option) - Required - Pre-fetched file metadata. ``` -------------------------------- ### Style DirEntry Objects with lscolors Source: https://context7.com/sharkdp/lscolors/llms.txt Applies styling to filesystem entries obtained from `std::fs::read_dir`. Useful for iterating over directory contents and displaying them with appropriate colors. ```rust use lscolors::{LsColors, Style}; use std::fs; let lscolors = LsColors::from_env().unwrap_or_default(); // List directory contents with colors let dir = fs::read_dir(".").expect("Failed to read directory"); for entry in dir.flatten() { let style = lscolors.style_for(&entry); let name = entry.file_name(); let name_str = name.to_string_lossy(); match style { Some(s) => { let ansi = s.to_nu_ansi_term_style(); println!("{}", ansi.paint(name_str)); } None => println!("{}", name_str), } } ``` -------------------------------- ### Define Colors with Color Enum Source: https://context7.com/sharkdp/lscolors/llms.txt Demonstrates usage of the Color enum to define standard, bright, 8-bit, and 24-bit RGB colors. ```rust use lscolors::Color; // Standard ANSI colors let colors = [ Color::Black, Color::Red, Color::Green, Color::Yellow, Color::Blue, Color::Magenta, Color::Cyan, Color::White, ]; // Bright variants let bright_colors = [ Color::BrightBlack, // Often gray Color::BrightRed, Color::BrightGreen, Color::BrightYellow, Color::BrightBlue, Color::BrightMagenta, Color::BrightCyan, Color::BrightWhite, ]; // 8-bit indexed color (0-255) let fixed_orange = Color::Fixed(208); let fixed_purple = Color::Fixed(141); // 24-bit true color RGB let custom_teal = Color::RGB(0, 128, 128); let custom_coral = Color::RGB(255, 127, 80); ``` -------------------------------- ### LsColors::from_string Source: https://context7.com/sharkdp/lscolors/llms.txt Creates an LsColors instance from a custom LS_COLORS-formatted string. ```APIDOC ## LsColors::from_string ### Description Creates an LsColors instance from a custom LS_COLORS-formatted string, useful for testing or when you want to use specific colors regardless of environment settings. ### Method Static Method ### Parameters - **custom_colors** (String) - Required - A string formatted according to LS_COLORS specifications. ``` -------------------------------- ### LsColors::style_for_path Source: https://context7.com/sharkdp/lscolors/llms.txt Retrieves the ANSI style for a given file path by examining its type and extension. ```APIDOC ## LsColors::style_for_path ### Description Returns the ANSI style for a given path by examining the file's type and extension. This method internally calls Path::symlink_metadata to determine the file type. ### Method Instance Method ### Parameters - **path** (Path) - Required - The file path to style. ``` -------------------------------- ### Style Conversion Source: https://context7.com/sharkdp/lscolors/llms.txt The Style struct can be converted to various terminal styling library formats. Enable the appropriate Cargo feature to use your preferred backend. ```APIDOC ## Style Conversion - Multiple Terminal Backend Support ### Description The Style struct can be converted to various terminal styling library formats. Enable the appropriate Cargo feature to use your preferred backend. ### Method (Implicitly called via iterator) ### Endpoint N/A (Library function) ### Request Example ```rust use lscolors::{Color, FontStyle, Style}; // Create a custom style programmatically let style = Style { foreground: Some(Color::RGB(255, 128, 0)), // Orange background: Some(Color::Fixed(236)), // Dark gray font_style: FontStyle { bold: true, italic: false, underline: true, ..Default::default() }, underline: None, }; // Convert to nu-ansi-term (default feature) #[cfg(feature = "nu-ansi-term")] { let nu_style = style.to_nu_ansi_term_style(); println!("{}", nu_style.paint("nu-ansi-term styled")); } // Convert to ansi_term #[cfg(feature = "ansi_term")] { let ansi_style = style.to_ansi_term_style(); println!("{}", ansi_style.paint("ansi_term styled")); } // Convert to crossterm #[cfg(feature = "crossterm")] { let cross_style = style.to_crossterm_style(); println!("{}", cross_style.apply("crossterm styled")); } // Convert to owo-colors #[cfg(feature = "owo-colors")] { use owo_colors::OwoColorize; let owo_style = style.to_owo_colors_style(); println!("{}", "owo-colors styled".style(owo_style)); } ``` ### Response Example ``` nu-ansi-term styled (styled with custom orange foreground, dark gray background, bold and underline) ansi_term styled (styled with custom orange foreground, dark gray background, bold and underline) crossterm styled (styled with custom orange foreground, dark gray background, bold and underline) owo-colors styled (styled with custom orange foreground, dark gray background, bold and underline) ``` ``` -------------------------------- ### LsColors::style_for_path_components Source: https://context7.com/sharkdp/lscolors/llms.txt Returns an iterator that yields each path component with its respective style, allowing you to color each part of a path individually. ```APIDOC ## LsColors::style_for_path_components - Style Each Path Component ### Description Returns an iterator that yields each path component with its respective style, allowing you to color each part of a path (e.g., `/home/`, `user/`, `file.txt`) individually. ### Method (Implicitly called via iterator) ### Endpoint N/A (Library function) ### Request Example ```rust use lscolors::{LsColors, Style}; use std::path::Path; let lscolors = LsColors::from_env().unwrap_or_default(); let path = Path::new("/home/user/projects/myapp/src/main.rs"); // Print each path component with its own color for (component, style) in lscolors.style_for_path_components(path) { let component_str = component.to_string_lossy(); match style { Some(s) => { let ansi = s.to_nu_ansi_term_style(); print!("{}", ansi.paint(component_str)); } None => print!("{}", component_str), } } println!(); ``` ### Response Example ``` each directory/file colored according to its type ``` ``` -------------------------------- ### LsColors::style_for_indicator Source: https://context7.com/sharkdp/lscolors/llms.txt Returns the style for a specific file type indicator (directory, symlink, executable, etc.), implementing fallback logic similar to GNU ls. ```APIDOC ## LsColors::style_for_indicator - Get Style by File Type Indicator ### Description Returns the style for a specific file type indicator (directory, symlink, executable, etc.), implementing fallback logic similar to GNU ls. ### Method (Implicitly called via iterator) ### Endpoint N/A (Library function) ### Request Example ```rust use lscolors::{Indicator, LsColors, Style}; let lscolors = LsColors::from_env().unwrap_or_default(); // Get styles for different file type indicators let indicators = [ (Indicator::Directory, "Directories"), (Indicator::SymbolicLink, "Symbolic links"), (Indicator::ExecutableFile, "Executables"), (Indicator::OrphanedSymbolicLink, "Broken symlinks"), (Indicator::FIFO, "Named pipes"), (Indicator::Socket, "Sockets"), (Indicator::BlockDevice, "Block devices"), (Indicator::CharacterDevice, "Character devices"), (Indicator::Setuid, "Setuid files"), (Indicator::Setgid, "Setgid files"), ]; for (indicator, description) in indicators { if let Some(style) = lscolors.style_for_indicator(indicator) { let ansi = style.to_nu_ansi_term_style(); println!("{}: {}", description, ansi.paint("example")); } } ``` ### Response Example ``` Directories: example (styled) Symbolic links: example (styled) Executables: example (styled) Broken symlinks: example (styled) Named pipes: example (styled) Sockets: example (styled) Block devices: example (styled) Character devices: example (styled) Setuid files: example (styled) Setgid files: example (styled) ``` ``` -------------------------------- ### Parse ANSI Color Codes into Style Source: https://context7.com/sharkdp/lscolors/llms.txt Convert ANSI escape sequence strings into a Style struct, supporting standard, 8-bit, and 24-bit RGB colors. ```rust use lscolors::style::Style; // Parse various ANSI sequences let sequences = [ ("31", "Red foreground"), ("01;34", "Bold blue"), ("38;5;208", "8-bit orange (color 208)"), ("38;2;255;128;0", "24-bit RGB orange"), ("01;04;33;44", "Bold underlined yellow on blue"), ("48;5;236", "Dark gray background"), ]; for (seq, description) in sequences { if let Some(style) = Style::from_ansi_sequence(seq) { let ansi = style.to_nu_ansi_term_style(); println!("{}: {}", description, ansi.paint("sample text")); } } ``` -------------------------------- ### Classify File Types with Indicator Enum Source: https://context7.com/sharkdp/lscolors/llms.txt Parses LS_COLORS indicator codes into Indicator enum variants. ```rust use lscolors::Indicator; // Parse indicator codes from LS_COLORS let codes = [ ("di", "Directory"), ("ln", "Symbolic link"), ("ex", "Executable"), ("fi", "Regular file"), ("or", "Orphaned symlink"), ("mi", "Missing file"), ("pi", "FIFO/named pipe"), ("so", "Socket"), ("bd", "Block device"), ("cd", "Character device"), ("su", "Setuid file"), ("sg", "Setgid file"), ("st", "Sticky directory"), ("ow", "Other-writable directory"), ("tw", "Sticky + other-writable"), ]; for (code, description) in codes { if let Some(indicator) = Indicator::from(code) { println!("{} => {:?}: {}", code, indicator, description); } } ``` -------------------------------- ### Style::from_ansi_sequence Source: https://context7.com/sharkdp/lscolors/llms.txt Parse an ANSI escape sequence string into a Style struct. Supports standard colors, 8-bit (256) colors, and 24-bit RGB colors, along with font attributes. ```APIDOC ## Style::from_ansi_sequence - Parse ANSI Color Codes ### Description Parse an ANSI escape sequence string into a Style struct. Supports standard colors, 8-bit (256) colors, and 24-bit RGB colors, along with font attributes. ### Method (Implicitly called via iterator) ### Endpoint N/A (Library function) ### Request Example ```rust use lscolors::style::Style; // Parse various ANSI sequences let sequences = [ ("31", "Red foreground"), ("01;34", "Bold blue"), ("38;5;208", "8-bit orange (color 208)"), ("38;2;255;128;0", "24-bit RGB orange"), ("01;04;33;44", "Bold underlined yellow on blue"), ("48;5;236", "Dark gray background"), ]; for (seq, description) in sequences { if let Some(style) = Style::from_ansi_sequence(seq) { let ansi = style.to_nu_ansi_term_style(); println!("{}: {}", description, ansi.paint("sample text")); } } ``` ### Response Example ``` Red foreground: sample text (styled red) Bold blue: sample text (styled bold blue) 8-bit orange (color 208): sample text (styled orange) 24-bit RGB orange: sample text (styled orange) Bold underlined yellow on blue: sample text (styled bold underlined yellow on blue) Dark gray background: sample text (styled with dark gray background) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.