promkit_widgets/
text_editor.rs1use promkit_core::{Pane, PaneFactory, grapheme::StyledGraphemes};
2
3mod history;
4pub use history::History;
5#[path = "text_editor/text_editor.rs"]
6mod inner;
7pub use inner::{Mode, TextEditor};
8pub mod config;
9pub use config::Config;
10
11#[derive(Clone, Default)]
12pub struct State {
13 pub texteditor: TextEditor,
15 pub history: Option<History>,
17
18 pub config: Config,
20}
21
22impl PaneFactory for State {
23 fn create_pane(&self, width: u16, height: u16) -> Pane {
24 let mut buf = StyledGraphemes::default();
25
26 let mut styled_prefix =
27 StyledGraphemes::from_str(&self.config.prefix, self.config.prefix_style);
28
29 buf.append(&mut styled_prefix);
30
31 let text = match self.config.mask {
32 Some(mask) => self.texteditor.masking(mask),
33 None => self.texteditor.text(),
34 };
35
36 let mut styled = text
37 .apply_style(self.config.inactive_char_style)
38 .apply_style_at(self.texteditor.position(), self.config.active_char_style);
39
40 buf.append(&mut styled);
41
42 let height = match self.config.lines {
43 Some(lines) => lines.min(height as usize),
44 None => height as usize,
45 };
46
47 let (matrix, offset) = buf.matrixify(
48 width as usize,
49 height,
50 (StyledGraphemes::from_str(&self.config.prefix, self.config.prefix_style).widths()
51 + self.texteditor.position())
52 / width as usize,
53 );
54
55 Pane::new(matrix, offset)
56 }
57}