diff --git a/README.md b/README.md index acc10bc..79c65cd 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ In application controls, these, amongst many other settings, can be customized w | ```( 1-9 )``` | Sort containers by heading, clicking on headings also sorts the selected column. | | ```( 0 )``` | Stop sorting.| | ```( F1 )``` or ```( / )``` | Enter filter mode. | +| ```( # )``` | Enter log search mode. | | ```( - ) ``` or ```(=)``` | Reduce or increase the height of the logs panel.| | ```( \ )``` | Toggle the visibility of the logs panel.| | ```( e )``` | Exec into the selected container - not available on Windows.| diff --git a/example_config/example.config.jsonc b/example_config/example.config.jsonc index af401dd..3b1f8fd 100644 --- a/example_config/example.config.jsonc +++ b/example_config/example.config.jsonc @@ -31,6 +31,8 @@ "use_cli": false, // Show the logs section - this can be changed during operation with the log_section_toggle key "show_logs": true, + // Use case-sensitive matching for logs + "log_search_case_sensitive": true, ////////////////// // Custom Keymap // ////////////////// @@ -67,6 +69,10 @@ "/", "F1" ], + // Enter log search mode + "log_search_mode": [ + "#" + ], // Quit at anytime "quit": [ "q" @@ -85,7 +91,9 @@ "end" ], // Modifier to scroll by 10 lines instead of one, used in conjunction with scroll_up/scroll_down - "scroll_many": ["control"], + "scroll_many": [ + "control" + ], // Scroll up to the start of a list "scroll_start": [ "home" @@ -285,6 +293,17 @@ // Highlighted text color "highlight": "magenta" }, + // The log search panel + "log_search": { + // Background color of panel + "background": "reset", + // color of text + "text": "gray", + // text color of the buttons text + "button_text": "black", + // Highlighted text color + "highlight": "magenta" + }, // The logs panel, will only be applied if color_logs is false "logs": { // Background color of panel diff --git a/example_config/example.config.toml b/example_config/example.config.toml index 79419a7..8d5f1db 100644 --- a/example_config/example.config.toml +++ b/example_config/example.config.toml @@ -43,6 +43,9 @@ use_cli = false # Show the logs section - this can be changed during operation with the log_section_toggle key show_logs = true +# Use case-sensitive matching for logs +log_search_case_sensitive = true + ################# # Custom Keymap # ################# @@ -72,6 +75,10 @@ delete_confirm = ["y"] exec = ["e"] # Enter filter mode filter_mode = ["/", "F1"] + +# Enter log search mode +log_search_mode = ["#"] + # Quit at anytime quit = ["q"] # Save logs of selected container to file on disk @@ -115,6 +122,9 @@ log_section_height_decrease = ["-"] log_section_height_increase = ["+"] # Toggle visibility of the log section log_section_toggle = ["\\"] + + + # Force a complete clear & redraw of the screen force_redraw = ["f"] @@ -192,6 +202,17 @@ selected_filter_text = "black" highlight = "magenta" +# The log search panel +[colors.log_search] +# Background color of panel +background = "reset" +# color of text +text = "gray" +# text color of the buttons text +button_text = "black" +# Highlighted text color +highlight = "magenta" + # The color the of Docker commands available for each container [colors.commands] # Background color of panel diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs index d31550e..aa6961c 100644 --- a/src/app_data/container_state.rs +++ b/src/app_data/container_state.rs @@ -22,6 +22,12 @@ const ONE_KB: f64 = 1000.0; const ONE_MB: f64 = ONE_KB * 1000.0; const ONE_GB: f64 = ONE_MB * 1000.0; +#[derive(Debug, Clone, Eq, Hash, PartialEq)] +pub enum ScrollDirection { + Next, + Previous, +} + #[derive(Debug, Clone, Eq, Hash, PartialEq)] pub struct ContainerId(String); @@ -177,7 +183,14 @@ impl StatefulList { self.state.select(Some(0)); } - pub fn next(&mut self) { + pub fn scroll(&mut self, scroll: &ScrollDirection) { + match scroll { + ScrollDirection::Next => self.next(), + ScrollDirection::Previous => self.previous(), + } + } + + fn next(&mut self) { if !self.items.is_empty() { self.state.select(Some( self.state.selected().map_or( @@ -190,7 +203,7 @@ impl StatefulList { } } - pub fn previous(&mut self) { + fn previous(&mut self) { if !self.items.is_empty() { self.state.select(Some( self.state @@ -600,6 +613,8 @@ impl LogsTz { pub struct Logs { lines: StatefulList>, tz: HashSet, + search_results: Vec, + search_term: Option, offset: u16, max_log_len: usize, adjusted_max_width: usize, @@ -614,6 +629,8 @@ impl Default for Logs { lines, tz: HashSet::new(), offset: 0, + search_term: None, + search_results: vec![], adjusted_max_width: 0, adjust_max_width_text_len: 0, max_log_len: 0, @@ -621,12 +638,189 @@ impl Default for Logs { } } +#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] +pub enum LogsButton { + Both, + Next, + Previous, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)] +pub struct LogSearch { + pub term: Option, + pub result: Option, + pub buttons: Option, +} + +/// LogSearch is used in FrameData +impl From<&Logs> for LogSearch { + fn from(l: &Logs) -> Self { + let buttons = l.lines.state.selected().as_ref().and_then(|x| { + let show_next = l.search_results.iter().any(|n| n > x); + let show_previous = l.search_results.iter().any(|n| n < x); + match (show_next, show_previous) { + (true, true) => Some(LogsButton::Both), + (true, false) => Some(LogsButton::Next), + (false, true) => Some(LogsButton::Previous), + (false, false) => None, + } + }); + Self { + term: l.search_term.clone(), + result: l.get_search_result(), + buttons, + } + } +} + impl Logs { + pub fn gen_log_search(&self) -> LogSearch { + LogSearch::from(self) + } + + /// Scroll to the next or previous search result, accounts for when currently selected line isn't in the results vec + pub fn search_scroll(&mut self, sd: &ScrollDirection) -> Option<()> { + if let Some(current_selected) = self.lines.state.selected() { + if let Some(current_position) = self + .search_results + .iter() + .position(|i| i == ¤t_selected) + { + if let Some(new_index) = match sd { + ScrollDirection::Next => current_position.checked_add(1), + ScrollDirection::Previous => current_position.checked_sub(1), + } { + if let Some(f) = self.search_results.get(new_index) { + self.lines.state.select(Some(*f)); + return Some(()); + } + } + } else { + let range = match sd { + ScrollDirection::Previous => (0..=current_selected).rev().collect::>(), + ScrollDirection::Next => (current_selected + ..=self + .search_results + .last() + .map_or_else(|| current_selected, |i| *i)) + .collect::>(), + }; + for i in range { + if self.search_results.contains(&i) { + self.lines.state.select(Some(i)); + return Some(()); + } + } + } + } + None + } + + /// Get a string x/y, where y is total matches found, and x is current ordered selected line + /// WIll be padded by max chars of total matches + fn get_search_result(&self) -> Option { + if self.search_results.is_empty() { + return None; + } + Some(self.lines.state.selected().map_or_else( + || format!("{}", self.search_results.len()), + |current_index| { + self.search_results + .iter() + .position(|i| i == ¤t_index) + .map_or_else( + || format!("{}", self.search_results.len()), + |index| { + let len = format!("{}", self.search_results.len()); + let len_width = len.chars().count(); + format!("{:>len_width$}/{len:>len_width$}", index + 1) + }, + ) + }, + )) + } + + /// Search through the logs for a matching string + pub fn search(&mut self, case_sensitive: bool) { + if let Some(search_term) = self.search_term.as_ref() { + let term = if case_sensitive { + search_term.to_owned() + } else { + search_term.to_lowercase() + }; + self.search_results = self + .lines + .items + .iter() + .enumerate() + .filter_map(|(index, a)| { + a.lines + .iter() + .any(|b| { + b.spans.iter().any(|c| { + if case_sensitive { + c.content.contains(&term) + } else { + c.content.to_lowercase().contains(&term) + } + }) + }) + .then_some(index) + }) + .collect(); + + if !self.search_results.is_empty() { + if let Some(currently_selected) = self.lines.state.selected() + && !self.search_results.contains(¤tly_selected) + { + self.lines.state.select(self.search_results.last().copied()); + self.offset = 0; + } else { + self.lines.state.select(self.search_results.last().copied()); + self.offset = 0; + } + } + } else { + self.search_results.clear(); + } + } + + /// Set a single char into the filter term + pub fn search_term_push(&mut self, c: char, case_sensitive: bool) { + if let Some(term) = self.search_term.as_mut() { + term.push(c); + } else { + self.search_term = Some(format!("{c}")); + } + self.search(case_sensitive); + } + + /// Delete the final char of the filter term + pub fn search_term_pop(&mut self, case_sensitive: bool) { + if let Some(term) = self.search_term.as_mut() { + term.pop(); + if term.is_empty() { + self.search_term = None; + } + } + self.search(case_sensitive); + } + + /// Remove the filter completely + pub fn search_term_clear(&mut self) { + self.search_term = None; + self.search_results.clear(); + } + /// Only allow a new log line to be inserted if the log timestamp isn't in the tz HashSet - pub fn insert(&mut self, line: Text<'static>, tz: LogsTz) { + pub fn insert(&mut self, line: Text<'static>, tz: LogsTz, case_sensitive: bool) { if self.tz.insert(tz) { self.max_log_len = self.max_log_len.max(line.width()); self.lines.items.push(line); + // Maybe - Ideally we'd re-render here + if self.search_term.is_some() { + self.search(case_sensitive); + } } } @@ -748,21 +942,27 @@ impl Logs { self.offset = self.offset.saturating_sub(1); } + /// Scroll lines down by one pub fn next(&mut self) { self.lines.next(); } + /// Scroll lines up by one pub fn previous(&mut self) { self.lines.previous(); } + /// Go to the end of the lines pub fn end(&mut self) { self.lines.end(); } + + /// Go to the start of the lines pub fn start(&mut self) { self.lines.start(); } + /// Get total number of log lines pub const fn len(&self) -> usize { self.lines.items.len() } @@ -938,7 +1138,7 @@ mod tests { }; use crate::{ - app_data::{ContainerImage, Logs, LogsTz, RunningState}, + app_data::{ContainerImage, LogSearch, Logs, LogsTz, RunningState}, ui::log_sanitizer, }; @@ -1075,9 +1275,9 @@ mod tests { let mut logs = Logs::default(); let line = log_sanitizer::remove_ansi(input); - logs.insert(Text::from(line.clone()), tz.clone()); - logs.insert(Text::from(line.clone()), tz.clone()); - logs.insert(Text::from(line), tz); + logs.insert(Text::from(line.clone()), tz.clone(), true); + logs.insert(Text::from(line.clone()), tz.clone(), true); + logs.insert(Text::from(line), tz, true); assert_eq!(logs.lines.items.len(), 1); @@ -1085,9 +1285,9 @@ mod tests { let (tz, _) = LogsTz::splitter(input); let line = log_sanitizer::remove_ansi(input); - logs.insert(Text::from(line.clone()), tz.clone()); - logs.insert(Text::from(line.clone()), tz.clone()); - logs.insert(Text::from(line), tz); + logs.insert(Text::from(line.clone()), tz.clone(), true); + logs.insert(Text::from(line.clone()), tz.clone(), true); + logs.insert(Text::from(line), tz, true); assert_eq!(logs.lines.items.len(), 2); } @@ -1150,15 +1350,15 @@ mod tests { let input = "2023-01-14T19:13:30.783138328Z Hello world some long line".to_owned(); let (tz, _) = LogsTz::splitter(&input); - logs.insert(Text::from(input), tz); + logs.insert(Text::from(input), tz, true); let input = "2023-01-14T19:13:31.783138328Z Hello world some line".to_owned(); let (tz, _) = LogsTz::splitter(&input); - logs.insert(Text::from(input), tz); + logs.insert(Text::from(input), tz, true); let input = "2023-01-14T19:13:32.783138328Z Hello world".to_owned(); let (tz, _) = LogsTz::splitter(&input); - logs.insert(Text::from(input), tz); + logs.insert(Text::from(input), tz, true); logs.offset = 43; let result = logs.get_visible_logs( @@ -1188,14 +1388,14 @@ mod tests { let input = "short".to_owned(); let (tz, _) = LogsTz::splitter(&input); - logs.insert(Text::from(input), tz); + logs.insert(Text::from(input), tz, true); let result = logs.get_scroll_title(10); assert!(result.is_none()); let input = "2023-01-14T19:13:30.783138328Z Hello world some long line".to_owned(); let (tz, _) = LogsTz::splitter(&input); - logs.insert(Text::from(input), tz); + logs.insert(Text::from(input), tz, true); let result = logs.get_scroll_title(10); assert_eq!(result, Some(" 0/51 → ".to_owned())); @@ -1211,4 +1411,119 @@ mod tests { let result = logs.get_scroll_title(10); assert_eq!(result, Some(" ← 51/51 ".to_owned())); } + + #[test] + /// Test the log search + fn test_logsearch() { + let mut logs = Logs::default(); + + for i in 1..=10 { + let input = if i % 2 == 0 { + format!("{i}, hello world some long line {i}") + } else { + format!("{i}, Hello world some long line {i}") + }; + let (tz, _) = LogsTz::splitter(&input); + logs.insert(Text::from(input), tz, true); + } + + logs.search_term_push('H', true); + assert_eq!(logs.search_results, [0, 2, 4, 6, 8]); + logs.search_term_clear(); + logs.search_term_push('H', false); + assert_eq!(logs.search_results, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + } + + #[test] + /// Test the LogSearch::From() methods + fn test_logsearch_from() { + let mut logs = Logs::default(); + + for i in 1..=10 { + let input = format!("{i}, Hello world some long line {i}"); + let (tz, _) = LogsTz::splitter(&input); + logs.insert(Text::from(input), tz, true); + } + + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: None, + result: None, + buttons: None + } + ); + + logs.search_term_push('H', true); + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: Some("H".to_owned()), + result: Some("10/10".to_owned()), + buttons: Some(crate::app_data::LogsButton::Previous) + } + ); + + logs.previous(); + + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: Some("H".to_owned()), + result: Some(" 9/10".to_owned()), + buttons: Some(crate::app_data::LogsButton::Both) + } + ); + + logs.start(); + + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: Some("H".to_owned()), + result: Some(" 1/10".to_owned()), + buttons: Some(crate::app_data::LogsButton::Next) + } + ); + + logs.search_term_push('H', true); + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: Some("HH".to_owned()), + result: None, + buttons: None + } + ); + + logs.search_term_clear(); + logs.search_term_push('2', true); + let log_search = LogSearch::from(&logs); + assert_eq!(logs.lines.state.selected(), Some(1)); + assert_eq!( + log_search, + LogSearch { + term: Some("2".to_owned()), + result: Some("1/1".to_owned()), + buttons: None + } + ); + + logs.next(); + + let log_search = LogSearch::from(&logs); + assert_eq!( + log_search, + LogSearch { + term: Some("2".to_owned()), + result: Some("1".to_owned()), + buttons: Some(crate::app_data::LogsButton::Previous) + } + ); + } } diff --git a/src/app_data/mod.rs b/src/app_data/mod.rs index ab7993e..cf306ae 100644 --- a/src/app_data/mod.rs +++ b/src/app_data/mod.rs @@ -171,6 +171,19 @@ impl AppData { (self.filter.by, self.filter.term.as_ref()) } + pub fn log_search_scroll(&mut self, np: &ScrollDirection) { + if let Some(i) = self.get_mut_selected_container() { + if i.logs.search_scroll(np).is_some() { + self.rerender.update_draw(); + } + } + } + + pub fn gen_log_search(&self) -> Option { + self.get_selected_container() + .map(|i| i.logs.gen_log_search()) + } + /// Check if a given container can be inserted into the "visible" list, based on current filter term and filter_by fn can_insert(&self, container: &ContainerItem) -> bool { self.filter.term.as_ref().is_none_or(|term| { @@ -224,6 +237,31 @@ impl AppData { } } + pub fn logs_search_clear(&mut self) { + if let Some(selected_container) = self.get_mut_selected_container() { + selected_container.logs.search_term_clear(); + self.rerender.update_draw(); + } + } + + /// Set a single char into the filter term + pub fn log_search_push(&mut self, c: char) { + let cs = self.config.log_search_case_sensitive; + if let Some(selected_container) = self.get_mut_selected_container() { + selected_container.logs.search_term_push(c, cs); + self.rerender.update_draw(); + } + } + + /// Delete the final char of the filter term + pub fn log_search_pop(&mut self) { + let cs = self.config.log_search_case_sensitive; + if let Some(selected_container) = self.get_mut_selected_container() { + selected_container.logs.search_term_pop(cs); + self.rerender.update_draw(); + } + } + /// Re-filter the containers, used after the filter.by has been changed fn re_filter(&mut self) { self.containers.items.append(&mut self.hidden_containers); @@ -448,15 +486,8 @@ impl AppData { self.rerender.update_draw(); } - /// Select the next container - pub fn containers_next(&mut self) { - self.containers.next(); - self.rerender.update_draw(); - } - - /// select the previous container - pub fn containers_previous(&mut self) { - self.containers.previous(); + pub fn containers_scroll(&mut self, scroll: &ScrollDirection) { + self.containers.scroll(scroll); self.rerender.update_draw(); } @@ -576,17 +607,10 @@ impl AppData { } /// Change selected choice of docker commands of selected container - pub fn docker_controls_next(&mut self) { + pub fn docker_controls_scroll(&mut self, scroll: &ScrollDirection) { if let Some(i) = self.get_mut_selected_container() { - i.docker_controls.next(); - self.rerender.update_draw(); - } - } - - /// Change selected choice of docker commands of selected container - pub fn docker_controls_previous(&mut self) { - if let Some(i) = self.get_mut_selected_container() { - i.docker_controls.previous(); + i.docker_controls.scroll(scroll); + // i.docker_controls.next(); self.rerender.update_draw(); } } @@ -643,34 +667,30 @@ impl AppData { .and_then(|i| i.logs.get_scroll_title(width)) } - /// Increase the logs offset, basically moving an invisible cursor back - pub fn log_back(&mut self) { - if let Some(i) = self.get_mut_selected_container() { - i.logs.back(); - self.rerender.update_draw(); - } - } - - /// Increase the logs offset, basically moving an invisible cursor forward - pub fn log_forward(&mut self, width: u16) { - if let Some(i) = self.get_mut_selected_container() { - i.logs.forward(width); - self.rerender.update_draw(); + pub fn logs_horizontal_scroll(&mut self, sd: &ScrollDirection, width: u16) { + match sd { + ScrollDirection::Next => { + if let Some(i) = self.get_mut_selected_container() { + i.logs.forward(width); + self.rerender.update_draw(); + } + } + ScrollDirection::Previous => { + if let Some(i) = self.get_mut_selected_container() { + i.logs.back(); + self.rerender.update_draw(); + } + } } } /// select next selected log line - pub fn log_next(&mut self) { + pub fn log_scroll(&mut self, scroll: &ScrollDirection) { if let Some(i) = self.get_mut_selected_container() { - i.logs.next(); - self.rerender.update_draw(); - } - } - - /// select previous selected log line - pub fn log_previous(&mut self) { - if let Some(i) = self.get_mut_selected_container() { - i.logs.previous(); + match scroll { + ScrollDirection::Next => i.logs.next(), + ScrollDirection::Previous => i.logs.previous(), + } self.rerender.update_draw(); } } @@ -857,7 +877,7 @@ impl AppData { // If removed container is currently selected, then change selected to previous // This will default to 0 in any edge cases if self.containers.state.selected().is_some() { - self.containers.previous(); + self.containers.scroll(&ScrollDirection::Previous); } // Check is some, else can cause out of bounds error, if containers get removed before a docker update if self.containers.items.get(index).is_some() { @@ -959,6 +979,8 @@ impl AppData { let format = self.config.timestamp_format.clone(); let config_tz = self.config.timezone.clone(); + let cs = self.config.log_search_case_sensitive; + let show_timestamp = self.config.show_timestamp; if let Some(container) = self.get_any_container_by_id(id) { @@ -985,7 +1007,7 @@ impl AppData { } else { log_sanitizer::remove_ansi(&i) }; - container.logs.insert(Text::from(lines), log_tz); + container.logs.insert(Text::from(lines), log_tz, cs); } // Set the logs selected row for each container @@ -1422,7 +1444,7 @@ mod tests { ); // Calling previous when at start has no effect - app_data.containers_previous(); + app_data.containers_scroll(&ScrollDirection::Previous); let result = app_data.get_selected_container_id(); assert_eq!(result, Some(ContainerId::from("1"))); let result = app_data.get_selected_container_id_state_name(); @@ -1445,7 +1467,7 @@ mod tests { // Advance list state by 1 app_data.containers_start(); - app_data.containers_next(); + app_data.containers.scroll(&ScrollDirection::Next); let result = app_data.get_container_state(); assert_eq!(result.selected(), Some(1)); @@ -1489,7 +1511,7 @@ mod tests { ); // Calling previous when at end has no effect - app_data.containers_next(); + app_data.containers.scroll(&ScrollDirection::Next); let result = app_data.get_selected_container_id(); assert_eq!(result, Some(ContainerId::from("3"))); let result = app_data.get_selected_container_id_state_name(); @@ -1510,7 +1532,7 @@ mod tests { let mut app_data = gen_appdata(&containers); app_data.containers_end(); - app_data.containers_previous(); + app_data.containers.scroll(&ScrollDirection::Previous); let result = app_data.get_container_state(); assert_eq!(result.selected(), Some(1)); assert_eq!(result.offset(), 0); @@ -1526,7 +1548,7 @@ mod tests { assert_eq!(result, None); app_data.containers.start(); - app_data.containers.next(); + app_data.containers.scroll(&ScrollDirection::Next); let result = app_data.get_selected_container(); assert_eq!(result, Some(&containers[1])); @@ -1613,7 +1635,7 @@ mod tests { let mut app_data = gen_appdata(&containers); app_data.containers_start(); app_data.docker_controls_start(); - app_data.docker_controls_next(); + app_data.docker_controls_scroll(&ScrollDirection::Next); let result = app_data.selected_docker_controls(); assert_eq!(result, Some(DockerCommand::Restart)); @@ -1631,7 +1653,7 @@ mod tests { assert_eq!(result, Some(DockerCommand::Delete)); // Next has no effect when at end - app_data.docker_controls_next(); + app_data.docker_controls_scroll(&ScrollDirection::Next); let result = app_data.selected_docker_controls(); assert_eq!(result, Some(DockerCommand::Delete)); } @@ -1643,14 +1665,14 @@ mod tests { let mut app_data = gen_appdata(&containers); app_data.containers_start(); app_data.docker_controls_end(); - app_data.docker_controls_previous(); + app_data.docker_controls_scroll(&ScrollDirection::Previous); let result = app_data.selected_docker_controls(); assert_eq!(result, Some(DockerCommand::Stop)); // previous has no effect when at start app_data.docker_controls_start(); - app_data.docker_controls_previous(); + app_data.docker_controls_scroll(&ScrollDirection::Previous); let result = app_data.selected_docker_controls(); assert_eq!(result, Some(DockerCommand::Pause)); } @@ -1914,7 +1936,7 @@ mod tests { assert_eq!(result, " 3/3 - container_1 - image_1"); // Change log state to no longer be at the end - app_data.log_previous(); + app_data.log_scroll(&ScrollDirection::Previous); let result = app_data.get_log_title(); assert_eq!(result, " 2/3 - container_1 - image_1"); } @@ -1935,7 +1957,7 @@ mod tests { assert_eq!(result, " - container_1 - image_1"); // change container - app_data.containers_next(); + app_data.containers_scroll(&ScrollDirection::Next); let result = app_data.get_log_title(); assert_eq!(result, " - container_2 - image_2"); @@ -1946,7 +1968,7 @@ mod tests { assert_eq!(result, " 3/3 - container_2 - image_2"); // Change log state to no longer be at the end - app_data.log_previous(); + app_data.log_scroll(&ScrollDirection::Previous); let result = app_data.get_log_title(); assert_eq!(result, " 2/3 - container_2 - image_2"); } @@ -2053,8 +2075,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 1/3 - container_1 - image_1"); - app_data.log_next(); - + app_data.log_scroll(&ScrollDirection::Next); let result = app_data.get_log_state(); assert!(result.is_some()); assert_eq!(result.as_ref().unwrap().selected(), Some(1)); @@ -2063,7 +2084,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 2/3 - container_1 - image_1"); - app_data.log_next(); + app_data.log_scroll(&ScrollDirection::Next); let result = app_data.get_log_state(); assert!(result.is_some()); assert_eq!(result.as_ref().unwrap().selected(), Some(2)); @@ -2071,7 +2092,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 3/3 - container_1 - image_1"); - app_data.log_next(); + app_data.log_scroll(&ScrollDirection::Next); let result = app_data.get_log_state(); assert!(result.is_some()); @@ -2102,7 +2123,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 3/3 - container_1 - image_1"); - app_data.log_previous(); + app_data.log_scroll(&ScrollDirection::Previous); let result = app_data.get_log_state(); assert!(result.is_some()); @@ -2111,7 +2132,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 2/3 - container_1 - image_1"); - app_data.log_previous(); + app_data.log_scroll(&ScrollDirection::Previous); let result = app_data.get_log_state(); assert!(result.is_some()); assert_eq!(result.as_ref().unwrap().selected(), Some(0)); @@ -2119,7 +2140,7 @@ mod tests { let result = app_data.get_log_title(); assert_eq!(result, " 1/3 - container_1 - image_1"); - app_data.log_previous(); + app_data.log_scroll(&ScrollDirection::Previous); let result = app_data.get_log_state(); assert!(result.is_some()); assert_eq!(result.as_ref().unwrap().selected(), Some(0)); @@ -2413,7 +2434,7 @@ mod tests { } for _ in 0..=500 { - app_data.log_next(); + app_data.log_scroll(&ScrollDirection::Next); } let result = app_data.get_logs( Size { diff --git a/src/config/color_parser.rs b/src/config/color_parser.rs index 1599637..87dacd7 100644 --- a/src/config/color_parser.rs +++ b/src/config/color_parser.rs @@ -89,6 +89,24 @@ impl From> for AppColors { Self::map_color(fc.text.as_deref(), &mut app_colors.filter.text); } + // Log search + if let Some(ls) = config_colors.log_search { + Self::map_color( + ls.background.as_deref(), + &mut app_colors.log_search.background, + ); + Self::map_color( + ls.highlight.as_deref(), + &mut app_colors.log_search.highlight, + ); + + Self::map_color( + ls.button_text.as_deref(), + &mut app_colors.log_search.button_text, + ); + Self::map_color(ls.text.as_deref(), &mut app_colors.log_search.text); + } + // Help Popup if let Some(hp) = config_colors.popup_help { Self::map_color( @@ -238,6 +256,7 @@ optional_config_struct!( ConfigContainers, background, icon, text, text_rx, text_tx; ConfigContainerState, background, dead, exited, paused, removing, restarting, running_healthy, running_unhealthy, unknown; ConfigFilter, background, text, selected_filter_background, selected_filter_text, highlight; + ConfigLogSearch, background, text, button_text, highlight; ConfigHeadersBar, background, loading_spinner, text, text_selected; ConfigLogs, background, text ); @@ -251,6 +270,7 @@ config_struct!( Containers, background, icon, text, text_rx, text_tx; ContainerState, dead, exited, paused, removing, restarting, running_healthy, running_unhealthy, unknown; Filter, background, text, selected_filter_background, selected_filter_text, highlight; + LogSearch, background, text, button_text, highlight; HeadersBar, background, text_selected, loading_spinner, text; Logs, background, text; PopupDelete, background, text, text_highlight; @@ -269,6 +289,7 @@ pub struct ConfigColors { container_state: Option, containers: Option, filter: Option, + log_search: Option, headers_bar: Option, logs: Option, popup_delete: Option, @@ -397,6 +418,18 @@ impl Filter { } } +/// Default colours for the log search +impl LogSearch { + const fn new() -> Self { + Self { + background: Color::Reset, + highlight: Color::Magenta, + button_text: Color::Black, + text: Color::Gray, + } + } +} + /// Default colours for the logs panel, only applied if color_logs is false impl Logs { const fn new() -> Self { @@ -458,6 +491,7 @@ pub struct AppColors { pub commands: Commands, pub container_state: ContainerState, pub containers: Containers, + pub log_search: LogSearch, pub filter: Filter, pub headers_bar: HeadersBar, pub logs: Logs, @@ -477,6 +511,7 @@ impl AppColors { commands: Commands::new(), container_state: ContainerState::new(), containers: Containers::new(), + log_search: LogSearch::new(), filter: Filter::new(), headers_bar: HeadersBar::new(), logs: Logs::new(), diff --git a/src/config/config.toml b/src/config/config.toml index 79419a7..8d5f1db 100644 --- a/src/config/config.toml +++ b/src/config/config.toml @@ -43,6 +43,9 @@ use_cli = false # Show the logs section - this can be changed during operation with the log_section_toggle key show_logs = true +# Use case-sensitive matching for logs +log_search_case_sensitive = true + ################# # Custom Keymap # ################# @@ -72,6 +75,10 @@ delete_confirm = ["y"] exec = ["e"] # Enter filter mode filter_mode = ["/", "F1"] + +# Enter log search mode +log_search_mode = ["#"] + # Quit at anytime quit = ["q"] # Save logs of selected container to file on disk @@ -115,6 +122,9 @@ log_section_height_decrease = ["-"] log_section_height_increase = ["+"] # Toggle visibility of the log section log_section_toggle = ["\\"] + + + # Force a complete clear & redraw of the screen force_redraw = ["f"] @@ -192,6 +202,17 @@ selected_filter_text = "black" highlight = "magenta" +# The log search panel +[colors.log_search] +# Background color of panel +background = "reset" +# color of text +text = "gray" +# text color of the buttons text +button_text = "black" +# Highlighted text color +highlight = "magenta" + # The color the of Docker commands available for each container [colors.commands] # Background color of panel diff --git a/src/config/keymap_parser.rs b/src/config/keymap_parser.rs index f4d463a..f75ff16 100644 --- a/src/config/keymap_parser.rs +++ b/src/config/keymap_parser.rs @@ -37,16 +37,17 @@ macro_rules! config_struct { optional_config_struct!( ConfigKeymap, clear, - force_redraw, - delete_deny, delete_confirm, + delete_deny, exec, filter_mode, - log_section_height_increase, - log_section_height_decrease, - log_section_toggle, - log_scroll_forward, + force_redraw, log_scroll_back, + log_scroll_forward, + log_search_mode, + log_section_height_decrease, + log_section_height_increase, + log_section_toggle, quit, save_logs, scroll_down, @@ -55,14 +56,14 @@ optional_config_struct!( scroll_up, select_next_panel, select_previous_panel, - sort_by_name, - sort_by_state, - sort_by_status, sort_by_cpu, - sort_by_memory, sort_by_id, sort_by_image, + sort_by_memory, + sort_by_name, sort_by_rx, + sort_by_state, + sort_by_status, sort_by_tx, sort_reset, toggle_help, @@ -72,16 +73,17 @@ optional_config_struct!( config_struct!( Keymap, clear, - delete_deny, delete_confirm, + delete_deny, exec, filter_mode, force_redraw, - log_section_height_increase, - log_section_height_decrease, - log_section_toggle, - log_scroll_forward, log_scroll_back, + log_scroll_forward, + log_search_mode, + log_section_height_decrease, + log_section_height_increase, + log_section_toggle, quit, save_logs, scroll_down, @@ -90,14 +92,14 @@ config_struct!( scroll_up, select_next_panel, select_previous_panel, - sort_by_name, - sort_by_state, - sort_by_status, sort_by_cpu, - sort_by_memory, sort_by_id, sort_by_image, + sort_by_memory, + sort_by_name, sort_by_rx, + sort_by_state, + sort_by_status, sort_by_tx, sort_reset, toggle_help, @@ -113,19 +115,18 @@ impl Keymap { exec: (KeyCode::Char('e'), None), filter_mode: (KeyCode::Char('/'), Some(KeyCode::F(1))), force_redraw: (KeyCode::Char('f'), None), + log_scroll_back: (KeyCode::Left, None), + log_scroll_forward: (KeyCode::Right, None), + log_search_mode: (KeyCode::Char('#'), None), log_section_height_decrease: (KeyCode::Char('-'), None), log_section_height_increase: (KeyCode::Char('='), None), log_section_toggle: (KeyCode::Char('\\'), None), - log_scroll_back: (KeyCode::Left, None), - log_scroll_forward: (KeyCode::Right, None), quit: (KeyCode::Char('q'), None), save_logs: (KeyCode::Char('s'), None), - // TODO rename in next release scroll_down: (KeyCode::Down, Some(KeyCode::Char('j'))), scroll_end: (KeyCode::End, None), - scroll_start: (KeyCode::Home, None), scroll_many: KeyModifiers::CONTROL, - // TODO rename in next release + scroll_start: (KeyCode::Home, None), scroll_up: (KeyCode::Up, Some(KeyCode::Char('k'))), select_next_panel: (KeyCode::Tab, None), select_previous_panel: (KeyCode::BackTab, None), @@ -204,6 +205,7 @@ impl From> for Keymap { update_keymap(ck.scroll_end, &mut keymap.scroll_end, &mut clash); update_keymap(ck.scroll_start, &mut keymap.scroll_start, &mut clash); update_keymap(ck.scroll_up, &mut keymap.scroll_up, &mut clash); + update_keymap(ck.log_search_mode, &mut keymap.log_search_mode, &mut clash); update_keymap( ck.log_scroll_forward, &mut keymap.log_scroll_forward, @@ -394,6 +396,7 @@ mod tests { filter_mode: None, force_redraw: None, log_scroll_back: None, + log_search_mode: None, log_scroll_forward: None, log_section_height_decrease: None, log_section_height_increase: None, @@ -434,14 +437,15 @@ mod tests { let input = ConfigKeymap { clear: gen_v(("a", "b")), delete_confirm: gen_v(("c", "d")), - delete_deny: gen_v(("e", "fd")), + delete_deny: gen_v(("e", "f")), exec: gen_v(("g", "h")), filter_mode: gen_v(("i", "j")), force_redraw: gen_v(("k", "l")), + log_scroll_back: gen_v(("s", "t")), + log_scroll_forward: gen_v(("q", "r")), + log_search_mode: gen_v(("1", "2")), log_section_height_decrease: gen_v(("m", "n")), log_section_height_increase: gen_v(("o", "p")), - log_scroll_forward: gen_v(("q", "r")), - log_scroll_back: gen_v(("s", "t")), log_section_toggle: gen_v(("u", "v")), quit: gen_v(("w", "x")), save_logs: gen_v(("y", "z")), @@ -470,37 +474,38 @@ mod tests { let expected = Keymap { clear: (KeyCode::Char('a'), Some(KeyCode::Char('b'))), - delete_deny: (KeyCode::Char('e'), None), delete_confirm: (KeyCode::Char('c'), Some(KeyCode::Char('d'))), + delete_deny: (KeyCode::Char('e'), Some(KeyCode::Char('f'))), exec: (KeyCode::Char('g'), Some(KeyCode::Char('h'))), filter_mode: (KeyCode::Char('i'), Some(KeyCode::Char('j'))), force_redraw: (KeyCode::Char('k'), Some(KeyCode::Char('l'))), - log_section_height_increase: (KeyCode::Char('o'), Some(KeyCode::Char('p'))), - log_section_height_decrease: (KeyCode::Char('m'), Some(KeyCode::Char('n'))), - log_section_toggle: (KeyCode::Char('u'), Some(KeyCode::Char('v'))), - log_scroll_forward: (KeyCode::Char('q'), Some(KeyCode::Char('r'))), log_scroll_back: (KeyCode::Char('s'), Some(KeyCode::Char('t'))), + log_scroll_forward: (KeyCode::Char('q'), Some(KeyCode::Char('r'))), + log_search_mode: (KeyCode::Char('1'), Some(KeyCode::Char('2'))), + log_section_height_decrease: (KeyCode::Char('m'), Some(KeyCode::Char('n'))), + log_section_height_increase: (KeyCode::Char('o'), Some(KeyCode::Char('p'))), + log_section_toggle: (KeyCode::Char('u'), Some(KeyCode::Char('v'))), quit: (KeyCode::Char('w'), Some(KeyCode::Char('x'))), save_logs: (KeyCode::Char('y'), Some(KeyCode::Char('z'))), scroll_down: (KeyCode::Char('3'), Some(KeyCode::Char('4'))), scroll_end: (KeyCode::Char('5'), Some(KeyCode::Char('6'))), + scroll_many: KeyModifiers::ALT, scroll_start: (KeyCode::Char('7'), Some(KeyCode::Char('8'))), scroll_up: (KeyCode::F(1), Some(KeyCode::F(2))), select_next_panel: (KeyCode::F(3), Some(KeyCode::F(4))), select_previous_panel: (KeyCode::F(5), Some(KeyCode::F(6))), - sort_by_name: (KeyCode::Up, Some(KeyCode::Down)), - sort_by_state: (KeyCode::Char('['), Some(KeyCode::Char(']'))), - sort_by_status: (KeyCode::Tab, None), sort_by_cpu: (KeyCode::F(7), Some(KeyCode::F(8))), - sort_by_memory: (KeyCode::Home, Some(KeyCode::End)), sort_by_id: (KeyCode::F(9), Some(KeyCode::F(10))), sort_by_image: (KeyCode::F(11), Some(KeyCode::F(12))), + sort_by_memory: (KeyCode::Home, Some(KeyCode::End)), + sort_by_name: (KeyCode::Up, Some(KeyCode::Down)), sort_by_rx: (KeyCode::Left, Some(KeyCode::Right)), + sort_by_state: (KeyCode::Char('['), Some(KeyCode::Char(']'))), + sort_by_status: (KeyCode::Tab, None), sort_by_tx: (KeyCode::PageDown, Some(KeyCode::PageUp)), sort_reset: (KeyCode::Char(','), Some(KeyCode::Char('.'))), toggle_help: (KeyCode::Char('-'), Some(KeyCode::Char('='))), toggle_mouse_capture: (KeyCode::Char('\\'), Some(KeyCode::Char('/'))), - scroll_many: KeyModifiers::ALT, }; assert_eq!(expected, result); } diff --git a/src/config/mod.rs b/src/config/mod.rs index 24e6877..699768e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -13,6 +13,9 @@ pub use {color_parser::AppColors, keymap_parser::Keymap}; mod parse_args; mod parse_config_file; +// TODO use a global pub static oncelock for the config +// static CELL: OnceLock = OnceLock::new(); + #[derive(Debug, Clone)] #[allow(clippy::struct_excessive_bools)] pub struct Config { @@ -23,14 +26,15 @@ pub struct Config { pub host: Option, pub in_container: bool, pub keymap: Keymap, + pub log_search_case_sensitive: bool, pub raw_logs: bool, pub save_dir: Option, + pub show_logs: bool, pub show_self: bool, pub show_std_err: bool, pub show_timestamp: bool, - pub timezone: Option, pub timestamp_format: String, - pub show_logs: bool, + pub timezone: Option, pub use_cli: bool, } @@ -44,15 +48,16 @@ impl From<&Args> for Config { host: args.host.clone(), in_container: Self::check_if_in_container(), keymap: Keymap::new(), + log_search_case_sensitive: true, raw_logs: args.raw, save_dir: Self::try_get_logs_dir(args.save_dir.as_ref()), + show_logs: true, show_self: !args.show_self, show_std_err: !args.no_std_err, show_timestamp: !args.timestamp, - timezone: Self::parse_timezone(args.timezone.clone()), timestamp_format: Self::parse_timestamp_format(None), + timezone: Self::parse_timezone(args.timezone.clone()), use_cli: args.use_cli, - show_logs: true, } } } @@ -67,15 +72,16 @@ impl From for Config { host: config_file.host, in_container: Self::check_if_in_container(), keymap: Keymap::from(config_file.keymap), + log_search_case_sensitive: config_file.log_search_case_sensitive.unwrap_or(true), raw_logs: config_file.raw_logs.unwrap_or(false), save_dir: Self::try_get_logs_dir(config_file.save_dir.as_ref()), + show_logs: config_file.show_logs.unwrap_or(true), show_self: config_file.show_self.unwrap_or(false), show_std_err: config_file.show_std_err.unwrap_or(true), show_timestamp: config_file.show_timestamp.unwrap_or(true), - timezone: Self::parse_timezone(config_file.timezone), timestamp_format: Self::parse_timestamp_format(config_file.timestamp_format), + timezone: Self::parse_timezone(config_file.timezone), use_cli: config_file.use_cli.unwrap_or(false), - show_logs: config_file.show_logs.unwrap_or(true), } } } diff --git a/src/config/parse_config_file.rs b/src/config/parse_config_file.rs index ead111a..d61991a 100644 --- a/src/config/parse_config_file.rs +++ b/src/config/parse_config_file.rs @@ -67,15 +67,16 @@ pub struct ConfigFile { pub gui: Option, pub host: Option, pub keymap: Option, + pub log_search_case_sensitive: Option, pub raw_logs: Option, pub save_dir: Option, + pub show_logs: Option, pub show_self: Option, pub show_std_err: Option, pub show_timestamp: Option, pub timestamp_format: Option, pub timezone: Option, pub use_cli: Option, - pub show_logs: Option, } impl ConfigFile { diff --git a/src/input_handler/mod.rs b/src/input_handler/mod.rs index 12abc41..15146d8 100644 --- a/src/input_handler/mod.rs +++ b/src/input_handler/mod.rs @@ -19,7 +19,7 @@ use uuid::Uuid; mod message; use crate::{ - app_data::{AppData, DockerCommand, Header}, + app_data::{AppData, DockerCommand, Header, ScrollDirection}, app_error::AppError, config, docker_data::DockerMessage, @@ -74,9 +74,11 @@ impl InputHandler { if contains(Status::DeleteConfirm) { self.button_intersect(mouse_event).await; } else if !contains(Status::Error) - | !contains(Status::Help) - | !contains(Status::DeleteConfirm) - | !contains(Status::Filter) + && !contains(Status::Help) + && !contains(Status::DeleteConfirm) + && !contains(Status::Filter) + // TODO handle state where you want to scroll log search results with the mouse wheel + && !contains(Status::SearchLogs) { self.mouse_press(mouse_event, modifider); } @@ -294,23 +296,12 @@ impl InputHandler { } } - /// Advance the "cursor" along the logs - fn logs_forward(&self, modifier: KeyModifiers) { + fn logs_horizontal_scroll(&self, modifier: KeyModifiers, sd: &ScrollDirection) { let panel = self.gui_state.lock().get_selected_panel(); if panel == SelectablePanel::Logs { for _ in 0..self.get_modifier_total(modifier) { let width = self.gui_state.lock().get_screen_width(); - self.app_data.lock().log_forward(width); - } - } - } - - /// Retreat the "cursor" along the logs - fn logs_back(&self, modifier: KeyModifiers) { - let panel = self.gui_state.lock().get_selected_panel(); - if panel == SelectablePanel::Logs { - for _ in 0..self.get_modifier_total(modifier) { - self.app_data.lock().log_back(); + self.app_data.lock().logs_horizontal_scroll(sd, width); } } } @@ -388,6 +379,66 @@ impl InputHandler { } } + /// Actions to take when Filter status active + fn handle_search_logs(&self, key_code: KeyCode, modifier: KeyModifiers) { + match key_code { + KeyCode::Esc => { + self.app_data.lock().logs_search_clear(); + self.gui_state.lock().status_del(Status::SearchLogs); + } + _ if KeyCode::Enter == key_code + || self.keymap.log_search_mode.0 == key_code + || self.keymap.log_search_mode.1 == Some(key_code) => + { + self.gui_state.lock().status_del(Status::SearchLogs); + } + + _ if self.keymap.log_scroll_back.0 == key_code + || self.keymap.log_scroll_back.1 == Some(key_code) => + { + self.logs_horizontal_scroll(modifier, &ScrollDirection::Previous); + } + + _ if self.keymap.log_scroll_forward.0 == key_code + || self.keymap.log_scroll_forward.1 == Some(key_code) => + { + self.logs_horizontal_scroll(modifier, &ScrollDirection::Next); + } + + _ if self.keymap.scroll_down.0 == key_code => { + self.app_data + .lock() + .log_search_scroll(&ScrollDirection::Next); + // TODO should only do this is log_search_scroll returns some + // Need to wait til app_data and gui_data is combined + self.gui_state + .lock() + .set_logs_panel_selected(&self.app_data); + // + } + + _ if self.keymap.scroll_up.0 == key_code => { + self.app_data + .lock() + .log_search_scroll(&ScrollDirection::Previous); + // TODO should only do this is log_search_scroll returns some + // Need to wait til app_data and gui_data is combined + self.gui_state + .lock() + .set_logs_panel_selected(&self.app_data); + } + + // handle up and down keys + KeyCode::Backspace => { + self.app_data.lock().log_search_pop(); + } + KeyCode::Char(x) => { + self.app_data.lock().log_search_push(x); + } + _ => (), + } + } + /// Actions to take when Filter status active fn handle_filter(&self, key_code: KeyCode) { match key_code { @@ -572,13 +623,13 @@ impl InputHandler { _ if self.keymap.scroll_up.0 == key_code || self.keymap.scroll_up.1 == Some(key_code) => { - self.scroll_up(modifier); + self.scroll(modifier, &ScrollDirection::Previous); } _ if self.keymap.scroll_down.0 == key_code || self.keymap.scroll_down.1 == Some(key_code) => { - self.scroll_down(modifier); + self.scroll(modifier, &ScrollDirection::Next); } _ if self.keymap.filter_mode.0 == key_code @@ -588,16 +639,27 @@ impl InputHandler { self.docker_tx.send(DockerMessage::Update).await.ok(); } + _ if self.keymap.log_search_mode.0 == key_code + || self.keymap.log_search_mode.1 == Some(key_code) => + { + if !self.gui_state.lock().get_show_logs() { + self.gui_state.lock().toggle_show_logs(); + } + self.gui_state.lock().status_push(Status::SearchLogs); + } + _ if self.keymap.log_scroll_back.0 == key_code || self.keymap.log_scroll_back.1 == Some(key_code) => { - self.logs_back(modifier); + self.logs_horizontal_scroll(modifier, &ScrollDirection::Previous); + // self.logs_back(modifier); } _ if self.keymap.log_scroll_forward.0 == key_code || self.keymap.log_scroll_forward.1 == Some(key_code) => { - self.logs_forward(modifier); + self.logs_horizontal_scroll(modifier, &ScrollDirection::Next); + // self.logs_forward(modifier); } KeyCode::Enter => self.enter_key().await, @@ -615,13 +677,14 @@ impl InputHandler { let contains_exec = contains(Status::Exec); let contains_filter = contains(Status::Filter); let contains_delete = contains(Status::DeleteConfirm); + let containes_search_logs = contains(Status::SearchLogs); if !contains_exec { let is_q = || key_code == self.keymap.quit.0 || Some(key_code) == self.keymap.quit.1; if key_modifier == KeyModifiers::CONTROL && key_code == KeyCode::Char('c') - || is_q() && !contains_filter + || is_q() && !contains_filter && !containes_search_logs { - // Always just quit on Ctrl + c/C or q/Q, unless in Filter status active + // Always just quit on Ctrl + c/C or q/Q, unless in filter/search_logs mode, i.e. when user inmput can include the q key self.quit(); } @@ -631,6 +694,8 @@ impl InputHandler { self.handle_help(key_code); } else if contains_filter { self.handle_filter(key_code); + } else if containes_search_logs { + self.handle_search_logs(key_code, key_modifier); } else if contains_delete { self.handle_delete(key_code).await; } else { @@ -669,8 +734,8 @@ impl InputHandler { } } else { match mouse_event.kind { - MouseEventKind::ScrollUp => self.scroll_up(modifier), - MouseEventKind::ScrollDown => self.scroll_down(modifier), + MouseEventKind::ScrollUp => self.scroll(modifier, &ScrollDirection::Previous), + MouseEventKind::ScrollDown => self.scroll(modifier, &ScrollDirection::Next), MouseEventKind::Down(MouseButton::Left) => { let mouse_point = Rect::new(mouse_event.column, mouse_event.row, 1, 1); let header = self.gui_state.lock().get_intersect_header(mouse_point); @@ -690,38 +755,25 @@ impl InputHandler { } /// Change state to next, depending which panel is currently in focus - fn scroll_down(&self, modifier: KeyModifiers) { - let selected_panel = self.gui_state.lock().get_selected_panel(); - match selected_panel { - SelectablePanel::Containers => { - for _ in 0..self.get_modifier_total(modifier) { - self.app_data.lock().containers_next(); + fn scroll(&self, modifier: KeyModifiers, scroll: &ScrollDirection) { + let status = self.gui_state.lock().get_status(); + if status.contains(&Status::SearchLogs) { + self.app_data.lock().log_search_scroll(scroll); + } else { + let selected_panel = self.gui_state.lock().get_selected_panel(); + match selected_panel { + SelectablePanel::Containers => { + for _ in 0..self.get_modifier_total(modifier) { + self.app_data.lock().containers_scroll(scroll); + } } - } - SelectablePanel::Logs => { - for _ in 0..self.get_modifier_total(modifier) { - self.app_data.lock().log_next(); + SelectablePanel::Logs => { + for _ in 0..self.get_modifier_total(modifier) { + self.app_data.lock().log_scroll(scroll); + } } + SelectablePanel::Commands => self.app_data.lock().docker_controls_scroll(scroll), } - SelectablePanel::Commands => self.app_data.lock().docker_controls_next(), - } - } - - /// Change state to previous, depending which panel is currently in focus - fn scroll_up(&self, modifier: KeyModifiers) { - let selected_panel = self.gui_state.lock().get_selected_panel(); - match selected_panel { - SelectablePanel::Containers => { - for _ in 0..self.get_modifier_total(modifier) { - self.app_data.lock().containers_previous(); - } - } - SelectablePanel::Logs => { - for _ in 0..self.get_modifier_total(modifier) { - self.app_data.lock().log_previous(); - } - } - SelectablePanel::Commands => self.app_data.lock().docker_controls_previous(), } } } diff --git a/src/main.rs b/src/main.rs index 4de8728..30ea787 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![allow(clippy::collapsible_if)] +// #![allow(unused)] // Zigbuild is stuck on 1.87.0, which means Mac builds won't work when using collapsible ifs use app_data::AppData; @@ -175,6 +176,7 @@ mod tests { show_std_err: false, in_container: false, save_dir: None, + log_search_case_sensitive: true, raw_logs: false, show_self: false, app_colors: AppColors::new(), diff --git a/src/ui/draw_blocks/commands.rs b/src/ui/draw_blocks/commands.rs index 805ae0f..d01af86 100644 --- a/src/ui/draw_blocks/commands.rs +++ b/src/ui/draw_blocks/commands.rs @@ -59,6 +59,7 @@ mod tests { use ratatui::style::{Color, Modifier}; use crate::{ + app_data::ScrollDirection, config::AppColors, tests::gen_container_summary, ui::{ @@ -169,7 +170,10 @@ mod tests { .app_data .lock() .update_containers(vec![gen_container_summary(1, "paused")]); - setup.app_data.lock().docker_controls_next(); + setup + .app_data + .lock() + .docker_controls_scroll(&ScrollDirection::Next); setup .terminal @@ -363,7 +367,10 @@ mod tests { .app_data .lock() .update_containers(vec![gen_container_summary(1, "paused")]); - setup.app_data.lock().docker_controls_next(); + setup + .app_data + .lock() + .docker_controls_scroll(&ScrollDirection::Next); setup .terminal diff --git a/src/ui/draw_blocks/help.rs b/src/ui/draw_blocks/help.rs index feba3fa..e7cd8b4 100644 --- a/src/ui/draw_blocks/help.rs +++ b/src/ui/draw_blocks/help.rs @@ -160,6 +160,11 @@ impl HelpInfo { button_item("/"), button_desc("enter filter mode"), ]), + Line::from(vec![ + space(), + button_item("#"), + button_desc("enter log search mode"), + ]), Line::from(vec![space(), button_item("0"), button_desc("stop sort")]), Line::from(vec![ space(), @@ -306,6 +311,7 @@ impl HelpInfo { "toggle mouse capture - if disabled, text on screen can be selected & copied", ), or_secondary(km.filter_mode, "enter filter mode"), + or_secondary(km.log_search_mode, "enter log search mode"), or_secondary(km.sort_reset, "reset container sorting"), or_secondary(km.sort_by_name, "sort containers by name"), or_secondary(km.sort_by_state, "sort containers by state"), @@ -458,7 +464,7 @@ mod tests { /// This test is incredibly annoying /// println!("{} {} {} {} {}", row_index, result_cell_index, result_cell.symbol(), result_cell.bg, result_cell.fg); fn test_draw_blocks_help() { - let mut setup = test_setup(87, 37, true, true); + let mut setup = test_setup(87, 39, true, true); let tz = setup.app_data.lock().config.timezone.clone(); setup @@ -480,35 +486,30 @@ mod tests { for (result_cell_index, result_cell) in result_row.iter().enumerate() { match (row_index, result_cell_index) { // first & last row, and first & last char on each row, is reset/reset, making sure that the help info is centered in the given area - (0 | 36, _) | (0..=35, 0 | 86) => { + (0 | 38, _) | (0..=37, 0 | 86) => { assert_eq!(result_cell.bg, Color::Reset); assert_eq!(result_cell.fg, Color::Reset); } - // border is red on black - (1 | 34, _) | (1..=31, 1 | 85) => { - assert_eq!(result_cell.bg, Color::Magenta); - assert_eq!(result_cell.fg, Color::Black); - } // Buttons - (2..=10, 2..=85) + (2..=10, 2..=84) | (12, 19..=66) | (14, 2..=10 | 13..=27) | (15, 2..=10 | 13..=21 | 24..=37) - | (16 | 27 | 29, 2..=10) + | (16 | 28 | 30, 2..=10) + | (19..=26 | 29 | 31, 2..=8) | (17, 2..=11) - | (18 | 26, 2..=12) - | (19 | 20 | 21 | 22 | 24 | 25 | 28 | 23 | 30, 2..=8) + | (18 | 27, 2..=12) | (24, 2..=9 | 12..=18) => { assert_eq!(result_cell.bg, Color::Magenta); assert_eq!(result_cell.fg, Color::White); } - // The URL is yellow and underlined - (33, 25..=60) => { + // The URL is white on yellow and underlined + (34, 25..=60) => { assert_eq!(result_cell.bg, Color::Magenta); assert_eq!(result_cell.fg, Color::White); assert_eq!(result_cell.modifier, Modifier::UNDERLINED); } - // The rest is red on black + // The rest is black on magenta _ => { assert_eq!(result_cell.bg, Color::Magenta); assert_eq!(result_cell.fg, Color::Black); @@ -523,7 +524,7 @@ mod tests { /// This test is incredibly annoying /// println!("{} {} {} {} {}", row_index, result_cell_index, result_cell.symbol(), result_cell.bg, result_cell.fg); fn test_draw_blocks_help_custom_colors() { - let mut setup = test_setup(87, 37, true, true); + let mut setup = test_setup(87, 39, true, true); let mut colors = AppColors::new(); let tz = setup.app_data.lock().config.timezone.clone(); @@ -549,35 +550,30 @@ mod tests { for (result_cell_index, result_cell) in result_row.iter().enumerate() { match (row_index, result_cell_index) { // first & last row, and first & last char on each row, is reset/reset, making sure that the help info is centered in the given area - (0 | 36, _) | (0..=35, 0 | 86) => { + (0 | 38, _) | (0..=37, 0 | 86) => { assert_eq!(result_cell.bg, Color::Reset); assert_eq!(result_cell.fg, Color::Reset); } - // border is red on black - (1 | 34, _) | (1..=31, 1 | 85) => { - assert_eq!(result_cell.bg, Color::Black); - assert_eq!(result_cell.fg, Color::Red); - } // Buttons - (2..=10, 2..=85) + (2..=10, 2..=84) | (12, 19..=66) | (14, 2..=10 | 13..=27) | (15, 2..=10 | 13..=21 | 24..=37) - | (16 | 27 | 29, 2..=10) + | (16 | 28 | 30, 2..=10) + | (19..=26 | 29 | 31, 2..=8) | (17, 2..=11) - | (18 | 26, 2..=12) - | (19 | 20 | 21 | 22 | 24 | 25 | 28 | 23 | 30, 2..=8) - | (24, 2..=9 | 12..=18) => { + | (18 | 27, 2..=12) + | (24, 2..=9 | 12..=18) => { assert_eq!(result_cell.bg, Color::Black); assert_eq!(result_cell.fg, Color::Yellow); } - // The URL is yellow and underlined - (33, 25..=60) => { + // The URL is white on yellow and underlined + (34, 25..=60) => { assert_eq!(result_cell.bg, Color::Black); assert_eq!(result_cell.fg, Color::Yellow); assert_eq!(result_cell.modifier, Modifier::UNDERLINED); } - // The rest is red on black + // The rest is black on magenta _ => { assert_eq!(result_cell.bg, Color::Black); assert_eq!(result_cell.fg, Color::Red); @@ -598,6 +594,7 @@ mod tests { delete_deny: (KeyCode::Char('c'), None), exec: (KeyCode::Char('d'), None), filter_mode: (KeyCode::Char('e'), None), + log_search_mode: (KeyCode::Char('7'), None), force_redraw: (KeyCode::Char('f'), None), log_scroll_back: (KeyCode::Char('g'), None), log_scroll_forward: (KeyCode::Char('h'), None), @@ -648,6 +645,7 @@ mod tests { delete_deny: (KeyCode::Char('c'), Some(KeyCode::Char('C'))), exec: (KeyCode::Char('d'), Some(KeyCode::Char('D'))), filter_mode: (KeyCode::Char('e'), Some(KeyCode::Char('E'))), + log_search_mode: (KeyCode::Char('m'), Some(KeyCode::Char('M'))), force_redraw: (KeyCode::Char('f'), Some(KeyCode::Char('F'))), log_scroll_back: (KeyCode::Char('f'), Some(KeyCode::Char('F'))), log_scroll_forward: (KeyCode::Char('g'), Some(KeyCode::Char('G'))), @@ -689,6 +687,7 @@ mod tests { #[test] /// Help panel will show custom keymap if in use, with either one or two definition for each entry + #[allow(clippy::todo)] fn test_draw_blocks_help_one_and_two_definitions() { let mut setup = test_setup(110, 50, true, true); @@ -698,6 +697,7 @@ mod tests { delete_deny: (KeyCode::Char('c'), Some(KeyCode::Char('C'))), exec: (KeyCode::Char('d'), None), filter_mode: (KeyCode::Char('e'), Some(KeyCode::Char('E'))), + log_search_mode: (KeyCode::Char('8'), None), force_redraw: (KeyCode::Char('f'), None), log_scroll_back: (KeyCode::Char('g'), Some(KeyCode::Char('G'))), log_scroll_forward: (KeyCode::Char('h'), None), @@ -724,7 +724,7 @@ mod tests { sort_by_tx: (KeyCode::Char('3'), None), sort_reset: (KeyCode::Char('4'), Some(KeyCode::Char('5'))), toggle_help: (KeyCode::Char('5'), None), - toggle_mouse_capture: (KeyCode::Char('6'), Some(KeyCode::Char('7'))), + toggle_mouse_capture: (KeyCode::Char('6'), Some(KeyCode::Char('#'))), }; let tz = setup.app_data.lock().config.timezone.clone(); @@ -741,7 +741,7 @@ mod tests { #[test] fn test_draw_blocks_help_show_timezone() { - let mut setup = test_setup(87, 37, true, true); + let mut setup = test_setup(87, 39, true, true); setup .terminal diff --git a/src/ui/draw_blocks/logs.rs b/src/ui/draw_blocks/logs.rs index 61d501f..ebf6ec7 100644 --- a/src/ui/draw_blocks/logs.rs +++ b/src/ui/draw_blocks/logs.rs @@ -81,7 +81,7 @@ mod tests { use uuid::Uuid; use crate::{ - app_data::{ContainerImage, ContainerName}, + app_data::{ContainerImage, ContainerName, ScrollDirection}, config::AppColors, ui::{ FrameData, Status, @@ -309,7 +309,7 @@ mod tests { ); }) .unwrap(); - setup.app_data.lock().log_previous(); + setup.app_data.lock().log_scroll(&ScrollDirection::Previous); let fd = FrameData::from((&setup.app_data, &setup.gui_state)); setup diff --git a/src/ui/draw_blocks/mod.rs b/src/ui/draw_blocks/mod.rs index d65a37c..507a913 100644 --- a/src/ui/draw_blocks/mod.rs +++ b/src/ui/draw_blocks/mod.rs @@ -23,6 +23,7 @@ pub mod info; pub mod logs; pub mod popup; pub mod ports; +pub mod search_logs; pub const NAME_TEXT: &str = r#" 88 @@ -160,19 +161,11 @@ pub mod tests { fn from(data: (&Arc>, &Arc>)) -> Self { let (mut app_data, gui_data) = (data.0.lock(), data.1.lock()); - // let container_section_height = app_data.get_container_len(); - // let container_section_height = if container_section_height < 12 { - // u16::try_from(container_section_height + 5).unwrap_or_default() - // } else { - // 12 - // }; - let (filter_by, filter_term) = app_data.get_filter(); Self { chart_data: app_data.get_chart_data(), color_logs: app_data.config.color_logs, columns: app_data.get_width(), - // container_section_height, container_title: app_data.get_container_title(), delete_confirm: gui_data.get_delete_container(), filter_by, @@ -182,6 +175,7 @@ pub mod tests { show_logs: gui_data.get_show_logs(), info_text: gui_data.info_box_text.clone(), is_loading: gui_data.is_loading(), + log_search: app_data.gen_log_search(), loading_icon: gui_data.get_loading().to_string(), log_height: gui_data.get_log_height(), log_title: app_data.get_log_title(), @@ -230,7 +224,6 @@ pub mod tests { /// Just a shorthand for when enumerating over result cells pub fn get_result( setup: &'_ TuiTestSetup, - // w: u16, ) -> std::iter::Enumerate> { setup .terminal diff --git a/src/ui/draw_blocks/search_logs.rs b/src/ui/draw_blocks/search_logs.rs new file mode 100644 index 0000000..81b1e31 --- /dev/null +++ b/src/ui/draw_blocks/search_logs.rs @@ -0,0 +1,479 @@ +use crossterm::event::KeyCode; +use ratatui::{ + Frame, + layout::{Constraint, Direction, Layout, Rect}, + style::{Modifier, Style}, + text::{Line, Span}, + widgets::Paragraph, +}; + +use crate::{ + app_data::LogsButton, + config::{AppColors, Keymap}, + ui::FrameData, +}; + +// background, text, selected_text, highlight; +/// Draw the filter bar +pub fn draw(area: Rect, colors: AppColors, frame: &mut Frame, fd: &FrameData, keymap: &Keymap) { + let style_but = Style::default() + .fg(colors.log_search.button_text) + .bg(colors.log_search.highlight); + let style_desc = Style::default() + .fg(colors.log_search.text) + .bg(colors.log_search.background); + let space = || Span::from(" "); + + let mut line = vec![ + Span::styled(" Esc ", style_but), + Span::styled(" clear ", style_desc), + space(), + ]; + line.extend([Span::styled( + " search term: ", + Style::default() + .fg(colors.log_search.highlight) + .bg(colors.log_search.background) + .add_modifier(Modifier::BOLD), + )]); + + if let Some(log_search) = fd.log_search.as_ref() { + line.extend([ + Span::styled( + log_search + .term + .as_ref() + .map_or(String::new(), std::clone::Clone::clone), + Style::default() + .fg(colors.log_search.text) + .bg(colors.log_search.background), + ), + space(), + ]); + } + + let left_text = Paragraph::new(Line::from(line)) + .alignment(ratatui::layout::Alignment::Left) + .style(Style::default().bg(colors.log_search.background)); + let mut line = vec![]; + if let Some(log_search) = fd.log_search.as_ref() { + if let Some(buttons) = log_search.buttons.as_ref() { + let down = if keymap.scroll_down.0 == KeyCode::Down { + "↑".to_owned() + } else { + keymap.scroll_down.0.to_string() + }; + let up = if keymap.scroll_up.0 == KeyCode::Up { + "↓".to_owned() + } else { + keymap.scroll_up.0.to_string() + }; + let next = [ + space(), + Span::styled(format!(" {up} "), style_but), + Span::styled(" next ", style_desc), + ]; + let previous = [ + space(), + Span::styled(format!(" {down} "), style_but), + Span::styled(" previous ", style_desc), + ]; + + match buttons { + LogsButton::Both => line.extend(previous.into_iter().chain(next)), + LogsButton::Next => line.extend(next), + LogsButton::Previous => line.extend(previous), + } + } + + if let Some(results) = log_search.result.as_ref() { + line.extend([ + Span::styled( + " matches: ", + Style::default() + .fg(colors.log_search.highlight) + .bg(colors.log_search.background) + .add_modifier(Modifier::BOLD), + ), + Span::styled( + results, + Style::default() + .fg(colors.log_search.text) + .bg(colors.log_search.background), + ), + ]); + } + } + let right_text = Paragraph::new(Line::from(line)) + .alignment(ratatui::layout::Alignment::Right) + .style(Style::default().bg(colors.log_search.background)); + + let line_split = Layout::default() + .direction(Direction::Horizontal) + .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) + .split(area); + frame.render_widget(left_text, line_split[0]); + frame.render_widget(right_text, line_split[1]); +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + + use crossterm::event::KeyCode; + use insta::assert_snapshot; + use ratatui::style::{Color, Modifier}; + + use crate::{ + config::{AppColors, Keymap}, + ui::{ + FrameData, + draw_blocks::tests::{get_result, insert_logs, test_setup}, + }, + }; + + #[test] + /// Filter row is drawn correctly & colors are correct + /// Colours change when filter_by option is changed + fn test_draw_blocks_log_search_row() { + let mut setup = test_setup(140, 1, true, true); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &setup.fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 5..=11 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 13..=26 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } + + #[test] + /// Log item found, previous button visible + fn test_draw_blocks_log_search_match_previous() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('e'); + + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 | 114..=116 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 5..=11 | 27 | 117..=126 | 137..=139 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 13..=26 | 127..=136 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } + + #[test] + /// Log item found, next button visible + fn test_draw_blocks_log_search_match_next() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('e'); + setup + .app_data + .lock() + .log_scroll(&crate::app_data::ScrollDirection::Previous); + setup + .app_data + .lock() + .log_scroll(&crate::app_data::ScrollDirection::Previous); + + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 | 118..=120 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 5..=11 | 27 | 121..=126 | 137..=139 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 13..=26 | 127..=136 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } + + #[test] + /// Log item found, next & previous button visible + fn test_draw_blocks_log_search_match_both_next_previous() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('e'); + setup + .app_data + .lock() + .log_scroll(&crate::app_data::ScrollDirection::Previous); + + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 | 104..=106 | 118..=120 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 5..=11 | 27 | 107..=116 | 121..=126 | 137..=139 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 13..=26 | 127..=136 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } + + #[test] + /// No log item found + fn test_draw_blocks_log_search_match_none() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('z'); + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 => { + assert_eq!(result_cell.bg, Color::Magenta); + assert_eq!(result_cell.fg, Color::Black); + } + 5..=11 | 27 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Gray); + } + 13..=26 => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Magenta); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::Reset); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } + + #[test] + /// Custom keymap for scroll buttons + fn test_draw_blocks_log_search_keymap() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + + let mut keymap = setup.app_data.lock().config.keymap.clone(); + keymap.scroll_up = (KeyCode::Char('a'), None); + keymap.scroll_down = (KeyCode::Char('b'), None); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('e'); + setup + .app_data + .lock() + .log_scroll(&crate::app_data::ScrollDirection::Previous); + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + + setup + .terminal + .draw(|f| { + super::draw(setup.area, AppColors::new(), f, &fd, &keymap); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + } + + #[test] + /// Custom colours applied + fn test_draw_blocks_log_search_colors() { + let mut setup = test_setup(140, 1, true, true); + + insert_logs(&setup); + + setup + .gui_state + .lock() + .status_push(crate::ui::Status::SearchLogs); + + setup.app_data.lock().log_search_push('e'); + setup + .app_data + .lock() + .log_scroll(&crate::app_data::ScrollDirection::Previous); + + let mut colors = AppColors::new(); + + colors.log_search.background = Color::White; + colors.log_search.highlight = Color::Blue; + colors.log_search.button_text = Color::Yellow; + colors.log_search.text = Color::Magenta; + + let fd = FrameData::from((&setup.app_data, &setup.gui_state)); + setup + .terminal + .draw(|f| { + super::draw(setup.area, colors, f, &fd, &Keymap::new()); + }) + .unwrap(); + + assert_snapshot!(setup.terminal.backend()); + + for (_, result_row) in get_result(&setup) { + for (result_cell_index, result_cell) in result_row.iter().enumerate() { + match result_cell_index { + 0..=4 | 104..=106 | 118..=120 => { + assert_eq!(result_cell.bg, Color::Blue); + assert_eq!(result_cell.fg, Color::Yellow); + } + 5..=11 | 27 | 107..=116 | 121..=126 | 137..=139 => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Magenta); + } + 13..=26 | 127..=136 => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Blue); + assert_eq!(result_cell.modifier, Modifier::BOLD); + } + _ => { + assert_eq!(result_cell.bg, Color::White); + assert_eq!(result_cell.fg, Color::Reset); + } + } + } + } + } +} diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help.snap index 13294ae..05c3479 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help.snap @@ -27,6 +27,7 @@ expression: setup.terminal.backend() " │ ( s ) save logs to file │ " " │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( F1 ) or ( / ) enter filter mode │ " +" │ ( # ) enter log search mode │ " " │ ( 0 ) stop sort │ " " │ ( 1 - 9 ) sort by header - or click header │ " " │ ( - = ) change log section height │ " @@ -37,5 +38,6 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " +" │ │ " " ╰───────────────────────────────────────────────────────────────────────────────────╯ " " " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_colors.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_colors.snap index 13294ae..05c3479 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_colors.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_colors.snap @@ -27,6 +27,7 @@ expression: setup.terminal.backend() " │ ( s ) save logs to file │ " " │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( F1 ) or ( / ) enter filter mode │ " +" │ ( # ) enter log search mode │ " " │ ( 0 ) stop sort │ " " │ ( 1 - 9 ) sort by header - or click header │ " " │ ( - = ) change log section height │ " @@ -37,5 +38,6 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " +" │ │ " " ╰───────────────────────────────────────────────────────────────────────────────────╯ " " " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_one_definition.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_one_definition.snap index a6a54ab..fe1fb83 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_one_definition.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_one_definition.snap @@ -31,6 +31,7 @@ expression: setup.terminal.backend() " │ ( m ) save logs to file │ " " │ ( 6 ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( e ) enter filter mode │ " +" │ ( 7 ) enter log search mode │ " " │ ( 4 ) reset container sorting │ " " │ ( z ) sort containers by name │ " " │ ( 1 ) sort containers by state │ " @@ -50,5 +51,4 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " -" │ │ " " ╰────────────────────────────────────────────────────────────────────────────────────╯ " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_two_definitions.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_two_definitions.snap index 0d8e0ac..173bc53 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_two_definitions.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_custom_keymap_two_definitions.snap @@ -31,6 +31,7 @@ expression: setup.terminal.backend() " │ ( l ) or ( L ) save logs to file │ " " │ ( 5 ) or ( Page Down ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( e ) or ( E ) enter filter mode │ " +" │ ( m ) or ( M ) enter log search mode │ " " │ ( 3 ) or ( 6 ) reset container sorting │ " " │ ( y ) or ( Y ) sort containers by name │ " " │ ( 0 ) or ( 9 ) sort containers by state │ " @@ -50,5 +51,4 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " -" │ │ " " ╰────────────────────────────────────────────────────────────────────────────────────────────────────╯ " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_one_and_two_definitions.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_one_and_two_definitions.snap index 3f36c29..b63b168 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_one_and_two_definitions.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_one_and_two_definitions.snap @@ -29,8 +29,9 @@ expression: setup.terminal.backend() " │ ( f ) force clear the screen & redraw the gui │ " " │ ( 5 ) toggle this help information - or click heading │ " " │ ( m ) or ( M ) save logs to file │ " -" │ ( 6 ) or ( 7 ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " +" │ ( 6 ) or ( # ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( e ) or ( E ) enter filter mode │ " +" │ ( 8 ) enter log search mode │ " " │ ( 4 ) or ( 5 ) reset container sorting │ " " │ ( z ) sort containers by name │ " " │ ( 1 ) sort containers by state │ " @@ -50,5 +51,4 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " -" │ │ " " ╰────────────────────────────────────────────────────────────────────────────────────────────╯ " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_show_timezone.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_show_timezone.snap index da0dbb7..6d237f2 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_show_timezone.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__help__tests__draw_blocks_help_show_timezone.snap @@ -28,6 +28,7 @@ expression: setup.terminal.backend() " │ ( s ) save logs to file │ " " │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ " " │ ( F1 ) or ( / ) enter filter mode │ " +" │ ( # ) enter log search mode │ " " │ ( 0 ) stop sort │ " " │ ( 1 - 9 ) sort by header - or click header │ " " │ ( - = ) change log section height │ " @@ -38,4 +39,5 @@ expression: setup.terminal.backend() " │ currently an early work in progress, all and any input appreciated │ " " │ https://github.com/mrjackwills/oxker │ " " │ │ " +" │ │ " " ╰───────────────────────────────────────────────────────────────────────────────────╯ " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_colors.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_colors.snap new file mode 100644 index 0000000..aa27273 --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_colors.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: e ↑ previous ↓ next matches: 2/3" diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_keymap.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_keymap.snap new file mode 100644 index 0000000..eafd76b --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_keymap.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: e b previous a next matches: 2/3" diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_both_next_previous.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_both_next_previous.snap new file mode 100644 index 0000000..aa27273 --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_both_next_previous.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: e ↑ previous ↓ next matches: 2/3" diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_next.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_next.snap new file mode 100644 index 0000000..c341ef3 --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_next.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: e ↓ next matches: 1/3" diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_none.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_none.snap new file mode 100644 index 0000000..4b4d112 --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_none.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: z " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_previous.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_previous.snap new file mode 100644 index 0000000..52a2e16 --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_match_previous.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: e ↑ previous matches: 3/3" diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_row.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_row.snap new file mode 100644 index 0000000..27d8aff --- /dev/null +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__search_logs__tests__draw_blocks_log_search_row.snap @@ -0,0 +1,5 @@ +--- +source: src/ui/draw_blocks/search_logs.rs +expression: setup.terminal.backend() +--- +" Esc clear search term: " diff --git a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__tests__draw_blocks_whole_layout_help_panel.snap b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__tests__draw_blocks_whole_layout_help_panel.snap index a45834e..c573cfb 100644 --- a/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__tests__draw_blocks_whole_layout_help_panel.snap +++ b/src/ui/draw_blocks/snapshots/oxker__ui__draw_blocks__tests__draw_blocks_whole_layout_help_panel.snap @@ -28,16 +28,16 @@ expression: setup.terminal.backend() "│ │ ( s ) save logs to file │ │" "│ │ ( m ) toggle mouse capture - if disabled, text on screen can be selected & copied │ │" "│ │ ( F1 ) or ( / ) enter filter mode │ │" +"│ │ ( # ) enter log search mode │ │" "│ │ ( 0 ) stop sort │ │" "│ │ ( 1 - 9 ) sort by header - or click header │ │" -"│ │ ( - = ) change log section height │ │" -"╰────────────────────────────────────│ ( \ ) toggle log section visibility │────────────────────────────────────╯" -"╭───────────────────────── cpu 03.00%│ ( esc ) close dialog │──────╮╭────────── ports ───────────╮" -"│10.00%│ •• │ ( q ) quit at any time │ ││ ip private public│" -"│ │ • • │ │ ││ 8001 │" -"│ │ •• • │ currently an early work in progress, all and any input appreciated │ ││127.0.0.1 8003 8003│" -"│ │ • • │ https://github.com/mrjackwills/oxker │ ││ │" -"│ │ •• • • │ │ ││ │" +"╰────────────────────────────────────│ ( - = ) change log section height │────────────────────────────────────╯" +"╭───────────────────────── cpu 03.00%│ ( \ ) toggle log section visibility │──────╮╭────────── ports ───────────╮" +"│10.00%│ •• │ ( esc ) close dialog │ ││ ip private public│" +"│ │ • • │ ( q ) quit at any time │ ││ 8001 │" +"│ │ •• • │ │ ││127.0.0.1 8003 8003│" +"│ │ • • │ currently an early work in progress, all and any input appreciated │ ││ │" +"│ │ •• • • │ https://github.com/mrjackwills/oxker │ ││ │" "│ │• •• │ │ ││ │" "│ │• • ╰────────────────────────────────────────────────────────────────────────────────────╯ ││ │" "│ │ ││ │ ││ │" diff --git a/src/ui/gui_state.rs b/src/ui/gui_state.rs index dd0aac9..9b598c4 100644 --- a/src/ui/gui_state.rs +++ b/src/ui/gui_state.rs @@ -170,6 +170,7 @@ pub enum Status { Help, Init, Logs, + SearchLogs, } /// Global gui_state, stored in an Arc @@ -411,6 +412,16 @@ impl GuiState { } } + pub fn set_logs_panel_selected(&mut self, app_data: &Arc>) { + self.selected_panel = SelectablePanel::Logs; + if (app_data.lock().get_container_len() == 0 + && self.get_selected_panel() == SelectablePanel::Commands) + || (self.log_height == 0 && self.get_selected_panel() == SelectablePanel::Logs) + { + self.selected_panel = self.selected_panel.next(); + } + self.rerender.update_draw(); + } /// Change to next selectable panel pub fn selectable_panel_next(&mut self, app_data: &Arc>) { self.selected_panel = self.selected_panel.next(); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c46dacf..bb7a458 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -30,8 +30,8 @@ pub use self::color_match::*; pub use self::gui_state::{DeleteButton, GuiState, SelectablePanel, Status}; use crate::{ app_data::{ - AppData, Columns, ContainerId, ContainerPorts, CpuTuple, FilterBy, Header, MemTuple, - SortedOrder, State, + AppData, Columns, ContainerId, ContainerPorts, CpuTuple, FilterBy, Header, LogSearch, + MemTuple, SortedOrder, State, }, app_error::AppError, config::{AppColors, Keymap}, @@ -218,10 +218,6 @@ impl Ui { } while self.is_running.load(Ordering::SeqCst) { - // if self.redraw.get_clear() { - // self.terminal.clear().ok(); - // continue; - // } if self.should_redraw(&mut drawn_at, docker_interval_ms) { let fd = FrameData::from(&*self); @@ -287,6 +283,7 @@ impl Ui { } /// Frequent data required by multiple frame drawing functions, can reduce mutex reads by placing it all in here +/// TODO refactor this #[derive(Debug, Clone)] #[allow(clippy::struct_excessive_bools)] pub struct FrameData { @@ -294,6 +291,7 @@ pub struct FrameData { color_logs: bool, columns: Columns, container_title: String, + log_search: Option, delete_confirm: Option, filter_by: FilterBy, filter_term: Option, @@ -327,6 +325,7 @@ impl From<&Ui> for FrameData { filter_by, filter_term: filter_term.cloned(), has_containers: app_data.get_container_len() > 0, + log_search: app_data.gen_log_search(), has_error: app_data.get_error(), info_text: gui_data.info_box_text.clone(), is_loading: gui_data.is_loading(), @@ -353,9 +352,12 @@ fn draw_frame( fd: &FrameData, gui_state: &Arc>, ) { + let contains_filter = fd.status.contains(&Status::Filter); + let contains_search_logs = fd.status.contains(&Status::SearchLogs); + let whole_layout = Layout::default() .direction(Direction::Vertical) - .constraints(if fd.status.contains(&Status::Filter) { + .constraints(if contains_filter || contains_search_logs { vec![Constraint::Max(1), Constraint::Min(1), Constraint::Max(1)] } else { vec![Constraint::Max(1), Constraint::Min(1)] @@ -364,9 +366,12 @@ fn draw_frame( draw_blocks::headers::draw(whole_layout[0], colors, f, fd, gui_state, keymap); - // If required, draw filter bar if let Some(rect) = whole_layout.get(2) { - draw_blocks::filter::draw(*rect, colors, f, fd); + if contains_filter { + draw_blocks::filter::draw(*rect, colors, f, fd); + } else { + draw_blocks::search_logs::draw(*rect, colors, f, fd, keymap); + } } let upper_main = Layout::default()