| 1 | use ratatui::{ |
| 2 | Frame, |
| 3 | layout::{Alignment, Rect}, |
| 4 | style::Style, |
| 5 | text::{Line, Span}, |
| 6 | widgets::Paragraph, |
| 7 | }; |
| 8 | |
| 9 | use crate::utils::input::InputFieldState; |
| 10 | |
| 11 | pub struct SearchInputState<'a> { |
| 12 | pub input_field_state: &'a InputFieldState, |
| 13 | pub is_focused: bool, |
| 14 | pub prefix: &'a str, |
| 15 | pub hint: Option<&'a str>, |
| 16 | } |
| 17 | |
| 18 | pub struct SearchInputStyles { |
| 19 | pub prefix_style: Style, |
| 20 | pub text_style: Style, |
| 21 | pub cursor_style: Style, |
| 22 | pub hint_style: Style, |
| 23 | } |
| 24 | |
| 25 | /// Build a query span from a [`InputFieldState`]. `available_width` is the terminal column width. |
| 26 | pub fn build_query_spans( |
| 27 | input_field_state: &InputFieldState, available_width: usize, is_on_widget: bool, |
| 28 | cursor_style: Style, text_style: Style, |
| 29 | ) -> Vec<Span<'_>> { |
| 30 | let query = input_field_state.current_query(); |
| 31 | |
| 32 | if !is_on_widget { |
| 33 | // This is easier - we just need to get a range of graphemes, rather than |
| 34 | // dealing with possibly inserting a cursor (as none is shown!) |
| 35 | return vec![Span::styled(query.to_string(), text_style)]; |
| 36 | } |
| 37 | |
| 38 | let start_index = input_field_state.display_start_index(); |
| 39 | let cursor_index = input_field_state.cursor_index(); |
| 40 | let mut current_width = 0; |
| 41 | let mut res = Vec::with_capacity(available_width); |
| 42 | |
| 43 | for (index, grapheme, lengths) in input_field_state.graphemes_with_ranges() { |
| 44 | if index < start_index { |
| 45 | continue; |
| 46 | } else if current_width > available_width { |
| 47 | break; |
| 48 | } else { |
| 49 | let styled = if index == cursor_index { |
| 50 | Span::styled(grapheme, cursor_style) |
| 51 | } else { |
| 52 | Span::styled(grapheme, text_style) |
| 53 | }; |
| 54 | |
| 55 | res.push(styled); |
| 56 | |
| 57 | // The lengths are the width of the graphemes terminal-wise. |
| 58 | current_width += lengths.end - lengths.start; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if cursor_index == query.len() { |
| 63 | res.push(Span::styled(" ", cursor_style)) |
| 64 | } |
| 65 | |
| 66 | res |
| 67 | } |
| 68 | |
| 69 | pub fn render_search_input( |
| 70 | f: &mut Frame<'_>, rect: Rect, state: SearchInputState<'_>, styles: SearchInputStyles, |
| 71 | ) { |
| 72 | let mut input_vec = vec![Span::styled(state.prefix, styles.prefix_style)]; |
| 73 | let query_spans = build_query_spans( |
| 74 | state.input_field_state, |
| 75 | rect.width.into(), |
| 76 | state.is_focused, |
| 77 | styles.cursor_style, |
| 78 | styles.text_style, |
| 79 | ); |
| 80 | |
| 81 | input_vec.extend(query_spans); |
| 82 | |
| 83 | if let Some(hint) = state.hint |
| 84 | && state.input_field_state.current_query().is_empty() |
| 85 | { |
| 86 | input_vec.push(Span::styled(" ", styles.text_style)); |
| 87 | input_vec.push(Span::styled(hint, styles.hint_style)); |
| 88 | } |
| 89 | |
| 90 | let input_line = Line::from(input_vec); |
| 91 | |
| 92 | f.render_widget( |
| 93 | Paragraph::new(input_line) |
| 94 | .style(styles.text_style) |
| 95 | .alignment(Alignment::Left), |
| 96 | rect, |
| 97 | ); |
| 98 | } |