edtui/view/
status_line.rs1use ratatui_core::{
2 buffer::Buffer,
3 layout::{Alignment, Constraint, Layout, Rect},
4 style::Style,
5 text::{Line, Span},
6 widgets::Widget,
7};
8use ratatui_widgets::paragraph::Paragraph;
9
10use super::theme::{DARK_GRAY, WHITE};
11
12#[derive(Debug, Clone)]
14pub struct EditorStatusLine {
15 mode: String,
17 search: Option<String>,
19 style_text: Style,
21 style_line: Style,
23 align_left: bool,
25}
26
27impl Default for EditorStatusLine {
28 fn default() -> Self {
32 Self {
33 mode: String::new(),
34 search: None,
35 style_text: Style::default().fg(WHITE).bg(DARK_GRAY).bold(),
36 style_line: Style::default().fg(WHITE).bg(DARK_GRAY),
37 align_left: true,
38 }
39 }
40}
41
42impl EditorStatusLine {
43 #[must_use]
48 pub fn style_text(mut self, style: Style) -> Self {
49 self.style_text = style;
50 self
51 }
52
53 #[must_use]
58 pub fn style_line(mut self, style: Style) -> Self {
59 self.style_line = style;
60 self
61 }
62
63 #[must_use]
67 pub fn mode<S: Into<String>>(mut self, mode: S) -> Self {
68 self.mode = mode.into();
69 self
70 }
71
72 #[must_use]
76 pub fn search<S: Into<String>>(mut self, search: Option<S>) -> Self {
77 self.search = search.map(Into::into);
78 self
79 }
80
81 #[must_use]
85 pub fn align_left(mut self, align_left: bool) -> Self {
86 self.align_left = align_left;
87 self
88 }
89}
90
91impl Widget for EditorStatusLine {
92 fn render(self, area: Rect, buf: &mut Buffer) {
93 let constraints = if self.align_left {
95 [Constraint::Length(10), Constraint::Min(0)]
96 } else {
97 [Constraint::Min(0), Constraint::Length(10)]
98 };
99 let [left, right] = Layout::horizontal(constraints).areas(area);
100
101 let mode_paragraph = Paragraph::new(Line::from(Span::from(self.mode)))
103 .alignment(Alignment::Center)
104 .style(self.style_text);
105 let search_text = self.search.map_or(String::new(), |s| format!("/{s}"));
106 let search_paragraph = Paragraph::new(Line::from(Span::from(search_text)))
107 .alignment(Alignment::Left)
108 .style(self.style_line);
109
110 if self.align_left {
112 mode_paragraph.render(left, buf);
113 search_paragraph.render(right, buf);
114 } else {
115 search_paragraph.render(left, buf);
116 mode_paragraph.render(right, buf);
117 };
118 }
119}