11 stable releases

3.0.0 Feb 2, 2024
2.5.2 Dec 9, 2023
2.5.0 Oct 8, 2023

#398 in Text processing

Download history 7/week @ 2024-01-31 2/week @ 2024-02-14 15/week @ 2024-02-21 9/week @ 2024-02-28 1/week @ 2024-03-06 1/week @ 2024-03-13 22/week @ 2024-03-27 45/week @ 2024-04-03

67 downloads per month

MIT license

34KB
683 lines

Linoleum

Linoleum is a smooth line editor designed for use in the gosh shell. It's ergonomic, both on the developer side and the user side.

It supports Ctrl-C/-D/-Left/-Right/-Backspace, all out of the box. The characters used by the latter three to delimit words are fully configurable.

Supports history. Make sure to run Editor::save_history before dropping the History.

Also supports completion with a similar interface to prompts; see Editor::completion. Note that completions only respect spaces, not the usual word breaks; this is because some (i.e. file) completions may require more license.

Examples

use linoleum::{Editor, EditResult};

fn main() {
    let mut editor = Editor::new(" > ");
    match editor.read().expect("Failed to read line") {
        EditResult::Ok(s) => println!("You entered: '{s}'"),
        EditResult::Cancel => println!("You canceled!"),
        EditResult::Quit => std::process::exit(1),
    }
}
use std::fmt;
use linoleum::{Editor, EditResult};

struct Prompt {
    template: String,
}

impl fmt::Display for Prompt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "{}", self.template.replace("{greet}", "hello"))
    }
}

fn main() {
    let prompt = Prompt { template: " {greet}> ".to_string() };
    let mut editor = Editor::new(prompt)
        .highlight(
            |data| data.replace("foo", "bar")
        );

    loop {
        match editor.read() {
            Err(e) => {
                eprintln!("failed to read line: {e}");
                break;
            }

            Ok(EditResult::Ok(s)) => {
                if s == "exit" {
                    break;
                } else if s == "clear" {
                    print!("{}[2J{0}[0;0H", 27 as char);
                } else {
                    eprintln!("huh?");
                }
            }

            Ok(EditResult::Cancel) => continue,
            Ok(EditResult::Quit) => break,
        }
    }
}

Dependencies

~0.8–6MB
~20K SLoC