Implement case insensitive, fix word disappearing bug

Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text
This commit is contained in:
Jeevitha Kannan K S 2024-11-10 18:46:35 +05:30
parent b8f0b82d5d
commit db0f6c5857
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333
3 changed files with 68 additions and 56 deletions

2
Cargo.lock generated
View File

@ -449,13 +449,13 @@ dependencies = [
"portable-pty", "portable-pty",
"rand", "rand",
"ratatui", "ratatui",
"regex",
"temp-dir", "temp-dir",
"textwrap", "textwrap",
"time", "time",
"tree-sitter-bash", "tree-sitter-bash",
"tree-sitter-highlight", "tree-sitter-highlight",
"tui-term", "tui-term",
"unicode-width 0.2.0",
"zips", "zips",
] ]

View File

@ -21,7 +21,6 @@ ratatui = "0.29.0"
tui-term = "0.2.0" tui-term = "0.2.0"
temp-dir = "0.1.14" temp-dir = "0.1.14"
time = { version = "0.3.36", features = ["local-offset", "macros", "formatting"] } time = { version = "0.3.36", features = ["local-offset", "macros", "formatting"] }
unicode-width = "0.2.0"
rand = { version = "0.8.5", optional = true } rand = { version = "0.8.5", optional = true }
linutil_core = { path = "../core", version = "24.9.28" } linutil_core = { path = "../core", version = "24.9.28" }
tree-sitter-highlight = "0.24.3" tree-sitter-highlight = "0.24.3"
@ -30,6 +29,7 @@ textwrap = "0.16.1"
anstyle = "1.0.8" anstyle = "1.0.8"
ansi-to-tui = "7.0.0" ansi-to-tui = "7.0.0"
zips = "0.1.7" zips = "0.1.7"
regex = { version = "1.3", default-features = false, features = ["std"] }
[[bin]] [[bin]]
name = "linutil" name = "linutil"

View File

