Skip to main content

Crate rabbitsay

Crate rabbitsay 

Source
Expand description

A fun library for displaying messages with a cute ASCII rabbit.

This library provides both a simple high-level API and low-level building blocks for creating rabbitsay messages. Perfect for CLI tools, TUI applications, or anywhere you need a cute rabbit to say something!

§Quick Start

The simplest way to use rabbitsay:

use rabbitsay::say;

let output = say("Hello, world!");
println!("{}", output);

§Custom Configuration

For more control over formatting:

use rabbitsay::{say_with, Config};

let config = Config {
    max_width: 20,
    padding: 2,
};

let output = say_with("A longer message that needs wrapping", config);
println!("{}", output);

§Low-Level API

For complete control, use the individual functions:

use rabbitsay::{wrap_text, build_sign, indent_rabbit, RABBIT_HANDLE_POSITION};

let message = "Hello, world!";
let lines = wrap_text(message, 20);
let mut sign = build_sign(&lines, 4);

let sign_width = sign.first()
    .map(|line| line.chars().count())
    .unwrap_or(0);

// For narrow signs, indent the sign to align with the rabbit's handle
// For wide signs, indent the rabbit to center its handle under the sign
let (sign_indent, rabbit_indent) = if sign_width < RABBIT_HANDLE_POSITION * 2 {
    (RABBIT_HANDLE_POSITION.saturating_sub(sign_width / 2), 0)
} else {
    (0, (sign_width / 2).saturating_sub(RABBIT_HANDLE_POSITION))
};

if sign_indent > 0 {
    let indent_str = " ".repeat(sign_indent);
    sign = sign.iter()
        .map(|line| format!("{}{}", indent_str, line))
        .collect();
}

println!("{}", sign.join("\n"));
println!("{}", indent_rabbit(rabbit_indent));

Structs§

Config
Configuration for generating rabbitsay messages.

Constants§

RABBIT_HANDLE_POSITION
Position of the rabbit’s handle (||) center in the ASCII art. The handle is at columns 7-8, so we center at position 8.

Functions§

build_sign
Builds a bordered message box around the provided lines of text.
indent_rabbit
Creates an ASCII art rabbit with the specified indentation.
say
Generate a complete rabbitsay message with default configuration.
say_with
Generate a rabbitsay message with custom configuration.
wrap_text
Wraps text to fit within a maximum width, breaking at word boundaries.