#readline #input #terminal #console #gui

sys rline

Input line retrieval using libreadline’s “Alternate Interface”

9 releases

✓ Uses Rust 2018 edition

0.3.0 Jan 20, 2020
0.2.1 Mar 9, 2019
0.2.0 Feb 23, 2019
0.1.4 Dec 26, 2018
0.0.0 Oct 22, 2018

#45 in Unix APIs

Download history 25/week @ 2019-11-27 8/week @ 2019-12-04 309/week @ 2019-12-11 1/week @ 2019-12-18 14/week @ 2019-12-25 9/week @ 2020-01-08 104/week @ 2020-01-15 3/week @ 2020-01-22 1/week @ 2020-01-29 4/week @ 2020-02-05 10/week @ 2020-02-12 32/week @ 2020-02-19 20/week @ 2020-02-26 18/week @ 2020-03-04 9/week @ 2020-03-11

194 downloads per month
Used in notnow

GPL-3.0-or-later

27KB
328 lines

pipeline coverage crates.io Docs rustc

rline

rline is a crate providing a convenient wrapper around libreadline's "Alternate Interface". It provides the goodness of libreadline (its ubiquity, powerful support for inputting text, as well as configurability) while leaving the actual character input in the hands of a user (or developer).

This can be a powerful tool for terminal based (but not necessarily command line based) applications that want to handle input on their own terms and only harness libreadline for a subset of their text input needs. But even graphical applications, which typically are not using file stream based input (or a terminal for that matter) at all, are enabled to use libreadline for providing an input experience as usually only known on the command line.

Usage

Integration of the crate into an application or library generally contains the following parts:

// Create a "context" for interacting with libreadline. Contexts are
// isolated from each other, such that you can keep input state and
// history around on a per-object basis (for example, per text input
// field).
let mut rl = rline::Readline::new();

// ...

// Feed data to libreadline and check what the result is. Globally
// configured settings (e.g., via /etc/inputrc) are honored. The result
// is either a completed line or `None`, if editing is still in
// progress.
if let Some(line) = rl.feed(&raw_input) {
  // We got a completed line. Work with to it.
  my_process(&line);
} else {
  // Editing is still in progress. Depending on the use-case we may want
  // to look at the current line input so far as well as the cursor
  // position, for example, to update the screen accordingly.
  rl.peek(|line_so_far, cursor| {
    my_display(&line_so_far, cursor)
  });
};

// ...

// If the user supplied text out-of-band, e.g., by pasting it via a
// cursor based input device, the libreadline state can be adjusted
// accordingly:
let new_line = CStr::from_bytes_with_nul(b"copied-and-pasted\0").unwrap();
let new_cursor = 6;
let clear_undo = true;
rl.reset(new_line, new_cursor, clear_undo);

Please note that rline requires libreadline to be available on the system. It does not support alternative line-editing implementations such as libedit.

Examples

A ready-to-compile example showing the usage of the crate to get line-editing support for a command line based applications is available.

The notnow program is (optionally) using the rline crate for the input of text in its terminal based UI. Another example integration can be found there.

Dependencies