termint 0.8.1

Library for colored printing and Terminal User Interfaces
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::{
    cmp::max,
    hash::{DefaultHasher, Hash, Hasher},
    marker::PhantomData,
};

use crate::{
    borders,
    buffer::Buffer,
    enums::{Border, BorderType, Color},
    geometry::Padding,
    prelude::{Constraint, Direction, Rect, Vec2},
    style::Style,
    text::Text,
    widgets::{Element, Layout, LayoutNode, Spacer, Span, Widget},
};

/// A widget that wraps another widget and adds border and title.
///
/// [`Block`] is typically used to visualize separation and organize sections.
/// You can customize the border style, type of the border and so on.
///
/// # Example
///
/// ```rust
/// use termint::prelude::*;
///
/// // Creates new block containing horizontal layout
/// let mut main: Block<(), _> = Block::horizontal()
///     // Add any `Text` widget as a title
///     .title("Termint".fg(Color::Red))
///     // Double line border
///     .border_type(BorderType::Double)
///     // Makes the border color light gray
///     .border_color(Color::LightGray);
///
/// // Block exposes `Layout` methods for simplification
/// main.push("Sidebar", Constraint::Percent(30));
/// main.push("Content", Constraint::Fill(1))
/// ```
#[derive(Debug)]
pub struct Block<M: 'static = (), W = Element<M>> {
    title: Box<dyn Text>,
    borders: Border,
    border_type: BorderType,
    border_style: Style,
    child: Element<M>,
    child_type: PhantomData<W>,
}

impl<M> Block<M, Element<M>> {
    /// Creates a new [`Block`] wrapping the given widget.
    ///
    /// By default all the borders are visible and no title is set.
    ///
    /// The `child` can be any type convertible to [`Element`].
    #[must_use]
    pub fn new<T>(child: T) -> Self
    where
        T: Into<Element<M>>,
    {
        Self {
            title: Box::new(Span::new("")),
            borders: Border::ALL,
            border_type: BorderType::Normal,
            border_style: Default::default(),
            child: child.into(),
            child_type: PhantomData,
        }
    }
}

impl<M, W> Block<M, W> {
    /// Sets the title at the top of the [`Block`].
    ///
    /// This is typically used for section labels in your TUI.
    ///
    /// The `title` can be any type implementing [`Text`] trait.
    #[must_use]
    pub fn title<T>(mut self, title: T) -> Self
    where
        T: Into<Box<dyn Text>>,
    {
        self.title = title.into();
        self
    }

    /// Sets the visible borders of the [`Block`] using the given [`Border`]
    /// flags.
    ///
    /// # Example
    ///
    /// ```rust
    /// use termint::{prelude::*, borders};
    ///
    /// // Creates new [`Block`] with only top and bottom borders
    /// let block1 = Block::<(), _>::empty()
    ///     .borders(Border::TOP | Border::BOTTOM);
    /// // Or shorter using `borders!` macro
    /// let block2 = Block::<(), _>::horizontal()
    ///     .borders(borders!(TOP, BOTTOM));
    /// ```
    #[must_use]
    pub fn borders(mut self, borders: Border) -> Self {
        self.borders = borders;
        self
    }

    /// Sets the [`BorderType`] used to render the [`Block`] border.
    ///
    /// You can look at the [`BorderType`]'s documentation to look at all the
    /// supported border types.
    #[must_use]
    pub fn border_type(mut self, border_type: BorderType) -> Self {
        self.border_type = border_type;
        self
    }

    /// Sets the style applied to [`Block`] borders.
    ///
    /// The `style` can be any type convertible to [`Style`].
    #[must_use]
    pub fn border_style<T>(mut self, style: T) -> Self
    where
        T: Into<Style>,
    {
        self.border_style = style.into();
        self
    }

    /// Sets the foreground color of the [`Block`] borders.
    #[must_use]
    pub fn border_color(mut self, color: Color) -> Self {
        self.border_style = self.border_style.fg(color);
        self
    }
}

impl<M: Clone + 'static> Block<M, Spacer> {
    /// Creates a new [`Block`] containing a [`Spacer`].
    ///
    /// By default all the borders are visible and no title is set. This is
    /// useful for creating empty bordered areas.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            title: Box::new(Span::new("")),
            borders: Border::ALL,
            border_type: BorderType::Normal,
            border_style: Default::default(),
            child: Spacer::new().into(),
            child_type: PhantomData,
        }
    }
}

