use std::any::Any;
use std::sync::Mutex;
use crate::component::Component;
use crate::theme::{theme, Theme};
use crate::utils::{truncate_to_width, visible_width};
pub struct SearchBar {
query: Mutex<String>,
match_count: Mutex<usize>,
current_match: Mutex<usize>,
visible: Mutex<bool>,
}
impl SearchBar {
pub fn new() -> Self {
Self {
query: Mutex::new(String::new()),
match_count: Mutex::new(0),
current_match: Mutex::new(0),
visible: Mutex::new(false),
}
}
pub fn set_query(&self, query: &str) {
*self.query.lock().unwrap() = query.to_string();
}
pub fn set_match_info(&self, current: usize, total: usize) {
*self.current_match.lock().unwrap() = current;
*self.match_count.lock().unwrap() = total;
}
pub fn set_visible(&self, visible: bool) {
*self.visible.lock().unwrap() = visible;
}
pub fn is_visible(&self) -> bool {
*self.visible.lock().unwrap()
}
fn render_with_theme(&self, width: usize, theme: &Theme) -> Vec<String> {
if !self.is_visible() || width == 0 {
return vec![];
}
let query = self.query.lock().unwrap().clone();
let match_count = *self.match_count.lock().unwrap();
let current_match = *self.current_match.lock().unwrap();
let colors = &theme.colors;
let search_icon = "🔍";
let match_info = if match_count == 0 {
if query.is_empty() {
String::new()
} else {
"No matches".to_string()
}
} else {
format!("{}/{}", current_match + 1, match_count)
};
let left = format!("{} Search: {}", search_icon, query);
let right = match_info;
let left_width = visible_width(&left);
let right_width = visible_width(&right);
let trailing = 2usize;
let line = if right.is_empty() {
let fitted_left = truncate_to_width(&left, width, "…");
let pad = width.saturating_sub(visible_width(&fitted_left));
format!("{}{}", colors.accent.fg(&fitted_left), " ".repeat(pad))
} else if left_width + right_width + trailing < width {
let padding = width - left_width - right_width - trailing;
format!(
"{}{}{}{}",
colors.accent.fg(&left),
" ".repeat(padding),
colors.dim.fg(&right),
" ".repeat(trailing)
)
} else if right_width + 1 < width {
let max_left = width - right_width - 1;
let fitted_left = truncate_to_width(&left, max_left, "…");
let padding = width - visible_width(&fitted_left) - right_width;
format!(
"{}{}{}",
colors.accent.fg(&fitted_left),
" ".repeat(padding),
colors.dim.fg(&right),
)
} else {
let fitted_left = truncate_to_width(&left, width, "…");
colors.accent.fg(&fitted_left)
};
vec![line]
}
}
impl Default for SearchBar {
fn default() -> Self {
Self::new()
}
}
impl Component for SearchBar {
fn render(&self, width: usize) -> Vec<String> {
self.render_with_theme(width, &theme())
}
fn invalidate(&self) {
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_search_bar_hidden() {
let bar = SearchBar::new();
assert_eq!(bar.render(80).len(), 0);
}
#[test]
fn test_search_bar_visible() {
let bar = SearchBar::new();
bar.set_visible(true);
bar.set_query("test");
bar.set_match_info(0, 5);
let lines = bar.render(80);
assert_eq!(lines.len(), 1);
assert!(lines[0].contains("test"));
assert!(lines[0].contains("1/5"));
assert!(visible_width(&lines[0]) <= 80);
}
#[test]
fn test_search_bar_cjk_width_and_foreground_color() {
let bar = SearchBar::new();
bar.set_visible(true);
bar.set_query("重构方案与视觉核查");
bar.set_match_info(1, 12);
let mut custom_theme = Theme::default();
custom_theme.colors.background = crate::theme::Color::Rgb(10, 20, 30);
for width in [80, 40, 24, 12, 4] {
let lines = bar.render_with_theme(width, &custom_theme);
assert_eq!(lines.len(), 1, "width={width}");
let rendered_width = visible_width(&lines[0]);
assert!(
rendered_width <= width,
"width={width}, rendered_width={rendered_width}, line={:?}",
lines[0]
);
assert!(
!lines[0].contains(&custom_theme.colors.background.to_fg()),
"search bar must not use background color as foreground: {:?}",
lines[0]
);
assert!(
lines[0].contains(&custom_theme.colors.accent.to_fg()),
"search bar should use a readable foreground token: {:?}",
lines[0]
);
}
let default_theme = Theme::default();
let default_lines = bar.render_with_theme(80, &default_theme);
assert_eq!(default_lines.len(), 1);
assert_eq!(visible_width(&default_lines[0]), 80);
assert!(!default_lines[0].contains(&default_theme.colors.background.to_fg()));
}
}