| 1 | use std::cmp::{max, min}; |
| 2 | |
| 3 | use ratatui::{ |
| 4 | Frame, |
| 5 | layout::{Alignment, Constraint, Direction, Layout, Rect}, |
| 6 | style::Style, |
| 7 | text::{Line, Span}, |
| 8 | widgets::{Padding, Paragraph, Wrap}, |
| 9 | }; |
| 10 | use unicode_width::UnicodeWidthStr; |
| 11 | |
| 12 | use crate::{ |
| 13 | app::App, |
| 14 | canvas::{ |
| 15 | Painter, |
| 16 | components::{ |
| 17 | scroll_bar::{ScrollBarArgs, dialog_scroll_bar_area, draw_scroll_bar}, |
| 18 | search_input, |
| 19 | }, |
| 20 | drawing_utils::dialog_block, |
| 21 | }, |
| 22 | constants::{self, HELP_TEXT}, |
| 23 | }; |
| 24 | |
| 25 | /// Append a highlighted match to `lines`, and return whether a match was found. |
| 26 | fn add_highlight_match<'a>( |
| 27 | query: &str, target: &'a str, lines: &mut Vec<Line<'a>>, normal_style: Style, |
| 28 | match_style: Style, |
| 29 | ) -> bool { |
| 30 | let lower = target.to_lowercase(); |
| 31 | |
| 32 | if let Some(pos) = lower.find(query) { |
| 33 | let match_end = pos + query.len(); |
| 34 | lines.push(Line::from(vec![ |
| 35 | Span::styled(&target[..pos], normal_style), |
| 36 | Span::styled(&target[pos..match_end], match_style), |
| 37 | Span::styled(&target[match_end..], normal_style), |
| 38 | ])); |
| 39 | true |
| 40 | } else { |
| 41 | false |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // TODO: [REFACTOR] Make generic dialog boxes to build off of instead? |
| 46 | impl Painter { |
| 47 | fn help_text_lines(&self, app_state: &App) -> Vec<Line<'_>> { |
| 48 | let mut lines: Vec<Line<'_>> = Vec::new(); |
| 49 | |
| 50 | let query = app_state |
| 51 | .help_dialog_state |
| 52 | .search_input_state |
| 53 | .current_query() |
| 54 | .trim() |
| 55 | .to_lowercase(); |
| 56 | |
| 57 | let is_filtering = !query.is_empty(); |
| 58 | if is_filtering { |
| 59 | let selected_header_style = self |
| 60 | .styles |
| 61 | .selected_text_style |
| 62 | .add_modifier(self.styles.table_header_style.add_modifier); |
| 63 | |
| 64 | HELP_TEXT.iter().enumerate().for_each(|(itx, section)| { |
| 65 | let mut iter = section.iter(); |
| 66 | |
| 67 | if itx == 0 { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | let header_str = iter.next().copied(); |
| 72 | |
| 73 | let mut header_line: Vec<Line<'_>> = Vec::new(); |
| 74 | let header_matches = if let Some(h) = header_str { |
| 75 | add_highlight_match( |
| 76 | &query, |
| 77 | h, |
| 78 | &mut header_line, |
| 79 | self.styles.table_header_style, |
| 80 | selected_header_style, |
| 81 | ) |
| 82 | } else { |
| 83 | false |
| 84 | }; |
| 85 | |
| 86 | let mut matched_body: Vec<Line<'_>> = Vec::new(); |
| 87 | for &text in iter { |
| 88 | add_highlight_match( |
| 89 | &query, |
| 90 | text, |
| 91 | &mut matched_body, |
| 92 | self.styles.text_style, |
| 93 | self.styles.selected_text_style, |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | if !matched_body.is_empty() || header_matches { |
| 98 | if let Some(header) = header_str { |
| 99 | // Don't insert the space if there's nothing above anyway. |
| 100 | if !lines.is_empty() { |
| 101 | lines.push(Line::from(Span::default())); |
| 102 | } |
| 103 | |
| 104 | if header_matches { |
| 105 | lines.extend(header_line); |
| 106 | } else { |
| 107 | lines.push(Line::from(Span::styled( |
| 108 | header, |
| 109 | self.styles.table_header_style, |
| 110 | ))); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | lines.extend(matched_body); |
| 115 | } |
| 116 | }); |
| 117 | } else { |
| 118 | HELP_TEXT.iter().enumerate().for_each(|(itx, section)| { |
| 119 | let mut iter = section.iter(); |
| 120 | |
| 121 | if itx == 0 { |
| 122 | for &text in iter { |
| 123 | lines.push(Line::from(Span::styled(text, self.styles.text_style))); |
| 124 | } |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | let header_opt = iter.next(); |
| 129 | let header_str = header_opt.copied(); |
| 130 | |
| 131 | if let Some(header) = header_str { |
| 132 | lines.push(Line::from(Span::default())); |
| 133 | lines.push(Line::from(Span::styled( |
| 134 | header, |
| 135 | self.styles.table_header_style, |
| 136 | ))); |
| 137 | } |
| 138 | for &text in iter { |
| 139 | lines.push(Line::from(Span::styled(text, self.styles.text_style))); |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | lines |
| 145 | } |
| 146 | |
| 147 | pub fn draw_help_dialog(&self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect) { |
| 148 | // TODO: We can reduce lines processed based on the known height. |
| 149 | let styled_help_text = self.help_text_lines(app_state); |
| 150 | |
| 151 | // Reserve one column on the right for the scroll bar. |
| 152 | let block = dialog_block(self.styles.border_type, self.styles.border_style) |
| 153 | .title_top(Line::styled(" Help ", self.styles.widget_title_style)) |
| 154 | .title_top( |
| 155 | Line::styled(" Esc to close ", self.styles.widget_title_style).right_aligned(), |
| 156 | ) |
| 157 | .padding(Padding::right(1)); |
| 158 | |
| 159 | let [content_area, input_area] = if app_state.help_dialog_state.is_searching() { |
| 160 | Layout::default() |
| 161 | .direction(Direction::Vertical) |
| 162 | .constraints([Constraint::Min(1), Constraint::Length(1)]) |
| 163 | .areas::<2>(draw_loc) |
| 164 | } else { |
| 165 | [draw_loc, Rect::default()] |
| 166 | }; |
| 167 | |
| 168 | if app_state.should_get_widget_bounds() { |
| 169 | // We must also recalculate how many lines are wrapping to properly get |
| 170 | // scrolling to work on small terminal sizes... oh joy. |
| 171 | |
| 172 | let inner = block.inner(content_area); |
| 173 | app_state.help_dialog_state.height = inner.height; |
| 174 | |
| 175 | // The overflow buffer is used to account for lines that wrap onto multiple lines, |
| 176 | // so we can properly calculate the max scroll index later. |
| 177 | let mut overflow_buffer = 0; |
| 178 | let paragraph_width: usize = max(inner.width, 1).into(); |
| 179 | let mut prev_section_len = 0; |
| 180 | |
| 181 | if app_state |
| 182 | .help_dialog_state |
| 183 | .search_input_state |
| 184 | .current_query() |
| 185 | .is_empty() |
| 186 | { |
| 187 | constants::HELP_TEXT |
| 188 | .iter() |
| 189 | .enumerate() |
| 190 | .for_each(|(itx, section)| { |
| 191 | let mut buffer = 0; |
| 192 | |
| 193 | section.iter().for_each(|text_line| { |
| 194 | buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16 |
| 195 | / paragraph_width as u16; |
| 196 | }); |
| 197 | |
| 198 | if itx == 0 { |
| 199 | app_state.help_dialog_state.index_shortcuts[itx] = 0; |
| 200 | } else { |
| 201 | app_state.help_dialog_state.index_shortcuts[itx] = |
| 202 | app_state.help_dialog_state.index_shortcuts[itx - 1] |
| 203 | + 1 |
| 204 | + prev_section_len; |
| 205 | } |
| 206 | prev_section_len = section.len() as u16 + buffer; |
| 207 | overflow_buffer += buffer; |
| 208 | }); |
| 209 | } else { |
| 210 | for line in &styled_help_text { |
| 211 | let width: usize = line |
| 212 | .spans |
| 213 | .iter() |
| 214 | .map(|s| UnicodeWidthStr::width(s.content.as_ref())) |
| 215 | .sum(); |
| 216 | |
| 217 | if width > paragraph_width { |
| 218 | overflow_buffer += (width.saturating_sub(1) / paragraph_width) as u16; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | let max_scroll_index = &mut app_state.help_dialog_state.scroll_state.max_scroll_index; |
| 224 | *max_scroll_index = (styled_help_text.len() as u16 + 3 + overflow_buffer) |
| 225 | .saturating_sub(draw_loc.height + 1); |
| 226 | |
| 227 | // Fix the scroll index if it is over-scrolled |
| 228 | let index = &mut app_state |
| 229 | .help_dialog_state |
| 230 | .scroll_state |
| 231 | .current_scroll_index; |
| 232 | |
| 233 | *index = min(*index, *max_scroll_index); |
| 234 | } |
| 235 | |
| 236 | f.render_widget( |
| 237 | Paragraph::new(styled_help_text) |
| 238 | .block(block) |
| 239 | .style(self.styles.text_style) |
| 240 | .alignment(Alignment::Left) |
| 241 | .wrap(Wrap { trim: true }) |
| 242 | .scroll(( |
| 243 | app_state |
| 244 | .help_dialog_state |
| 245 | .scroll_state |
| 246 | .current_scroll_index, |
| 247 | 0, |
| 248 | )), |
| 249 | content_area, |
| 250 | ); |
| 251 | |
| 252 | if app_state.help_dialog_state.is_searching() { |
| 253 | search_input::render_search_input( |
| 254 | f, |
| 255 | input_area, |
| 256 | search_input::SearchInputState { |
| 257 | input_field_state: &app_state.help_dialog_state.search_input_state, |
| 258 | is_focused: true, |
| 259 | prefix: "Search: ", |
| 260 | hint: Some("Type to search, Esc to close"), |
| 261 | }, |
| 262 | search_input::SearchInputStyles { |
| 263 | prefix_style: self.styles.widget_title_style, |
| 264 | text_style: self.styles.text_style, |
| 265 | cursor_style: self.styles.selected_text_style, |
| 266 | hint_style: self.styles.text_style.dim(), |
| 267 | }, |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | let scrollbar_area = dialog_scroll_bar_area(draw_loc); |
| 272 | let content_length = app_state |
| 273 | .help_dialog_state |
| 274 | .scroll_state |
| 275 | .max_scroll_index |
| 276 | .into(); |
| 277 | let viewport_length = app_state.help_dialog_state.height.into(); |
| 278 | let position = app_state |
| 279 | .help_dialog_state |
| 280 | .scroll_state |
| 281 | .current_scroll_index |
| 282 | .into(); |
| 283 | |
| 284 | draw_scroll_bar( |
| 285 | f, |
| 286 | scrollbar_area, |
| 287 | ScrollBarArgs { |
| 288 | content_length, |
| 289 | viewport_length, |
| 290 | position, |
| 291 | style: self.styles.text_style, |
| 292 | }, |
| 293 | ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | #[cfg(test)] |
| 298 | mod tests { |
| 299 | use super::*; |
| 300 | |
| 301 | fn check_spans(line: &Line<'_>, expected: [&str; 3]) { |
| 302 | assert_eq!(line.spans[0].content, expected[0]); |
| 303 | assert_eq!(line.spans[1].content, expected[1]); |
| 304 | assert_eq!(line.spans[2].content, expected[2]); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn test_no_match() { |
| 309 | let mut lines = Vec::new(); |
| 310 | let matched = add_highlight_match( |
| 311 | "xyz", |
| 312 | "hello world", |
| 313 | &mut lines, |
| 314 | Style::default(), |
| 315 | Style::default(), |
| 316 | ); |
| 317 | assert!(!matched); |
| 318 | assert!(lines.is_empty()); |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn test_match_in_middle() { |
| 323 | let mut lines = Vec::new(); |
| 324 | assert!(add_highlight_match( |
| 325 | "world", |
| 326 | "hello world!", |
| 327 | &mut lines, |
| 328 | Style::default(), |
| 329 | Style::default() |
| 330 | )); |
| 331 | check_spans(&lines[0], ["hello ", "world", "!"]); |
| 332 | } |
| 333 | |
| 334 | #[test] |
| 335 | fn test_match_at_start() { |
| 336 | let mut lines = Vec::new(); |
| 337 | assert!(add_highlight_match( |
| 338 | "hello", |
| 339 | "hello world", |
| 340 | &mut lines, |
| 341 | Style::default(), |
| 342 | Style::default() |
| 343 | )); |
| 344 | check_spans(&lines[0], ["", "hello", " world"]); |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn test_match_at_end() { |
| 349 | let mut lines = Vec::new(); |
| 350 | assert!(add_highlight_match( |
| 351 | "world", |
| 352 | "hello world", |
| 353 | &mut lines, |
| 354 | Style::default(), |
| 355 | Style::default() |
| 356 | )); |
| 357 | check_spans(&lines[0], ["hello ", "world", ""]); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn test_full_match() { |
| 362 | let mut lines = Vec::new(); |
| 363 | assert!(add_highlight_match( |
| 364 | "hello", |
| 365 | "hello", |
| 366 | &mut lines, |
| 367 | Style::default(), |
| 368 | Style::default() |
| 369 | )); |
| 370 | check_spans(&lines[0], ["", "hello", ""]); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn test_case_insensitive() { |
| 375 | let mut lines = Vec::new(); |
| 376 | assert!(add_highlight_match( |
| 377 | "cpu", |
| 378 | "CPU widget", |
| 379 | &mut lines, |
| 380 | Style::default(), |
| 381 | Style::default() |
| 382 | )); |
| 383 | check_spans(&lines[0], ["", "CPU", " widget"]); |
| 384 | } |
| 385 | |
| 386 | #[test] |
| 387 | fn test_unicode_in_target() { |
| 388 | let mut lines = Vec::new(); |
| 389 | assert!(add_highlight_match( |
| 390 | "éléphants", |
| 391 | "J'adore les éléphants et les lapins.", |
| 392 | &mut lines, |
| 393 | Style::default(), |
| 394 | Style::default() |
| 395 | )); |
| 396 | check_spans(&lines[0], ["J'adore les ", "éléphants", " et les lapins."]); |
| 397 | } |
| 398 | |
| 399 | #[test] |
| 400 | fn test_unicode() { |
| 401 | let mut lines = Vec::new(); |
| 402 | assert!(add_highlight_match( |
| 403 | "你", |
| 404 | "Hi 你好!🇨🇦", |
| 405 | &mut lines, |
| 406 | Style::default(), |
| 407 | Style::default() |
| 408 | )); |
| 409 | check_spans(&lines[0], ["Hi ", "你", "好!🇨🇦"]); |
| 410 | } |
| 411 | |
| 412 | #[test] |
| 413 | fn test_unicode_2() { |
| 414 | let mut lines = Vec::new(); |
| 415 | assert!(add_highlight_match( |
| 416 | "🇨🇦", |
| 417 | "Hi 你好!🇨🇦", |
| 418 | &mut lines, |
| 419 | Style::default(), |
| 420 | Style::default() |
| 421 | )); |
| 422 | check_spans(&lines[0], ["Hi 你好!", "🇨🇦", ""]); |
| 423 | } |
| 424 | |
| 425 | /// Shows that we only match the first occurrence in the line. This is expected behaviour as we're passing |
| 426 | /// in separate strings. |
| 427 | #[test] |
| 428 | fn test_unicode_3() { |
| 429 | let mut lines = Vec::new(); |
| 430 | assert!(add_highlight_match( |
| 431 | "🇨🇦", |
| 432 | "Hi 🇨🇦 你好!🇨🇦", |
| 433 | &mut lines, |
| 434 | Style::default(), |
| 435 | Style::default() |
| 436 | )); |
| 437 | check_spans(&lines[0], ["Hi ", "🇨🇦", " 你好!🇨🇦"]); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn test_unicode_case_insensitive() { |
| 442 | let mut lines = Vec::new(); |
| 443 | assert!(add_highlight_match( |
| 444 | "éléphants", |
| 445 | "J'adore les Éléphants et les lapins.", |
| 446 | &mut lines, |
| 447 | Style::default(), |
| 448 | Style::default() |
| 449 | )); |
| 450 | check_spans(&lines[0], ["J'adore les ", "Éléphants", " et les lapins."]); |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn test_accumulates_across_calls() { |
| 455 | let mut lines = Vec::new(); |
| 456 | add_highlight_match("a", "cat", &mut lines, Style::default(), Style::default()); // match |
| 457 | add_highlight_match("a", "dog", &mut lines, Style::default(), Style::default()); // no match |
| 458 | add_highlight_match("a", "rat", &mut lines, Style::default(), Style::default()); // match |
| 459 | assert_eq!(lines.len(), 2); |
| 460 | } |
| 461 | } |