repl-block

Crates.iorepl-block
lib.rsrepl-block
version0.9.0
sourcesrc
created_at2024-05-15 01:15:22.611471
updated_at2024-06-05 12:56:04.623107
descriptionA crossterm-based library for building Read-Eval-Print-Loops (REPLs).
homepage
repositoryhttps://github.com/jjpe/repl-block.git
max_upload_size
id1240509
size64,844
Joey Ezechiƫls (jjpe)

documentation

README

repl-block

crates.io Documentation Rust

Synopsis

This crate provides a simple and easy way to build a Read-Eval-Print-Loop, a.k.a. REPL.

Usage

Add a dependency on this crate to your project's Cargo.toml:

[dependencies]
repl-block= "0.9.0"

Then one can use the ReplBuilder type to build an start a REPL like this:

use repl_block::prelude::{ReplBuilder, ReplBlockResult, Utf8PathBuf};

fn main() -> ReplBlockResult<()> {
    let mut evaluator = /* initialize your evaluator */;
    let path = Utf8PathBuf::try_from(env::current_dir()?)?.join(".repl.history");
    ReplBuilder::default()
        // Explicitly register .repl.history as the history file:
        .history_filepath(path)
        // Register the evaluator; the default evaluator fn is NOP
        .evaluator(|query: &str| {
            match evaluator.evaluate(query) {
                Ok(value) => println!("{value}"),
                Err(err)  => println!("{err}"),
            }
            Ok(())
        })
        .build()? // Use `self` to build a REPL
        .start()?;
    Ok(())
}

Commit count: 62

cargo fmt