impl<M: Clone + 'static> Block<M, Layout<M>> {
    /// Creates a new [`Block`] wrapping a vertical [`Layout`].
    ///
    /// This is convenience constructor equivalent to
    /// `Block::new(Layout::vertical())`.
    #[must_use]
    pub fn vertical() -> Self {
        Self {
            title: Box::new(Span::new("")),
            borders: Border::ALL,
            border_type: Default::default(),
            border_style: Default::default(),
            child: Layout::vertical().into(),
            child_type: PhantomData,
        }
    }

    /// Creates a new [`Block`] wrapping a horizontal [`Layout`].
    ///
    /// This is convenience constructor equivalent to
    /// `Block::new(Layout::horizontal())`.
    #[must_use]
    pub fn horizontal() -> Self {
        Self {
            title: Box::new(Span::new("")),
            borders: Border::ALL,
            border_type: Default::default(),
            border_style: Default::default(),
            child: Layout::horizontal().into(),
            child_type: PhantomData,
        }
    }

    /// Sets flexing [`Direction`] of the [`Layout`].
    #[must_use]
    pub fn direction(mut self, direction: Direction) -> Self {
        self.child =
            self.child.map::<Layout<M>, _>(|l| l.direction(direction));
        self
    }

    /// Sets the base style of the [`Layout`].
    ///
    /// The `style` can be any type convertible to [`Style`].
    #[must_use]
    pub fn style<T>(mut self, style: T) -> Self
    where
        T: Into<Style>,
    {
        self.child = self.child.map::<Layout<M>, _>(|l| l.style(style));
        self
    }

    /// Sets base background color of the [`Layout`].
    ///
    /// The `bg` can be any type convertible into `Option<Color>`. If `None` is
    /// supplied, the background is transparent.
    #[must_use]
    pub fn bg<T>(mut self, bg: T) -> Self
    where
        T: Into<Option<Color>>,
    {
        self.child = self.child.map::<Layout<M>, _>(|l| l.bg(bg));
        self
    }

    /// Sets base foreground color of the [`Layout`].
    ///
    /// The `fg` can be any type convertible into `Option<Color>`. If `None` is
    /// supplied, it keeps the original foreground color.
    #[must_use]
    pub fn fg<T>(mut self, fg: T) -> Self
    where
        T: Into<Option<Color>>,
    {
        self.child = self.child.map::<Layout<M>, _>(|l| l.fg(fg));
        self
    }

    /// Sets the [`Padding`] of the [`Layout`].
    ///
    /// The `padding` can be any type convertible into [`Padding`], such as
    /// `usize` (uniform), `(usize, usize)` (vertical, horizontal). You can
    /// read more in the [`Padding`] documentation.
    #[must_use]
    pub fn padding<T>(mut self, padding: T) -> Self
    where
        T: Into<Padding>,
    {
        self.child = self.child.map::<Layout<M>, _>(|l| l.padding(padding));
        self
    }

    /// Makes [`Layout`] center its content in the direction it flexes.
    ///
    /// If the layout is flexing its children horizontally, the content will
    /// be centered horizontally. Otherwise it will be centered vertically.
    #[must_use]
    pub fn center(mut self) -> Self {
        self.child = self.child.map::<Layout<M>, _>(|l| l.center());
        self
    }

    /// Adds a child widget with its contraint
    ///
    /// The `child` can be any type convertible into [`Element`].
    ///
    /// The `constraint` can be any type convertible into [`Constraint`], such
    /// as `5` (`Constraint::Length(5)`) and `1..` (`Constraint::Min(1)`).
    pub fn push<T, C>(&mut self, child: T, constraint: C)
    where
        T: Into<Element<M>>,
        C: Into<Constraint>,
    {
        if let Some(layout) = self.child.downcast_mut::<Layout<M>>() {
            layout.push(child, constraint);
        }
    }
}

