-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathborders.rs
More file actions
246 lines (215 loc) · 6.71 KB
/
borders.rs
File metadata and controls
246 lines (215 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//! Border styles and utilities.
//!
//! Provides consistent border rendering across all components.
use cortex_core::style::{BORDER, BORDER_FOCUS, CYAN_PRIMARY};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::symbols::border::Set as BorderSet;
use ratatui::widgets::{Block, Borders, Widget};
/// Rounded border character set used throughout Cortex TUI.
pub const ROUNDED_BORDER: BorderSet = BorderSet {
top_left: "╭",
top_right: "╮",
bottom_left: "╰",
bottom_right: "╯",
horizontal_top: "─",
horizontal_bottom: "─",
vertical_left: "│",
vertical_right: "│",
};
/// Single-line border character set.
pub const SINGLE_BORDER: BorderSet = BorderSet {
top_left: "┌",
top_right: "┐",
bottom_left: "└",
bottom_right: "┘",
horizontal_top: "─",
horizontal_bottom: "─",
vertical_left: "│",
vertical_right: "│",
};
/// Double-line border character set.
pub const DOUBLE_BORDER: BorderSet = BorderSet {
top_left: "╔",
top_right: "╗",
bottom_left: "╚",
bottom_right: "╝",
horizontal_top: "═",
horizontal_bottom: "═",
vertical_left: "║",
vertical_right: "║",
};
/// ASCII-only border for maximum compatibility.
pub const ASCII_BORDER: BorderSet = BorderSet {
top_left: "+",
top_right: "+",
bottom_left: "+",
bottom_right: "+",
horizontal_top: "-",
horizontal_bottom: "-",
vertical_left: "|",
vertical_right: "|",
};
/// Border style variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BorderStyle {
/// No border
None,
/// Rounded corners (default Cortex style)
#[default]
Rounded,
/// Single line border
Single,
/// Double line border
Double,
/// ASCII-only for maximum terminal compatibility
Ascii,
}
impl BorderStyle {
/// Get the border character set for this style.
pub fn border_set(&self) -> Option<BorderSet> {
match self {
BorderStyle::None => None,
BorderStyle::Rounded => Some(ROUNDED_BORDER),
BorderStyle::Single => Some(SINGLE_BORDER),
BorderStyle::Double => Some(DOUBLE_BORDER),
BorderStyle::Ascii => Some(ASCII_BORDER),
}
}
/// Create a ratatui Block with this border style.
pub fn block(&self, focused: bool) -> Block<'static> {
let border_color = if focused { BORDER_FOCUS } else { BORDER };
let mut block = Block::default().border_style(Style::default().fg(border_color));
if let Some(set) = self.border_set() {
block = block.borders(Borders::ALL).border_set(set);
}
block
}
}
/// A pre-configured rounded border widget.
///
/// Use this for consistent bordered containers across the TUI.
#[derive(Clone)]
pub struct RoundedBorder<'a> {
title: Option<&'a str>,
focused: bool,
border_style: BorderStyle,
}
impl<'a> RoundedBorder<'a> {
/// Create a new rounded border.
pub fn new() -> Self {
Self {
title: None,
focused: false,
border_style: BorderStyle::Rounded,
}
}
/// Set the border title.
pub fn title(mut self, title: &'a str) -> Self {
self.title = Some(title);
self
}
/// Set the focused state.
pub fn focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
/// Set the border style.
pub fn style(mut self, style: BorderStyle) -> Self {
self.border_style = style;
self
}
/// Create a ratatui Block from this configuration.
pub fn to_block(&self) -> Block<'static> {
let border_color = if self.focused { BORDER_FOCUS } else { BORDER };
let title_color = if self.focused { CYAN_PRIMARY } else { BORDER };
let mut block = Block::default()
.borders(Borders::ALL)
.border_set(self.border_style.border_set().unwrap_or(ROUNDED_BORDER))
.border_style(Style::default().fg(border_color));
if let Some(title) = self.title {
block = block
.title(format!(" {} ", title))
.title_style(Style::default().fg(title_color));
}
block
}
/// Calculate the inner area after accounting for borders.
pub fn inner(&self, area: Rect) -> Rect {
self.to_block().inner(area)
}
}
impl Default for RoundedBorder<'_> {
fn default() -> Self {
Self::new()
}
}
impl Widget for RoundedBorder<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.to_block().render(area, buf);
}
}
/// Draw a horizontal separator line.
///
/// # Arguments
/// * `buf` - The buffer to render to
/// * `y` - The y coordinate
/// * `x_start` - Starting x coordinate
/// * `width` - Width of the line
/// * `style` - Style to use for the separator
pub fn draw_horizontal_separator(buf: &mut Buffer, y: u16, x_start: u16, width: u16, style: Style) {
for x in x_start..x_start.saturating_add(width) {
if let Some(cell) = buf.cell_mut((x, y)) {
cell.set_char('─').set_style(style);
}
}
}
/// Draw a vertical separator line.
///
/// # Arguments
/// * `buf` - The buffer to render to
/// * `x` - The x coordinate
/// * `y_start` - Starting y coordinate
/// * `height` - Height of the line
/// * `style` - Style to use for the separator
pub fn draw_vertical_separator(buf: &mut Buffer, x: u16, y_start: u16, height: u16, style: Style) {
for y in y_start..y_start.saturating_add(height) {
if let Some(cell) = buf.cell_mut((x, y)) {
cell.set_char('│').set_style(style);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_border_style_set() {
assert!(BorderStyle::None.border_set().is_none());
assert!(BorderStyle::Rounded.border_set().is_some());
assert!(BorderStyle::Single.border_set().is_some());
assert!(BorderStyle::Double.border_set().is_some());
assert!(BorderStyle::Ascii.border_set().is_some());
}
#[test]
fn test_rounded_border_builder() {
let border = RoundedBorder::new()
.title("Test")
.focused(true)
.style(BorderStyle::Single);
assert_eq!(border.title, Some("Test"));
assert!(border.focused);
assert_eq!(border.border_style, BorderStyle::Single);
}
#[test]
fn test_rounded_border_inner() {
let border = RoundedBorder::new();
let area = Rect::new(0, 0, 10, 5);
let inner = border.inner(area);
// Inner area should be smaller by 1 on each side for borders
assert_eq!(inner.x, 1);
assert_eq!(inner.y, 1);
assert_eq!(inner.width, 8);
assert_eq!(inner.height, 3);
}
}