@ -8,7 +8,7 @@ use ratatui::{
widgets::{Block, Borders, Paragraph}, widgets::{Block, Borders, Paragraph},
Frame, Frame,
}; };
use unicode_width::UnicodeWidthChar; use regex::RegexBuilder;
pub enum SearchAction { pub enum SearchAction {
None, None,
@ -17,7 +17,7 @@ pub enum SearchAction {
} }
pub struct Filter { pub struct Filter {
search_input: Vec<char>, search_input: String,
in_search_mode: bool, in_search_mode: bool,
input_position: usize, input_position: usize,
items: Vec<ListEntry>, items: Vec<ListEntry>,
@ -27,7 +27,7 @@ pub struct Filter {
impl Filter { impl Filter {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
search_input: vec![], search_input: String::new(),
in_search_mode: false, in_search_mode: false,
input_position: 0, input_position: 0,
items: vec![], items: vec![],
@ -62,47 +62,45 @@ impl Filter {
.collect(); .collect();
} else { } else {
self.items.clear(); self.items.clear();
if let Ok(regex) = self.regex_builder(&regex::escape(&self.search_input)) {
let query_lower = self.search_input.iter().collect::<String>().to_lowercase(); for tab in tabs {
for tab in tabs.iter() {
let mut stack = vec![tab.tree.root().id()]; let mut stack = vec![tab.tree.root().id()];
while let Some(node_id) = stack.pop() { while let Some(node_id) = stack.pop() {
let node = tab.tree.get(node_id).unwrap(); let node = tab.tree.get(node_id).unwrap();
if regex.is_match(&node.value().name) && !node.has_children() {
if node.value().name.to_lowercase().contains(&query_lower)
&& !node.has_children()
{
self.items.push(ListEntry { self.items.push(ListEntry {
node: node.value().clone(), node: node.value().clone(),
id: node.id(), id: node.id(),
has_children: false, has_children: false,
}); });
} }
stack.extend(node.children().map(|child| child.id())); stack.extend(node.children().map(|child| child.id()));
} }
} }
self.items.sort_by(|a, b| a.node.name.cmp(&b.node.name)); self.items
.sort_unstable_by(|a, b| a.node.name.cmp(&b.node.name));
} else {
self.search_input.clear();
}
} }
self.update_completion_preview(); self.update_completion_preview();
} }
fn update_completion_preview(&mut self) { fn update_completion_preview(&mut self) {
if self.search_input.is_empty() { self.completion_preview = if self.items.is_empty() || self.search_input.is_empty() {
self.completion_preview = None; None
return; } else {
} let pattern = format!("(?i)^{}", regex::escape(&self.search_input));
if let Ok(regex) = self.regex_builder(&pattern) {
let input = self.search_input.iter().collect::<String>().to_lowercase(); self.items.iter().find_map(|item| {
self.completion_preview = self.items.iter().find_map(|item| { regex
let item_name_lower = item.node.name.to_lowercase(); .find(&item.node.name)
if item_name_lower.starts_with(&input) { .map(|mat| item.node.name[mat.end()..].to_string())
Some(item_name_lower[input.len()..].to_string()) })
} else { } else {
None None
} }
}); }
} }
pub fn draw_searchbar(&self, frame: &mut Frame, area: Rect, theme: &Theme) { pub fn draw_searchbar(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
@ -110,8 +108,10 @@ impl Filter {
let display_text = if !self.in_search_mode && self.search_input.is_empty() { let display_text = if !self.in_search_mode && self.search_input.is_empty() {
Span::raw("Press / to search") Span::raw("Press / to search")
} else { } else {
let input_text = self.search_input.iter().collect::<String>(); Span::styled(
Span::styled(input_text, Style::default().fg(theme.focused_color())) &self.search_input,
Style::default().fg(theme.focused_color()),
)
}; };
let search_color = if self.in_search_mode { let search_color = if self.in_search_mode {
@ -135,25 +135,16 @@ impl Filter {
// Render cursor in search bar // Render cursor in search bar
if self.in_search_mode { if self.in_search_mode {
let cursor_position: usize = self.search_input[..self.input_position] let x = area.x + self.input_position as u16 + 1;
.iter()
.map(|c| c.width().unwrap_or(1))
.sum();
let x = area.x + cursor_position as u16 + 1;
let y = area.y + 1; let y = area.y + 1;
frame.set_cursor_position(Position::new(x, y)); frame.set_cursor_position(Position::new(x, y));
if let Some(preview) = &self.completion_preview { if let Some(preview) = &self.completion_preview {
let preview_x = area.x + self.search_input.len() as u16 + 1;
let preview_span = let preview_span =
Span::styled(preview, Style::default().fg(theme.search_preview_color())); Span::styled(preview, Style::default().fg(theme.search_preview_color()));
let preview_paragraph = Paragraph::new(preview_span).style(Style::default()); let preview_area = Rect::new(preview_x, y, preview.len() as u16, 1);
let preview_area = Rect::new( frame.render_widget(Paragraph::new(preview_span), preview_area);
x,
y,
(preview.len() as u16).min(area.width - cursor_position as u16 - 1),
1,
);
frame.render_widget(preview_paragraph, preview_area);
} }
} }
} }
@ -220,16 +211,37 @@ impl Filter {
} }
} }
fn regex_builder(&self, pattern: &str) -> Result<regex::Regex, regex::Error> {
RegexBuilder::new(pattern).case_insensitive(true).build()
}
fn complete_search(&mut self) -> SearchAction { fn complete_search(&mut self) -> SearchAction {
if let Some(completion) = self.completion_preview.take() { if self.completion_preview.is_none() {
self.search_input.extend(completion.chars()); SearchAction::None
} else {
let pattern = format!("(?i)^{}", self.search_input);
if let Ok(regex) = self.regex_builder(&pattern) {
self.search_input = self
.items
.iter()
.find_map(|item| {
if regex.is_match(&item.node.name) {
Some(item.node.name.clone())
} else {
None
}
})
.unwrap_or_default();
self.completion_preview = None;
self.input_position = self.search_input.len(); self.input_position = self.search_input.len();
self.update_completion_preview();
SearchAction::Update SearchAction::Update
} else { } else {
SearchAction::None SearchAction::None
} }
} }
}
pub fn clear_search(&mut self) { pub fn clear_search(&mut self) {
self.search_input.clear(); self.search_input.clear();