moving things around and documentation

This commit is contained in:
Alexandre Pasmantier 2024-11-05 00:49:42 +01:00
parent 635ea8a774
commit 65cad8dda8
4 changed files with 94 additions and 93 deletions

View File

@ -16,8 +16,11 @@ pub struct MatchedItem<I>
where where
I: Sync + Send + Clone + 'static, I: Sync + Send + Clone + 'static,
{ {
/// The matched item.
pub inner: I, pub inner: I,
/// The dimension against which the item was matched (as a string).
pub matched_string: String, pub matched_string: String,
/// The indices of the matched characters.
pub match_indices: Vec<(u32, u32)>, pub match_indices: Vec<(u32, u32)>,
} }
@ -29,6 +32,7 @@ where
/// front-end and display a loading indicator. /// front-end and display a loading indicator.
#[derive(Default)] #[derive(Default)]
pub struct Status { pub struct Status {
/// Whether the matcher is currently running.
pub running: bool, pub running: bool,
} }
@ -52,9 +56,13 @@ impl From<nucleo::Status> for Status {
/// cores on the current machine). /// cores on the current machine).
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Config { pub struct Config {
/// The number of threads to use for the fuzzy matcher.
pub n_threads: Option<usize>, pub n_threads: Option<usize>,
/// Whether to ignore case when matching.
pub ignore_case: bool, pub ignore_case: bool,
/// Whether to prefer prefix matches.
pub prefer_prefix: bool, pub prefer_prefix: bool,
/// Whether to optimize for matching paths.
pub match_paths: bool, pub match_paths: bool,
} }
@ -118,6 +126,7 @@ pub struct Injector<I>
where where
I: Sync + Send + Clone + 'static, I: Sync + Send + Clone + 'static,
{ {
/// The inner `Injector` from the `Nucleo` fuzzy matcher.
inner: nucleo::Injector<I>, inner: nucleo::Injector<I>,
} }
@ -166,10 +175,15 @@ pub struct Matcher<I>
where where
I: Sync + Send + Clone + 'static, I: Sync + Send + Clone + 'static,
{ {
/// The inner `Nucleo` fuzzy matcher.
inner: nucleo::Nucleo<I>, inner: nucleo::Nucleo<I>,
/// The current total number of items in the matcher.
pub total_item_count: u32, pub total_item_count: u32,
/// The current number of matched items in the matcher.
pub matched_item_count: u32, pub matched_item_count: u32,
/// The current status of the matcher.
pub status: Status, pub status: Status,
/// The last pattern that was matched against.
pub last_pattern: String, pub last_pattern: String,
} }

View File

@ -8,24 +8,25 @@ use tracing::{debug, info};
use crate::app::App; use crate::app::App;
use crate::channels::stdin::Channel as StdinChannel; use crate::channels::stdin::Channel as StdinChannel;
use crate::cli::Cli; use crate::cli::Cli;
use crate::utils::is_readable_stdin;
mod action; pub mod action;
mod app; pub mod app;
mod channels; pub mod channels;
mod cli; pub mod cli;
mod config; pub mod config;
mod entry; pub mod entry;
mod errors; pub mod errors;
mod event; pub mod event;
mod fuzzy; pub mod fuzzy;
mod logging; pub mod logging;
mod picker; pub mod picker;
mod previewers; pub mod previewers;
mod render; pub mod render;
mod television; pub mod television;
mod tui; pub mod tui;
mod ui; pub mod ui;
mod utils; pub mod utils;
#[tokio::main(flavor = "multi_thread")] #[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -56,62 +57,3 @@ async fn main() -> Result<()> {
} }
Ok(()) Ok(())
} }
pub fn is_readable_stdin() -> bool {
use std::io::IsTerminal;
#[cfg(unix)]
fn imp() -> bool {
use std::{
fs::File,
os::{fd::AsFd, unix::fs::FileTypeExt},
};
let stdin = std::io::stdin();
let Ok(fd) = stdin.as_fd().try_clone_to_owned() else {
return false;
};
let file = File::from(fd);
let Ok(md) = file.metadata() else {
return false;
};
let ft = md.file_type();
let is_file = ft.is_file();
let is_fifo = ft.is_fifo();
let is_socket = ft.is_socket();
is_file || is_fifo || is_socket
}
#[cfg(windows)]
fn imp() -> bool {
let stdin = winapi_util::HandleRef::stdin();
let typ = match winapi_util::file::typ(stdin) {
Ok(typ) => typ,
Err(err) => {
log::debug!(
"for heuristic stdin detection on Windows, \
could not get file type of stdin \
(thus assuming stdin is not readable): {err}",
);
return false;
}
};
let is_disk = typ.is_disk();
let is_pipe = typ.is_pipe();
let is_readable = is_disk || is_pipe;
log::debug!(
"for heuristic stdin detection on Windows, \
found that is_disk={is_disk} and is_pipe={is_pipe}, \
and thus concluded that is_stdin_readable={is_readable}",
);
is_readable
}
#[cfg(not(any(unix, windows)))]
fn imp() -> bool {
log::debug!("on non-{{Unix,Windows}}, assuming stdin is not readable");
false
}
!std::io::stdin().is_terminal() && imp()
}

