forked from skim-rs/skim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
383 lines (348 loc) · 12.3 KB
/
lib.rs
File metadata and controls
383 lines (348 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Skim is a fuzzy finder library for Rust.
//!
//! It provides a fast and customizable way to filter and select items interactively,
//! similar to fzf. Skim can be used as a library or as a command-line tool.
//!
//! # Examples
//!
//! ```no_run
//! use skim::prelude::*;
//! use std::io::Cursor;
//!
//! let options = SkimOptionsBuilder::default()
//! .height("50%")
//! .multi(true)
//! .build()
//! .unwrap();
//!
//! let input = "awk\nbash\ncsh\ndash\nfish\nksh\nzsh";
//! let item_reader = SkimItemReader::default();
//! let items = item_reader.of_bufread(Cursor::new(input));
//!
//! let output = Skim::run_with(options, Some(items)).unwrap();
//! ```
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![allow(clippy::default_trait_access, clippy::struct_excessive_bools)]
#[macro_use]
extern crate log;
#[global_allocator]
static GLOBAL_ALLOCATOR: mimalloc::MiMalloc = mimalloc::MiMalloc;
use std::any::Any;
use std::borrow::Cow;
use std::fmt::Display;
use std::sync::Arc;
use crate::fuzzy_matcher::MatchIndices;
use ratatui::{
style::Style,
text::{Line, Span},
};
pub use crate::engine::fuzzy::FuzzyAlgorithm;
pub use crate::item::RankCriteria;
pub use crate::options::SkimOptions;
pub use crate::output::SkimOutput;
pub use crate::skim::*;
pub use crate::skim_item::SkimItem;
use crate::tui::Size;
pub use util::printf;
pub mod binds;
mod engine;
pub mod field;
pub mod fuzzy_matcher;
pub mod helper;
pub mod item;
pub mod matcher;
pub mod options;
mod output;
pub mod prelude;
pub mod reader;
mod skim;
mod skim_item;
pub mod spinlock;
pub mod theme;
pub mod tmux;
pub mod tui;
mod util;
#[cfg(feature = "cli")]
pub mod manpage;
#[cfg(feature = "cli")]
pub mod shell;
//------------------------------------------------------------------------------
/// Trait for downcasting to concrete types from trait objects
pub trait AsAny {
/// Returns a reference to the value as `Any`
fn as_any(&self) -> &dyn Any;
/// Returns a mutable reference to the value as `Any`
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T: Any> AsAny for T {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
//------------------------------------------------------------------------------
// Display Context
#[derive(Default, Debug, Clone)]
/// Represents how a query matches an item
pub enum Matches {
/// No matches
#[default]
None,
/// Matches at specific character indices
CharIndices(MatchIndices),
/// Matches in a character range (start, end)
CharRange(usize, usize),
/// Matches in a byte range (start, end)
ByteRange(usize, usize),
}
#[derive(Default, Clone)]
/// Context information for displaying an item
pub struct DisplayContext {
/// The match score for this item
pub score: i32,
/// Where the query matched in the item
pub matches: Matches,
/// The width of the container to display in
pub container_width: usize,
/// The base style to apply to non-matched portions
pub base_style: Style,
/// The style to apply to matched portions
pub matched_style: Style,
}
impl DisplayContext {
/// Converts the context and text into a styled `Line` with highlighted matches
///
/// # Panics
///
/// Panics if the byte ranges in `Matches::ByteRange` do not align with valid UTF-8 boundaries.
#[must_use]
pub fn to_line(self, cow: Cow<str>) -> Line {
let text: String = cow.into_owned();
// Combine base_style with match style for highlighted text
// Match style takes precedence for fg, but inherits bg from base if not set
match &self.matches {
Matches::CharIndices(indices) => {
let mut res = Line::default();
let mut chars = text.chars();
let mut prev_index = 0;
for &index in indices {
let span_content = chars.by_ref().take(index - prev_index);
res.push_span(Span::styled(span_content.collect::<String>(), self.base_style));
let highlighted_char = chars.next().unwrap_or_default().to_string();
res.push_span(Span::styled(
highlighted_char,
self.base_style.patch(self.matched_style),
));
prev_index = index + 1;
}
res.push_span(Span::styled(chars.collect::<String>(), self.base_style));
res
}
// AnsiString::from((context.text, indices, context.highlight_attr)),
#[allow(clippy::cast_possible_truncation)]
Matches::CharRange(start, end) => {
let mut chars = text.chars();
let mut res = Line::default();
res.push_span(Span::styled(
chars.by_ref().take(*start).collect::<String>(),
self.base_style,
));
let highlighted_text = chars.by_ref().take(*end - *start).collect::<String>();
res.push_span(Span::styled(
highlighted_text,
self.base_style.patch(self.matched_style),
));
res.push_span(Span::styled(chars.collect::<String>(), self.base_style));
res
}
Matches::ByteRange(start, end) => {
let mut bytes = text.bytes();
let mut res = Line::default();
res.push_span(Span::styled(
String::from_utf8(bytes.by_ref().take(*start).collect()).unwrap(),
self.base_style,
));
let highlighted_bytes = bytes.by_ref().take(*end - *start).collect();
let highlighted_text = String::from_utf8(highlighted_bytes).unwrap();
res.push_span(Span::styled(
highlighted_text,
self.base_style.patch(self.matched_style),
));
res.push_span(Span::styled(
String::from_utf8(bytes.collect()).unwrap(),
self.base_style,
));
res
}
Matches::None => Line::from(vec![Span::styled(text, self.base_style)]),
}
}
}
//------------------------------------------------------------------------------
// Preview Context
/// Context information for generating item previews
pub struct PreviewContext<'a> {
/// The current search query
pub query: &'a str,
/// The current command query (for interactive mode)
pub cmd_query: &'a str,
/// Width of the preview window
pub width: usize,
/// Height of the preview window
pub height: usize,
/// Index of the current item
pub current_index: usize,
/// Text of the current selection
pub current_selection: &'a str,
/// selected item indices (may or may not include current item)
pub selected_indices: &'a [usize],
/// selected item texts (may or may not include current item)
pub selections: &'a [&'a str],
}
//------------------------------------------------------------------------------
// Preview
/// Position and scroll information for preview display
#[derive(Default, Copy, Clone, Debug)]
pub struct PreviewPosition {
/// Horizontal scroll position
pub h_scroll: Size,
/// Horizontal offset
pub h_offset: Size,
/// Vertical scroll position
pub v_scroll: Size,
/// Vertical offset
pub v_offset: Size,
}
/// Defines how an item should be previewed
pub enum ItemPreview {
/// execute the command and print the command's output
Command(String),
/// Display the prepared text(lines)
Text(String),
/// Display the colored text(lines)
AnsiText(String),
/// Execute a command and display output with position
CommandWithPos(String, PreviewPosition),
/// Display text with position
TextWithPos(String, PreviewPosition),
/// Display ANSI-colored text with position
AnsiWithPos(String, PreviewPosition),
/// Use global command settings to preview the item
Global,
}
//==============================================================================
// A match engine will execute the matching algorithm
/// Case sensitivity mode for matching
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum), clap(rename_all = "snake_case"))]
pub enum CaseMatching {
/// Case-sensitive matching
Respect,
/// Case-insensitive matching
Ignore,
/// Smart case: case-insensitive unless query contains uppercase
#[default]
Smart,
}
/// Typo tolerance configuration for fuzzy matching
///
/// Controls how many character mismatches (typos) are allowed when matching.
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
pub enum Typos {
/// No typo tolerance — query must match exactly
#[default]
Disabled,
/// Adaptive typo tolerance — allows `pattern_length / 4` typos
Smart,
/// Fixed typo tolerance — allows exactly `n` typos
Fixed(usize),
}
impl From<usize> for Typos {
fn from(n: usize) -> Self {
match n {
0 => Typos::Disabled,
n => Typos::Fixed(n),
}
}
}
/// Represents the range of a match in an item
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum MatchRange {
/// Range of bytes (start, end)
ByteRange(usize, usize),
/// Individual character indices that matched
Chars(MatchIndices),
}
/// Rank stores the raw match measurements used for sorting results.
///
/// Named fields preserve the semantic meaning of each value. The actual
/// sort key (taking into account the user-configured tiebreak criteria and
/// their direction) is computed lazily via [`Rank::sort_key`] rather than
/// being baked in at construction time.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Rank {
/// Raw fuzzy/exact match score (higher is a better match)
pub score: i32,
/// Index of the first matched character (0-based)
pub begin: i32,
/// Index of the last matched character (0-based)
pub end: i32,
/// Length of the item text in bytes
pub length: i32,
/// Ordinal position of the item in the input stream
pub index: i32,
/// Byte offset of the first character after the last path separator (`/` or `\`).
/// Equal to `0` when the item text contains no path separator.
pub path_name_offset: i32,
}
/// Result of matching a query against an item
#[derive(Clone, Debug)]
pub struct MatchResult {
/// The rank/score of this match
pub rank: Rank,
/// The range where the match occurred
pub matched_range: MatchRange,
}
impl MatchResult {
#[must_use]
/// Converts the match range to character indices
pub fn range_char_indices(&self, text: &str) -> MatchIndices {
match &self.matched_range {
&MatchRange::ByteRange(start, end) => {
let first = text[..start].chars().count();
let last = first + text[start..end].chars().count();
(first..last).collect()
}
MatchRange::Chars(vec) => vec.clone(),
}
}
}
/// A matching engine that can match queries against items
pub trait MatchEngine: Sync + Send + Display {
/// Matches an item against the query, returning a result if matched
fn match_item(&self, item: &dyn SkimItem) -> Option<MatchResult>;
}
/// Factory for creating match engines
pub trait MatchEngineFactory {
/// Creates a match engine with explicit case sensitivity
fn create_engine_with_case(&self, query: &str, case: CaseMatching) -> Box<dyn MatchEngine>;
/// Creates a match engine with default case sensitivity
fn create_engine(&self, query: &str) -> Box<dyn MatchEngine> {
self.create_engine_with_case(query, CaseMatching::default())
}
}
//------------------------------------------------------------------------------
// Preselection
/// A selector that determines whether an item should be "pre-selected" in multi-selection mode
pub trait Selector {
/// Returns true if the item at the given index should be pre-selected
fn should_select(&self, index: usize, item: &dyn SkimItem) -> bool;
}
//------------------------------------------------------------------------------
/// Sender for streaming items to skim
pub type SkimItemSender = kanal::Sender<Vec<Arc<dyn SkimItem>>>;
/// Receiver for streaming items to skim
pub type SkimItemReceiver = kanal::Receiver<Vec<Arc<dyn SkimItem>>>;