| 1 | use ratatuirust-analyzer cargo ratatui 0.30.2 crate/::{ |
| 2 | Framerust-analyzer cargo ratatui-core 0.1.2 terminal/frame/Frame#, |
| 3 | layoutrust-analyzer cargo ratatui-core 0.1.2 layout/::{Alignmentrust-analyzer cargo ratatui-core 0.1.2 layout/alignment/Alignment#, Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#}, |
| 4 | stylerust-analyzer cargo ratatui-core 0.1.2 style/::Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 5 | textrust-analyzer cargo ratatui-core 0.1.2 text/::{Linerust-analyzer cargo ratatui-core 0.1.2 text/line/Line#, Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#}, |
| 6 | widgetsrust-analyzer cargo ratatui 0.30.2 widgets/::Paragraphrust-analyzer cargo ratatui-widgets 0.3.2 paragraph/Paragraph#, |
| 7 | }; |
| 8 | |
| 9 | use craterust-analyzer cargo bottom 0.14.9 crate/::utilsrust-analyzer cargo bottom 0.14.9 utils/::inputrust-analyzer cargo bottom 0.14.9 utils/input/::InputFieldStaterust-analyzer cargo bottom 0.14.9 utils/input/InputFieldState#; |
| 10 | |
| 11 | pub struct SearchInputStaterust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#<'a> { |
| 12 | pub input_field_staterust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#input_field_state.: &'a InputFieldStaterust-analyzer cargo bottom 0.14.9 utils/input/InputFieldState#, |
| 13 | pub is_focusedrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#is_focused.: bool, |
| 14 | pub prefixrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#prefix.: &'a str, |
| 15 | pub hintrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#hint.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>, |
| 16 | } |
| 17 | |
| 18 | pub struct SearchInputStylesrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles# { |
| 19 | pub prefix_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#prefix_style.: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 20 | pub text_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#text_style.: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 21 | pub cursor_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#cursor_style.: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 22 | pub hint_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#hint_style.: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 23 | } |
| 24 | |
| 25 | /// Build a query span from a [`InputFieldState`]. `available_width` is the terminal column width. |
| 26 | pub fn build_query_spansrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/build_query_spans().( |
| 27 | input_field_state: &InputFieldStaterust-analyzer cargo bottom 0.14.9 utils/input/InputFieldState#, available_width: usize, is_on_widget: bool, |
| 28 | cursor_style: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, text_style: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 29 | ) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#<'_>> { |
| 30 | let query = input_field_state.current_queryrust-analyzer cargo bottom 0.14.9 utils/input/impl#[InputFieldState]current_query().(); |
| 31 | |
| 32 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().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 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(query.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), text_style)]; |
| 36 | } |
| 37 | |
| 38 | let start_index = input_field_state.display_start_indexrust-analyzer cargo bottom 0.14.9 utils/input/impl#[InputFieldState]display_start_index().(); |
| 39 | let cursor_index = input_field_state.cursor_indexrust-analyzer cargo bottom 0.14.9 utils/input/impl#[InputFieldState]cursor_index().(); |
| 40 | let mut current_width = 0; |
| 41 | let mut res = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(available_width); |
| 42 | |
| 43 | for (index, grapheme, lengths) in input_field_state.graphemes_with_rangesrust-analyzer cargo bottom 0.14.9 utils/input/impl#[InputFieldState]graphemes_with_ranges().() { |
| 44 | if index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().start_index { |
| 45 | continue; |
| 46 | } else if current_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().available_width { |
| 47 | break; |
| 48 | } else { |
| 49 | let styled = if index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().cursor_index { |
| 50 | Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(grapheme, cursor_style) |
| 51 | } else { |
| 52 | Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(grapheme, text_style) |
| 53 | }; |
| 54 | |
| 55 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(styled); |
| 56 | |
| 57 | // The lengths are the width of the graphemes terminal-wise. |
| 58 | current_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().lengths.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().lengths.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start.; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if cursor_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().query.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() { |
| 63 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(" ", cursor_style)) |
| 64 | } |
| 65 | |
| 66 | res |
| 67 | } |
| 68 | |
| 69 | pub fn render_search_inputrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/render_search_input().( |
| 70 | f: &mut Framerust-analyzer cargo ratatui-core 0.1.2 terminal/frame/Frame#<'_>, rect: Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#, state: SearchInputStaterust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#<'_>, styles: SearchInputStylesrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#, |
| 71 | ) { |
| 72 | let mut input_vec = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(state.prefixrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#prefix., styles.prefix_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#prefix_style.)]; |
| 73 | let query_spans = build_query_spansrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/build_query_spans().( |
| 74 | state.input_field_staterust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#input_field_state., |
| 75 | rect.widthrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#width..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 76 | state.is_focusedrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#is_focused., |
| 77 | styles.cursor_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#cursor_style., |
| 78 | styles.text_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#text_style., |
| 79 | ); |
| 80 | |
| 81 | input_vec.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(query_spans); |
| 82 | |
| 83 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(hint) = state.hintrust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#hint. |
| 84 | && state.input_field_staterust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputState#input_field_state..current_queryrust-analyzer cargo bottom 0.14.9 utils/input/impl#[InputFieldState]current_query().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() |
| 85 | { |
| 86 | input_vec.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(" ", styles.text_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#text_style.)); |
| 87 | input_vec.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(Spanrust-analyzer cargo ratatui-core 0.1.2 text/span/Span#::styledrust-analyzer cargo ratatui-core 0.1.2 text/span/impl#[`Span<'a>`]styled().(hint, styles.hint_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#hint_style.)); |
| 88 | } |
| 89 | |
| 90 | let input_line = Linerust-analyzer cargo ratatui-core 0.1.2 text/line/Line#::fromrust-analyzer cargo ratatui-core 0.1.2 text/line/impl#[`Line<'a>`][`From<Vec<Span<'a>>>`]from().(input_vec); |
| 91 | |
| 92 | f.render_widgetrust-analyzer cargo ratatui-core 0.1.2 terminal/frame/impl#[`Frame<'_>`]render_widget().( |
| 93 | Paragraphrust-analyzer cargo ratatui-widgets 0.3.2 paragraph/Paragraph#::newrust-analyzer cargo ratatui-widgets 0.3.2 paragraph/impl#[`Paragraph<'a>`]new().(input_line) |
| 94 | .stylerust-analyzer cargo ratatui-widgets 0.3.2 paragraph/impl#[`Paragraph<'a>`]style().(styles.text_stylerust-analyzer cargo bottom 0.14.9 canvas/components/search_input/SearchInputStyles#text_style.) |
| 95 | .alignmentrust-analyzer cargo ratatui-widgets 0.3.2 paragraph/impl#[`Paragraph<'a>`]alignment().(Alignmentrust-analyzer cargo ratatui-core 0.1.2 layout/alignment/Alignment#::Leftrust-analyzer cargo ratatui-core 0.1.2 layout/alignment/HorizontalAlignment#Left#), |
| 96 | rect, |
| 97 | ); |
| 98 | } |