Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature renderable filter #48

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/unrenderable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! # Demonstration of configuration options, some of which are un-renderable, demonstrating the filtering ability
//!
//! This blocking example demonstrates some of the configuration options available to the picker.
use std::io::Result;

use nucleo_picker::{nucleo::Config, render::StrRenderer, PickerOptions};


fn list_control_characters() -> Vec<char> {
(0x00..=0x1F).map(|i| char::from_u32(i).unwrap()).collect()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to return a Vec here, you can return impl Iterator<Item = char> to the same effect:

fn control_chars() -> impl Iterator<Item = char> {
    (0x00..=0x1F).map(|i| char::from_u32(i).unwrap())
}

and the code should still compile. The reason is that for looks in rust are actually based on Iterators: the thing you are looping over is anything that implements the IntoIterator trait. The lazy version is better since it saves on an allocation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The impl ... return type says "I am returning a concrete type, but I am just not telling you exactly what it is"; it's not actually a dynamic object. The compiler will replace your impl ... block with the actual type of the thing that you return.

Copy link
Contributor

@alexrutar alexrutar Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, you can directly construct a Range<char>:

const ASCII_CONTROL: Range<char> = '\x00'..='\x1F';

and just use that instead (since Range<char> implements IntoIterator<Item = char>).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so the idea being that we are only storing one char in memory at a time OK makes sense. Will make this change thanks!

fn main() -> Result<()> {
let mut picker = PickerOptions::default()
// set the configuration to match 'path-like' objects
.config(Config::DEFAULT.match_paths())
// set the default query string to `/var`
.query("/var")
.picker(StrRenderer);




let control_chars = list_control_characters();


// populate the matcher
let injector = picker.injector();
for ctrl in control_chars {
let hex_ctrl = format!("{:X}", ctrl as u32); // Convert the char to a u32 and format as hex
let option = format!("hex is {} control {} |",hex_ctrl, ctrl);
injector.push(option);
}

// open interactive prompt
match picker.pick()? {
Some(opt) => {
println!("You selected: '{opt}'");
}
None => {
println!("Nothing selected!");
}
}

Ok(())
}