Debian Codegraph @

helix-tui/src/widgets/block.rs

raw symbols SCIP
1 use crate::{
2 buffer::Buffer,
3 symbols::line,
4 text::Spans,
5 widgets::{Borders, Widget},
6 };
7 use helix_view::graphics::{Rect, Style};
8
9 /// Border render type. Defaults to [`BorderType::Plain`].
10 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
11 pub enum BorderType {
12 #[default]
13 Plain,
14 Rounded,
15 Double,
16 Thick,
17 }
18
19 impl BorderType {
20 pub fn line_symbols(border_type: Self) -> line::Set {
21 match border_type {
22 Self::Plain => line::NORMAL,
23 Self::Rounded => line::ROUNDED,
24 Self::Double => line::DOUBLE,
25 Self::Thick => line::THICK,
26 }
27 }
28 }
29
30 /// Base widget to be used with all upper level ones. It may be used to display a box border around
31 /// the widget and/or add a title.
32 ///
33 /// # Examples
34 ///
35 /// ```
36 /// # use helix_tui::widgets::{Block, BorderType, Borders};
37 /// # use helix_view::graphics::{Style, Color};
38 /// Block::default()
39 /// .title("Block")
40 /// .borders(Borders::LEFT | Borders::RIGHT)
41 /// .border_style(Style::default().fg(Color::White))
42 /// .border_type(BorderType::Rounded)
43 /// .style(Style::default().bg(Color::Black));
44 /// ```
45 #[derive(Debug, Default, Clone, PartialEq, Eq)]
46 pub struct Block<'a> {
47 /// Optional title place on the upper left of the block
48 title: Option<Spans<'a>>,
49 /// Visible borders
50 borders: Borders,
51 /// Border style
52 border_style: Style,
53 /// Type of the border. The default is plain lines but one can choose to have rounded corners
54 /// or doubled lines instead.
55 border_type: BorderType,
56 /// Widget style
57 style: Style,
58 }
59
60 impl<'a> Block<'a> {
61 pub const fn new() -> Self {
62 Self {
63 title: None,
64 borders: Borders::empty(),
65 border_style: Style::new(),
66 border_type: BorderType::Plain,
67 style: Style::new(),
68 }
69 }
70
71 pub const fn bordered() -> Self {
72 let mut block = Self::new();
73 block.borders = Borders::ALL;
74 block
75 }
76
77 pub fn title<T>(mut self, title: T) -> Block<'a>
78 where
79 T: Into<Spans<'a>>,
80 {
81 self.title = Some(title.into());
82 self
83 }
84
85 pub const fn border_style(mut self, style: Style) -> Block<'a> {
86 self.border_style = style;
87 self
88 }
89
90 pub const fn style(mut self, style: Style) -> Block<'a> {
91 self.style = style;
92 self
93 }
94
95 pub const fn borders(mut self, flag: Borders) -> Block<'a> {
96 self.borders = flag;
97 self
98 }
99
100 pub const fn border_type(mut self, border_type: BorderType) -> Block<'a> {
101 self.border_type = border_type;
102 self
103 }
104
105 /// Compute the inner area of a block based on its border visibility rules.
106 pub fn inner(&self, area: Rect) -> Rect {
107 let mut inner = area;
108 if self.borders.intersects(Borders::LEFT) {
109 inner.x = inner.x.saturating_add(1).min(inner.right());
110 inner.width = inner.width.saturating_sub(1);
111 }
112 if self.borders.intersects(Borders::TOP) || self.title.is_some() {
113 inner.y = inner.y.saturating_add(1).min(inner.bottom());
114 inner.height = inner.height.saturating_sub(1);
115 }
116 if self.borders.intersects(Borders::RIGHT) {
117 inner.width = inner.width.saturating_sub(1);
118 }
119 if self.borders.intersects(Borders::BOTTOM) {
120 inner.height = inner.height.saturating_sub(1);
121 }
122 inner
123 }
124 }
125
126 impl Widget for Block<'_> {
127 fn render(self, area: Rect, buf: &mut Buffer) {
128 if area.area() == 0 {
129 return;
130 }
131 buf.set_style(area, self.style);
132 let symbols = BorderType::line_symbols(self.border_type);
133
134 // Sides
135 if self.borders.intersects(Borders::LEFT) {
136 for y in area.top()..area.bottom() {
137 buf[(area.left(), y)]
138 .set_symbol(symbols.vertical)
139 .set_style(self.border_style);
140 }
141 }
142 if self.borders.intersects(Borders::TOP) {
143 for x in area.left()..area.right() {
144 buf[(x, area.top())]
145 .set_symbol(symbols.horizontal)
146 .set_style(self.border_style);
147 }
148 }
149 if self.borders.intersects(Borders::RIGHT) {
150 let x = area.right() - 1;
151 for y in area.top()..area.bottom() {
152 buf[(x, y)]
153 .set_symbol(symbols.vertical)
154 .set_style(self.border_style);
155 }
156 }
157 if self.borders.intersects(Borders::BOTTOM) {
158 let y = area.bottom() - 1;
159 for x in area.left()..area.right() {
160 buf[(x, y)]
161 .set_symbol(symbols.horizontal)
162 .set_style(self.border_style);
163 }
164 }
165
166 // Corners
167 if self.borders.contains(Borders::RIGHT | Borders::BOTTOM) {
168 buf[(area.right() - 1, area.bottom() - 1)]
169 .set_symbol(symbols.bottom_right)
170 .set_style(self.border_style);
171 }
172 if self.borders.contains(Borders::RIGHT | Borders::TOP) {
173 buf[(area.right() - 1, area.top())]
174 .set_symbol(symbols.top_right)
175 .set_style(self.border_style);
176 }
177 if self.borders.contains(Borders::LEFT | Borders::BOTTOM) {
178 buf[(area.left(), area.bottom() - 1)]
179 .set_symbol(symbols.bottom_left)
180 .set_style(self.border_style);
181 }
182 if self.borders.contains(Borders::LEFT | Borders::TOP) {
183 buf[(area.left(), area.top())]
184 .set_symbol(symbols.top_left)
185 .set_style(self.border_style);
186 }
187
188 if let Some(title) = self.title {
189 let lx = u16::from(self.borders.intersects(Borders::LEFT));
190 let rx = u16::from(self.borders.intersects(Borders::RIGHT));
191 let width = area.width.saturating_sub(lx).saturating_sub(rx);
192 buf.set_spans(area.left() + lx, area.top(), &title, width);
193 }
194 }
195 }
196
197 #[cfg(test)]
198 mod tests {
199 use super::*;
200
201 #[test]
202 fn inner_takes_into_account_the_borders() {
203 // No borders
204 assert_eq!(
205 Block::default().inner(Rect::default()),
206 Rect {
207 x: 0,
208 y: 0,
209 width: 0,
210 height: 0
211 },
212 "no borders, width=0, height=0"
213 );
214 assert_eq!(
215 Block::default().inner(Rect {
216 x: 0,
217 y: 0,
218 width: 1,
219 height: 1
220 }),
221 Rect {
222 x: 0,
223 y: 0,
224 width: 1,
225 height: 1
226 },
227 "no borders, width=1, height=1"
228 );
229
230 // Left border
231 assert_eq!(
232 Block::default().borders(Borders::LEFT).inner(Rect {
233 x: 0,
234 y: 0,
235 width: 0,
236 height: 1
237 }),
238 Rect {
239 x: 0,
240 y: 0,
241 width: 0,
242 height: 1
243 },
244 "left, width=0"
245 );
246 assert_eq!(
247 Block::default().borders(Borders::LEFT).inner(Rect {
248 x: 0,
249 y: 0,
250 width: 1,
251 height: 1
252 }),
253 Rect {
254 x: 1,
255 y: 0,
256 width: 0,
257 height: 1
258 },
259 "left, width=1"
260 );
261 assert_eq!(
262 Block::default().borders(Borders::LEFT).inner(Rect {
263 x: 0,
264 y: 0,
265 width: 2,
266 height: 1
267 }),
268 Rect {
269 x: 1,
270 y: 0,
271 width: 1,
272 height: 1
273 },
274 "left, width=2"
275 );
276
277 // Top border
278 assert_eq!(
279 Block::default().borders(Borders::TOP).inner(Rect {
280 x: 0,
281 y: 0,
282 width: 1,
283 height: 0
284 }),
285 Rect {
286 x: 0,
287 y: 0,
288 width: 1,
289 height: 0
290 },
291 "top, height=0"
292 );
293 assert_eq!(
294 Block::default().borders(Borders::TOP).inner(Rect {
295 x: 0,
296 y: 0,
297 width: 1,
298 height: 1
299 }),
300 Rect {
301 x: 0,
302 y: 1,
303 width: 1,
304 height: 0
305 },
306 "top, height=1"
307 );
308 assert_eq!(
309 Block::default().borders(Borders::TOP).inner(Rect {
310 x: 0,
311 y: 0,
312 width: 1,
313 height: 2
314 }),
315 Rect {
316 x: 0,
317 y: 1,
318 width: 1,
319 height: 1
320 },
321 "top, height=2"
322 );
323
324 // Right border
325 assert_eq!(
326 Block::default().borders(Borders::RIGHT).inner(Rect {
327 x: 0,
328 y: 0,
329 width: 0,
330 height: 1
331 }),
332 Rect {
333 x: 0,
334 y: 0,
335 width: 0,
336 height: 1
337 },
338 "right, width=0"
339 );
340 assert_eq!(
341 Block::default().borders(Borders::RIGHT).inner(Rect {
342 x: 0,
343 y: 0,
344 width: 1,
345 height: 1
346 }),
347 Rect {
348 x: 0,
349 y: 0,
350 width: 0,
351 height: 1
352 },
353 "right, width=1"
354 );
355 assert_eq!(
356 Block::default().borders(Borders::RIGHT).inner(Rect {
357 x: 0,
358 y: 0,
359 width: 2,
360 height: 1
361 }),
362 Rect {
363 x: 0,
364 y: 0,
365 width: 1,
366 height: 1
367 },
368 "right, width=2"
369 );
370
371 // Bottom border
372 assert_eq!(
373 Block::default().borders(Borders::BOTTOM).inner(Rect {
374 x: 0,
375 y: 0,
376 width: 1,
377 height: 0
378 }),
379 Rect {
380 x: 0,
381 y: 0,
382 width: 1,
383 height: 0
384 },
385 "bottom, height=0"
386 );
387 assert_eq!(
388 Block::default().borders(Borders::BOTTOM).inner(Rect {
389 x: 0,
390 y: 0,
391 width: 1,
392 height: 1
393 }),
394 Rect {
395 x: 0,
396 y: 0,
397 width: 1,
398 height: 0
399 },
400 "bottom, height=1"
401 );
402 assert_eq!(
403 Block::default().borders(Borders::BOTTOM).inner(Rect {
404 x: 0,
405 y: 0,
406 width: 1,
407 height: 2
408 }),
409 Rect {
410 x: 0,
411 y: 0,
412 width: 1,
413 height: 1
414 },
415 "bottom, height=2"
416 );
417
418 // All borders
419 assert_eq!(
420 Block::bordered().inner(Rect::default()),
421 Rect {
422 x: 0,
423 y: 0,
424 width: 0,
425 height: 0
426 },
427 "all borders, width=0, height=0"
428 );
429 assert_eq!(
430 Block::bordered().inner(Rect {
431 x: 0,
432 y: 0,
433 width: 1,
434 height: 1
435 }),
436 Rect {
437 x: 1,
438 y: 1,
439 width: 0,
440 height: 0,
441 },
442 "all borders, width=1, height=1"
443 );
444 assert_eq!(
445 Block::bordered().inner(Rect {
446 x: 0,
447 y: 0,
448 width: 2,
449 height: 2,
450 }),
451 Rect {
452 x: 1,
453 y: 1,
454 width: 0,
455 height: 0,
456 },
457 "all borders, width=2, height=2"
458 );
459 assert_eq!(
460 Block::bordered().inner(Rect {
461 x: 0,
462 y: 0,
463 width: 3,
464 height: 3,
465 }),
466 Rect {
467 x: 1,
468 y: 1,
469 width: 1,
470 height: 1,
471 },
472 "all borders, width=3, height=3"
473 );
474 }
475
476 #[test]
477 fn inner_takes_into_account_the_title() {
478 assert_eq!(
479 Block::default().title("Test").inner(Rect {
480 x: 0,
481 y: 0,
482 width: 0,
483 height: 1,
484 }),
485 Rect {
486 x: 0,
487 y: 1,
488 width: 0,
489 height: 0,
490 },
491 );
492 }
493 }