Crate inksac

Source
Expand description

A type-safe terminal text styling library with color support

inksac provides a safe and ergonomic way to add colors and styles to terminal text output. It automatically detects terminal capabilities and handles fallbacks gracefully.

§Key Features

  • Safe color handling with terminal capability detection
  • Support for 16 colors, 256 colors, and true color (16M colors)
  • Composable text styling (bold, italic, underline)
  • RGB and hex color definitions
  • Error handling for all operations
  • Zero unsafe code

§Basic Usage

use inksac::{Color, Style, Styleable};

// Create a style
let style = Style::builder()
    .foreground(Color::Green)
    .bold()
    .build();

// Apply style to text
let colored_text = "Hello, world!".style(style);
println!("{}", colored_text);

§Advanced Usage

use inksac::{Color, Style, Styleable};

// RGB colors (requires true color support)
let rgb_style = Style::builder()
    .foreground(Color::new_rgb(255, 128, 0).unwrap())
    .italic()
    .build();

// Hex colors
let hex_style = Style::builder()
    .background(Color::new_hex("#FF8000").unwrap())
    .underline()
    .build();

// Compose styles
let text = "Custom colors!"
    .style(rgb_style)
    .with_style(hex_style);

§Error Handling

The library uses proper error handling throughout:

use inksac::{check_color_support, ColorSupport, ColorError};

fn print_colored() -> Result<(), ColorError> {
    // Check terminal capabilities
    let support = check_color_support()?;
     
    match support {
        ColorSupport::TrueColor => {
            // Use RGB colors
        }
        ColorSupport::Basic => {
            // Fallback to basic colors
        }
        _ => {
            // Handle no color support
        }
    }
     
    Ok(())
}

Modules§

prelude

Structs§

ColoredString
A string with an associated style
Style
Represents a complete text style including colors and formatting
StyleBuilder
Builder for creating Style instances

Enums§

Color
ColorError
Represents errors that can occur when working with colors
ColorSupport
Terminal color support levels

Traits§

Styleable
Trait for applying styles to strings

Functions§

check_color_support
Check the level of color support in the current terminal
is_color_available
Check if the terminal supports ANSI colors