View File

@ -221,14 +221,6 @@ impl Television {
/// Build the corresponding spans for a group of keys. /// Build the corresponding spans for a group of keys.
/// ///
/// # Arguments
/// - `group_name`: The name of the group.
/// - `key_groups`: A vector of vectors of strings representing the keys for each group.
/// Each vector of strings represents a group of alternate keys for a given `Action`.
///
/// # Returns
/// A vector of `Span`s representing the key groups.
///
/// # Example /// # Example
/// ```rust /// ```rust
/// use ratatui::text::Span; /// use ratatui::text::Span;
@ -259,7 +251,6 @@ fn build_cells_for_key_groups(
))]; ))];
let mut spans = Vec::new(); let mut spans = Vec::new();
//spans.push(Span::styled("[", Style::default().fg(KEY_COLOR)));
let key_group_spans: Vec<Span> = non_empty_groups let key_group_spans: Vec<Span> = non_empty_groups
.map(|keys| { .map(|keys| {
@ -274,7 +265,6 @@ fn build_cells_for_key_groups(
} }
}); });
//spans.push(Span::styled("]", Style::default().fg(KEY_COLOR)));
cells.push(Cell::from(Line::from(spans))); cells.push(Cell::from(Line::from(spans)));
cells cells
@ -282,13 +272,6 @@ fn build_cells_for_key_groups(
/// Get the keys for a given action. /// Get the keys for a given action.
/// ///
/// # Arguments
/// - `keymap`: A hashmap of keybindings.
/// - `action`: The action to get the keys for.
///
/// # Returns
/// A vector of strings representing the keys for the given action.
///
/// # Example /// # Example
/// ```rust /// ```rust
/// use std::collections::HashMap; /// use std::collections::HashMap;

View File

@ -2,3 +2,65 @@ pub mod files;
pub mod indices; pub mod indices;
pub mod strings; pub mod strings;
pub mod syntax; pub mod syntax;
/// Heuristic to determine if stdin is readable.
///
/// This is used to determine if we should use the stdin channel.
pub fn is_readable_stdin() -> bool {
use std::io::IsTerminal;
#[cfg(unix)]
fn imp() -> bool {
use std::{
fs::File,
os::{fd::AsFd, unix::fs::FileTypeExt},
};
let stdin = std::io::stdin();
let Ok(fd) = stdin.as_fd().try_clone_to_owned() else {
return false;
};
let file = File::from(fd);
let Ok(md) = file.metadata() else {
return false;
};
let ft = md.file_type();
let is_file = ft.is_file();
let is_fifo = ft.is_fifo();
let is_socket = ft.is_socket();
is_file || is_fifo || is_socket
}
#[cfg(windows)]
fn imp() -> bool {
let stdin = winapi_util::HandleRef::stdin();
let typ = match winapi_util::file::typ(stdin) {
Ok(typ) => typ,
Err(err) => {
log::debug!(
"for heuristic stdin detection on Windows, \
could not get file type of stdin \
(thus assuming stdin is not readable): {err}",
);
return false;
}
};
let is_disk = typ.is_disk();
let is_pipe = typ.is_pipe();
let is_readable = is_disk || is_pipe;
log::debug!(
"for heuristic stdin detection on Windows, \
found that is_disk={is_disk} and is_pipe={is_pipe}, \
and thus concluded that is_stdin_readable={is_readable}",
);
is_readable
}
#[cfg(not(any(unix, windows)))]
fn imp() -> bool {
log::debug!("on non-{{Unix,Windows}}, assuming stdin is not readable");
false
}
!std::io::stdin().is_terminal() && imp()
}