Skip to content

a performant and Unicode-aware fuzzy picker tui library for rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

autobib/nucleo-picker

Repository files navigation

Current crates.io release Documentation

nucleo-picker

A native Rust library which enables you to incorporate a highly performant and Unicode-aware fuzzy picker directly in your own terminal application.

This library provides a TUI for the nucleo crate with an interface similar to the fzf command-line tool.

  • For implementation examples, jump to the fzf example or see the examples directory.
  • For documentation of interactive usage of the picker, see the USAGE.md file.
  • For a list of recent changes, see the CHANGELOG.md file.

Elevator pitch

Why use this library instead of a general-purpose fuzzy-finder such as fzf or a lower level library such as nucleo?

  1. Much tighter integration between your data source and your application. Instead of reading from a SQLite database with sqlite3 and then parsing raw text, read directly into in-memory data structures with rusqlite and render the in-memory objects in the picker.
  2. Skip the subprocess overhead and improve startup time. Instead of starting up a subprocess to call fzf, have the picker integrated directly into your binary.
  3. Distinguish items from their matcher representation. Instead of writing your data structure to a string, passing it to fzf, and then parsing the resulting match string back into your data structure, directly obtain the original data structure when matching is complete.
  4. Don't spend time debugging terminal rendering edge cases. Out-of-the-box, nucleo-picker handles terminal rendering subtleties such as multiline rendering, double-width Unicode, automatic overflow scrollthrough, and grapheme-aware query input so you don't have to.

Features

  • Highly optimized matching.
  • Robust rendering:
    • Full Unicode handling with Unicode text segmentation and Unicode width.
    • Match highlighting with automatic scroll-through.
    • Correctly render multi-line or overflowed items.
    • Responsive interface with batched keyboard input.
  • Ergonomic API:
    • Fully concurrent lock- and wait-free streaming of input items.
    • Generic Picker for any type T which is Send + Sync + 'static.
    • Customizable rendering of crate-local and foreign types with the Render trait.

Example

Implement a heavily simplified fzf clone in 25 lines of code. Try it out with:

cargo build --release --example fzf
cat myfile.txt | ./target/release/examples/fzf

The code to create the binary:

use std::{
    io::{self, BufRead},
    process::exit,
    thread::spawn,
};

use nucleo_picker::{render::StrRenderer, Picker};

fn main() -> io::Result<()> {
    let mut picker = Picker::new(StrRenderer);

    let injector = picker.injector();
    spawn(move || {
        for line in io::stdin().lock().lines() {
            if let Ok(s) = line {
                injector.push(s);
            }
        }
    });

    match picker.pick()? {
        Some(it) => println!("{it}"),
        None => exit(1),
    }
    Ok(())
}

Related crates

This crate mainly exists as a result of the author's annoyance with pretty much every fuzzy picker TUI in the rust ecosystem.

  • skim's Arc<dyn SkimItem> is inconvenient for a variety of reasons. skim also has a large number of dependencies and is designed more as a binary than a library.
  • fuzzypicker is based on skim and inherits skim's problems.
  • nucleo-ui only has a blocking API and only supports matching on String. It also seems to be un-maintained.
  • fuzzy-select only has a blocking API.
  • dialoguer FuzzySelect only has a blocking API and only supports matching on String. The terminal handling also has a few strange bugs.

Disclaimer

The feature set of this library is quite minimal (by design) but may be expanded in the future. There are a currently a few known problems which have not been addressed (see the issues page on GitHub for a list).

About

a performant and Unicode-aware fuzzy picker tui library for rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages