Skip to content

Commit

Permalink
moving things around and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Nov 4, 2024
1 parent 635ea8a commit 65cad8d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 94 deletions.
14 changes: 14 additions & 0 deletions crates/television/fuzzy/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ pub struct MatchedItem<I>
where
I: Sync + Send + Clone + 'static,
{
/// The matched item.
pub inner: I,
/// The dimension against which the item was matched (as a string).
pub matched_string: String,
/// The indices of the matched characters.
pub match_indices: Vec<(u32, u32)>,
}

Expand All @@ -29,6 +32,7 @@ where
/// front-end and display a loading indicator.
#[derive(Default)]
pub struct Status {
/// Whether the matcher is currently running.
pub running: bool,
}

Expand All @@ -52,9 +56,13 @@ impl From<nucleo::Status> for Status {
/// cores on the current machine).
#[derive(Copy, Clone)]
pub struct Config {
/// The number of threads to use for the fuzzy matcher.
pub n_threads: Option<usize>,
/// Whether to ignore case when matching.
pub ignore_case: bool,
/// Whether to prefer prefix matches.
pub prefer_prefix: bool,
/// Whether to optimize for matching paths.
pub match_paths: bool,
}

Expand Down Expand Up @@ -118,6 +126,7 @@ pub struct Injector<I>
where
I: Sync + Send + Clone + 'static,
{
/// The inner `Injector` from the `Nucleo` fuzzy matcher.
inner: nucleo::Injector<I>,
}

Expand Down Expand Up @@ -166,10 +175,15 @@ pub struct Matcher<I>
where
I: Sync + Send + Clone + 'static,
{
/// The inner `Nucleo` fuzzy matcher.
inner: nucleo::Nucleo<I>,
/// The current total number of items in the matcher.
pub total_item_count: u32,
/// The current number of matched items in the matcher.
pub matched_item_count: u32,
/// The current status of the matcher.
pub status: Status,
/// The last pattern that was matched against.
pub last_pattern: String,
}

Expand Down
96 changes: 19 additions & 77 deletions crates/television/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ use tracing::{debug, info};
use crate::app::App;
use crate::channels::stdin::Channel as StdinChannel;
use crate::cli::Cli;

mod action;
mod app;
mod channels;
mod cli;
mod config;
mod entry;
mod errors;
mod event;
mod fuzzy;
mod logging;
mod picker;
mod previewers;
mod render;
mod television;
mod tui;
mod ui;
mod utils;
use crate::utils::is_readable_stdin;

pub mod action;
pub mod app;
pub mod channels;
pub mod cli;
pub mod config;
pub mod entry;
pub mod errors;
pub mod event;
pub mod fuzzy;
pub mod logging;
pub mod picker;
pub mod previewers;
pub mod render;
pub mod television;
pub mod tui;
pub mod ui;
pub mod utils;

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -56,62 +57,3 @@ async fn main() -> Result<()> {
}
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()
}
17 changes: 0 additions & 17 deletions crates/television/ui/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,6 @@ impl Television {

/// 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
/// ```rust
/// use ratatui::text::Span;
Expand Down Expand Up @@ -259,7 +251,6 @@ fn build_cells_for_key_groups(
))];

let mut spans = Vec::new();
//spans.push(Span::styled("[", Style::default().fg(KEY_COLOR)));

let key_group_spans: Vec<Span> = non_empty_groups
.map(|keys| {
Expand All @@ -274,21 +265,13 @@ fn build_cells_for_key_groups(
}
});

//spans.push(Span::styled("]", Style::default().fg(KEY_COLOR)));
cells.push(Cell::from(Line::from(spans)));

cells
}

/// 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
/// ```rust
/// use std::collections::HashMap;
Expand Down
62 changes: 62 additions & 0 deletions crates/television/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,65 @@ pub mod files;
pub mod indices;
pub mod strings;
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()
}

0 comments on commit 65cad8d

Please sign in to comment.