diff --git a/build/linutil b/build/linutil index a783b934..da735344 100755 Binary files a/build/linutil and b/build/linutil differ diff --git a/build/linutil-aarch64 b/build/linutil-aarch64 index 1801024b..7ab812da 100755 Binary files a/build/linutil-aarch64 and b/build/linutil-aarch64 differ diff --git a/src/main.rs b/src/main.rs index 7502b61b..0e93d9db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ mod float; mod floating_text; mod running_command; +mod search; pub mod state; mod tabs; mod theme; +use crate::search::SearchBar; use std::{ io::{self, stdout}, time::Duration, diff --git a/src/search.rs b/src/search.rs new file mode 100644 index 00000000..68827c24 --- /dev/null +++ b/src/search.rs @@ -0,0 +1,81 @@ +use crossterm::event::{KeyCode, KeyEvent}; +use ratatui::{ + layout::Rect, + style::Style, + text::Span, + widgets::{Block, Borders, Paragraph}, + Frame, +}; + +use crate::state::AppState; + +pub struct SearchBar { + search_input: String, + in_search_mode: bool, +} + +impl SearchBar { + pub fn new() -> Self { + SearchBar { + search_input: String::new(), + in_search_mode: false, + } + } + + pub fn activate_search(&mut self) { + self.in_search_mode = true; + } + + pub fn deactivate_search(&mut self) { + self.in_search_mode = false; + } + + pub fn is_search_active(&self) -> bool { + self.in_search_mode + } + + pub fn draw(&self, frame: &mut Frame, area: Rect, state: &AppState) { + //Set the search bar text (If empty use the placeholder) + let display_text = if !self.in_search_mode && self.search_input.is_empty() { + Span::raw("Press / to search") + } else { + Span::raw(&self.search_input) + }; + + //Create the search bar widget + let mut search_bar = Paragraph::new(display_text) + .block(Block::default().borders(Borders::ALL).title("Search")) + .style(Style::default().fg(state.theme.unfocused_color)); + + //Change the color if in search mode + if self.in_search_mode { + search_bar = search_bar + .clone() + .style(Style::default().fg(state.theme.focused_color)); + } + + //Render the search bar (First chunk of the screen) + frame.render_widget(search_bar, area); + } + + pub fn handle_key(&mut self, event: KeyEvent) -> String { + //Insert user input into the search bar + match event.code { + KeyCode::Char(c) => { + self.search_input.push(c); + } + KeyCode::Backspace => { + self.search_input.pop(); + } + KeyCode::Esc => { + self.search_input = String::new(); + self.in_search_mode = false; + } + KeyCode::Enter => { + self.in_search_mode = false; + } + _ => {} + } + self.search_input.clone() + } +} diff --git a/src/theme.rs b/src/theme.rs index 149dbece..0ba0c77d 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -10,6 +10,8 @@ pub struct Theme { pub tab_icon: &'static str, pub success_color: Color, pub fail_color: Color, + pub focused_color: Color, + pub unfocused_color: Color, } pub const THEMES: [Theme; 2] = [ @@ -22,6 +24,8 @@ pub const THEMES: [Theme; 2] = [ tab_icon: ">> ", success_color: Color::Green, fail_color: Color::Red, + focused_color: Color::LightBlue, + unfocused_color: Color::Gray, }, Theme { dir_color: Color::Blue, @@ -32,5 +36,7 @@ pub const THEMES: [Theme; 2] = [ tab_icon: " ", fail_color: Color::Rgb(199, 55, 44), success_color: Color::Rgb(5, 255, 55), + focused_color: Color::LightBlue, + unfocused_color: Color::Gray, }, ];