Crate rusty_repl

Crate rusty_repl 

Source
Expand description

Β§πŸ¦€ rusty_repl

Rusty REPL

A lightweight, modular framework for building interactive REPLs in Rust β€” with clean prompts, terminal isolation, and customisable input handling.


§✨ Features

  • πŸ–₯️ Alternate screen mode β€” clean, isolated terminal workspace
  • πŸ’¬ Custom prompts β€” easily define your own Prompt style via CleanPrompt
  • ⚑ Input handling loop β€” powered by reedline
  • 🧹 Automatic cleanup β€” restores cursor, title, and screen on exit
  • 🎨 Keyword highlighting β€” built-in highlighter with custom color schemes (KeywordStyle)
  • 🧩 Extensible design β€” drop in new highlighters, prompts, or REPL logic
  • πŸ› οΈ Config builder β€” safely construct REPL configuration with ReplConfig::new().with_title(...).with_prompt(...).with_kw_style(...)

Β§πŸš€ Quick Start

Add to your Cargo.toml:

[dependencies]
rusty_repl = "0.3.0"

Then in your project:

β“˜
use rusty_repl::Repl;

fn input_handler(cmd: String) -> bool {
    match cmd.as_str() {
        "e" | "q" | "quit" | "exit" => {
            return true;
        }
        val => println!("{val}"),
    };

    false
}

fn main() {
    // Use default configuration
    let repl_manager = Repl::new();

    // Run REPL loop
    let _ = repl_manager.run(run);
}

Or

β“˜
use rusty_repl::{CleanPrompt, Color, DefaultPromptSegment, KeywordStyle, Repl, ReplConfig};

fn input_handler(cmd: String) -> bool {
    match cmd.as_str() {
        "e" | "q" | "quit" | "exit" => {
            return true;
        }
        val => println!("{val}"),
    };

    false
}

fn main() {
    // Configure keywords
    let ks = KeywordStyle::new(
        vec!["ls", "pwd", "cd", "e", "q", "quit", "exit"],
        Color::Red,
    );

    // Configure prompt
    let default_prompt = CleanPrompt::from(
        DefaultPromptSegment::Basic("❯❯❯ ".to_string()),
        DefaultPromptSegment::Empty,
    );

    // Build REPL configuration
    let cfg = ReplConfig::new("REPL")
        .with_kw_style(ks)
        .with_prompt(default_prompt);

    let repl_manager = Repl::from(cfg);

    // Run REPL loop
    let _ = repl_manager.run(run);
}

This opens an alternate terminal screen with a minimal prompt.

Type exit to leave the session.

§🧩 Architecture

ModuleResponsibility
repl::inputHandles user input with reedline
repl::terminalManages terminal (alternate screen, cursor, cleanup)
repl::promptDefines a cleaner, customisable prompt
repl::highlighterHighlights configured keywords during input
repl::styleConfigures keyword styles (colors, word list)
repl::replConnects all components into a cohesive REPL session

Β§πŸ”§ Future Enhancements

  • πŸ’Ύ Persistent command history
  • 🧠 Syntax-aware input completion
  • 🌈 Theming and multi-color highlighting

Β§πŸ“œ License

Licensed under the MIT License

Β§πŸ“ Changelog

Β§0.3.0

  • Introduced ReplConfig builder with .with_title(), .with_prompt(), .with_kw_style()
  • REPL prompt now uses Arc<dyn Prompt> to avoid ownership/move issues
  • InputHandler stores the prompt internally; run no longer requires external prompt
  • Updated all modules to consume Arc<ReplConfig> instead of cloning individual fields
  • Documentation updated for builder pattern, prompt handling, and Arc usage
  • Example code updated to reflect new API

Β§0.2.0

  • Added keyword highlighting support
  • Introduced KeywordHighlighter for basic keyword-based styling.
  • Exposed KeywordStyle as part of the public API.
  • Updated Repl::new to accept an optional KeywordStyle argument.

StructsΒ§

CleanPrompt
A simplified Prompt implementation that suppresses Reedline’s extra visual indicators while keeping your custom prompt text.
KeywordStyle
Represents a set of keywords and the style used to highlight them.
Repl
Top-level REPL manager that orchestrates terminal setup and user input.
ReplConfig
Configuration for the REPL (Read-Eval-Print Loop).

EnumsΒ§

Color
A color is one specific type of ANSI escape code, and can refer to either the foreground or background color.
DefaultPromptSegment
A struct to control the appearance of the left or right prompt in a DefaultPrompt