impl<M, W> Widget<M> for Block<M, W>
where
    M: Clone + 'static,
    W: Widget<M>,
{
    fn render(&self, buffer: &mut Buffer, layout: &LayoutNode) {
        let rect = layout.area;
        let (_, r, _, l) = self.render_border(buffer, &rect);
        let pos = Vec2::new(rect.x() + l, rect.y());
        let size = Vec2::new(rect.width().saturating_sub(l + r), 1);

        let trect = Rect::from_coords(pos, size);
        _ = self.title.render_offset(buffer, trect, 0, None);

        self.child.render(buffer, &layout.children[0]);
    }

    fn height(&self, size: &Vec2) -> usize {
        let (width, height) = self.border_size();
        let size = Vec2::new(
            size.x.saturating_sub(width),
            size.y.saturating_sub(height),
        );
        height + self.child.height(&size)
    }

    fn width(&self, size: &Vec2) -> usize {
        let (width, height) = self.border_size();
        let size = Vec2::new(
            size.x.saturating_sub(width),
            size.y.saturating_sub(height),
        );
        max(self.child.width(&size), self.title.get_text().len()) + width
    }

    fn children(&self) -> Vec<&Element<M>> {
        vec![&self.child]
    }

    fn layout_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();

        self.title.get_text().hash(&mut hasher);
        self.borders.hash(&mut hasher);

        hasher.finish()
    }

    fn layout(&self, node: &mut LayoutNode, area: Rect) {
        node.children[0].layout(&self.child, area.inner(self.borders));
    }
}

impl<M, W> Block<M, W> {
    /// Renders [`Block`] border
    fn render_border(
        &self,
        buffer: &mut Buffer,
        rect: &Rect,
    ) -> (usize, usize, usize, usize) {
        let l = self.ver_border(buffer, rect, rect.left(), Border::LEFT);
        let r = self.ver_border(buffer, rect, rect.right(), Border::RIGHT);
        let t = self.hor_border(buffer, rect, rect.top(), Border::TOP);
        let b = self.hor_border(buffer, rect, rect.bottom(), Border::BOTTOM);

        if rect.width() <= 1 || rect.height() <= 1 {
            return (t, r, b, l);
        }

        self.render_corner(buffer, *rect.pos(), borders!(TOP, LEFT));
        self.render_corner(buffer, rect.top_right(), borders!(TOP, RIGHT));
        self.render_corner(buffer, rect.bottom_left(), borders!(BOTTOM, LEFT));
        self.render_corner(
            buffer,
            rect.bottom_right(),
            borders!(BOTTOM, RIGHT),
        );

        (t, r, b, l)
    }

    /// Adds horizontal border to the buffer
    fn hor_border(
        &self,
        buffer: &mut Buffer,
        rect: &Rect,
        y: usize,
        border: Border,
    ) -> usize {
        if (self.borders & border) == Border::NONE {
            return 0;
        }

        let c = self.border_type.get(border);
        let mut pos = Vec2::new(rect.x(), y);
        while pos.x <= rect.right() {
            buffer[pos].char(c).style(self.border_style);
            pos.x += 1;
        }
        1
    }

    /// Adds vertical border to the buffer
    fn ver_border(
        &self,
        buffer: &mut Buffer,
        rect: &Rect,
        x: usize,
        border: Border,
    ) -> usize {
        if (self.borders & border) == Border::NONE {
            return 0;
        }

        let c = self.border_type.get(border);
        let mut pos = Vec2::new(x, rect.y());
        while pos.y <= rect.bottom() {
            buffer[pos].char(c).style(self.border_style);
            pos.y += 1;
        }
        1
    }

    /// Adds corner of [`Block`] border to the string
    fn render_corner(&self, buffer: &mut Buffer, pos: Vec2, border: Border) {
        if (self.borders & border) == border {
            let c = self.border_type.get(border);
            buffer[pos].char(c).style(self.border_style);
        }
    }

    /// Gets border size
    fn border_size(&self) -> (usize, usize) {
        (self.hor_border_size(), self.ver_border_size())
    }

    /// Gets horizontal border size
    fn hor_border_size(&self) -> usize {
        (self.borders & Border::RIGHT != Border::NONE) as usize
            + (self.borders & Border::LEFT != Border::NONE) as usize
    }

    /// Gets vertical border size and acounting title as well
    fn ver_border_size(&self) -> usize {
        (self.borders & Border::TOP != Border::NONE) as usize
            + (self.borders & Border::BOTTOM != Border::NONE) as usize
    }
}

// From implementations
impl<M, W> From<Block<M, W>> for Box<dyn Widget<M>>
where
    M: Clone + 'static,
    W: Widget<M> + 'static,
{
    fn from(value: Block<M, W>) -> Self {
        Box::new(value)
    }
}

impl<M, W> From<Block<M, W>> for Element<M>
where
    M: Clone + 'static,
    W: Widget<M> + 'static,
{
    fn from(value: Block<M, W>) -> Self {
        Element::new(value)
    }
}