Coder Social home page Coder Social logo

rucline's Introduction

rucline

Github MIT licensed Cargo Documentation

demo

Code for demo above available at examples/multiple.rs

Rucline, the Rust CLI Line reader, or simply "recline", is a cross-platform, UTF-8 compatible line reader that provides hooks for autocompletion and tab suggestion. It supports advanced editing actions and hooks for customizing the line reader behavior of the line reader making it more flexible than simply reading from stdin.

Basic usage:

use rucline::completion;
use rucline::Prompt;

if let Ok(Some(string)) = Prompt::from("What's you favorite website? ")
    // Add some tab completions (Optional)
    .suggester(completion::Basic::new(&[
        "https://www.rust-lang.org/",
        "https://docs.rs/",
        "https://crates.io/",
    ]))
    //Block until value is ready
    .read_line()
{
    println!("'{}' seems to be your favorite website", string);
}

Actions

Rucline's behavior can be customized and composed with use of actions.

There is a built-in set of default actions that will be executed upon user interaction. These are meant to feel natural when coming from the default terminal, while also adding further functionality and editing commands. For example, a few of the build-ins:

  • Tab: cycle through completions
  • Shift + Tab: cycle through completions in reverse
  • CTRL + W: delete the current work
  • CTRL + J: delete the beginning of the word
  • CTRL + K: delete the end of the word
  • CTRL + U: delete the line
  • CTRL + H: delete the beggining of the line
  • CTRL + L: delete the end of the line

See Action for the full default behavior specification

The default behavior can be customized by overriding user events with actions. Which in turn can be serialized, stored, and loaded at run-time.

Overriding key bindings

use rucline::{completion, Prompt};
use rucline::actions::{Action, Event, KeyBindings, Range};
use crossterm::event::KeyCode;

let mut bindings = KeyBindings::new();

// Accept the full suggestions if `right` is pressed
bindings.insert(Event::from(KeyCode::Right), Action::Complete(Range::Line));

if let Ok(Some(string)) = Prompt::from("What's you favorite website? ")
    // Add some likely values as completions
    .completer(completion::Basic::new(&[
        "https://www.rust-lang.org/",
        "https://docs.rs/",
        "https://crates.io/",
    ]))
    // Set the new key bindings as an override
    .overrider(bindings)
    //Block until value is ready
    .read_line()
{
    println!("'{}' seems to be your favorite website", string);
}

rucline's People

Contributors

m-lima avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.