Skip to main content

FilterState

Struct FilterState 

Source
pub struct FilterState {
    pub active: bool,
    /* private fields */
}
Expand description

Inline fuzzy-filter state machine + memoised matched-indices cache. Default opens the filter in the closed / empty / cold-cache state.

Fields§

§active: bool

true while the user is typing in the / bar. Toggles by Self::open / Self::close_keep / Self::close_cancel (the close methods describe the sticky-vs-clear contract).

Implementations§

Source§

impl FilterState

Source

pub fn new() -> Self

Source

pub fn query(&self) -> &str

Read access to the live query buffer. Returned as &str so the caller can’t grow / shrink the underlying String and bypass the cache-invalidation contract on push_char / pop_char / set_query / clear. Use query().len() if you need the byte length and query().is_empty() for the “no filter” check.

Source

pub fn push_char(&mut self, c: char)

Append a character to the query buffer and invalidate the cache. Called by the event loop on every keypress while active.

Source

pub fn pop_char(&mut self)

Pop the last character off the query buffer. Backspace handler. No-op on an empty buffer (the user already cleared the filter; the second backspace must not toggle active off — Esc does that). Invalidates the cache iff a character actually came off.

Source

pub fn set_query(&mut self, q: String)

Overwrite the query buffer wholesale. Invalidates the cache. Used by tests and by any future caller that wants to set the filter programmatically (e.g. a “restore session” path).

Source

pub fn clear(&mut self)

Clear the buffer, close the bar, and invalidate the cache. Used by Self::close_cancel and as a standalone reset.

Source

pub fn open(&mut self)

Open the filter bar. Preserves the existing query so the user can refine an already-sticky filter; close_cancel is how they start fresh. Does NOT invalidate the cache: opening the bar doesn’t change what’s filtered, only that the next keypress targets the buffer.

Source

pub fn close_keep(&mut self)

Close the filter bar but keep the query — the sticky-filter path (Enter). Cache survives: the filter set didn’t change, only the input target. Subsequent reads stay cached.

Source

pub fn close_cancel(&mut self)

Close the filter bar AND clear the query — the cancel path (Esc). Delegates to clear so the cache invalidation contract stays in one place.

Source

pub fn invalidate(&mut self)

Explicit cache flush. Called by App::refresh after it mutates App.worktrees, so the next render recomputes against the fresh list. (The cache_worktrees_len auto-invalidation catches len changes too, but refresh may produce a vec of the same length with different contents — clearing here is the safe play.)

Source

pub fn filtered_indices<F>( &mut self, worktrees: &[WorktreeInfo], compute: F, ) -> &[usize]
where F: FnOnce(&str, &[WorktreeInfo]) -> Vec<usize>,

Memoised lookup of the matched-indices vec. On cold cache, runs compute(&self.query, worktrees) and stores the result; on hot cache (no buffer mutation since the previous call AND same worktrees length), returns the cached slice directly.

Returns a borrowed slice so the call sites in tui/ui.rs don’t pay for a clone on the hot path. The compute closure shape matches fuzzy_match_indices — the App passes that fn in, keeping FilterState agnostic of the matcher (and trivial to unit-test with a counting closure).

Source

pub fn snapshot_indices<F>( &self, worktrees: &[WorktreeInfo], compute: F, ) -> Vec<usize>
where F: FnOnce(&str, &[WorktreeInfo]) -> Vec<usize>,

&self-friendly read for callers that already know the cache is warm (or are willing to recompute via compute without storing). Used by App::selected() which is &self for ergonomics — most callers of selected hold a shared borrow and can’t take the &mut needed by Self::filtered_indices. Returns an owned Vec<usize> because the caller may hit the recompute branch and we can’t surface a temporary as &[usize]. Cheap when the cache is hot (the per-frame render already populated it); equivalent to the pre-extraction cost when cold.

Trait Implementations§

Source§

impl Debug for FilterState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FilterState

Source§

fn default() -> FilterState

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.