Merge branch 'main' into column_tabs

This commit is contained in:
Chris Titus 2024-08-15 15:26:29 -05:00 committed by GitHub
commit f8b3edb250
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 89 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -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,

81
src/search.rs Normal file
View File

@ -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()
}
}

View File

@ -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,
},
];