tui_temp_fork/widgets/
mod.rs

1use bitflags::bitflags;
2use std::borrow::Cow;
3
4mod barchart;
5mod block;
6pub mod canvas;
7mod chart;
8mod gauge;
9mod list;
10mod paragraph;
11mod reflow;
12mod sparkline;
13mod table;
14mod tabs;
15
16pub use self::barchart::BarChart;
17pub use self::block::Block;
18pub use self::chart::{Axis, Chart, Dataset, Marker};
19pub use self::gauge::Gauge;
20pub use self::list::{List, SelectableList};
21pub use self::paragraph::Paragraph;
22pub use self::sparkline::Sparkline;
23pub use self::table::{Row, Table};
24pub use self::tabs::Tabs;
25
26use crate::backend::Backend;
27use crate::buffer::Buffer;
28use crate::layout::Rect;
29use crate::style::{Color, Style};
30use crate::terminal::Frame;
31
32bitflags! {
33    /// Bitflags that can be composed to set the visible borders essentially on the block widget.
34    pub struct Borders: u32 {
35        /// Show no border (default)
36        const NONE  = 0b0000_0001;
37        /// Show the top border
38        const TOP   = 0b0000_0010;
39        /// Show the right border
40        const RIGHT = 0b0000_0100;
41        /// Show the bottom border
42        const BOTTOM = 0b000_1000;
43        /// Show the left border
44        const LEFT = 0b0001_0000;
45        /// Show all borders
46        const ALL = Self::TOP.bits | Self::RIGHT.bits | Self::BOTTOM.bits | Self::LEFT.bits;
47    }
48}
49
50#[derive(Clone, Debug, PartialEq)]
51pub enum Text<'b> {
52    Raw(Cow<'b, str>),
53    Styled(Cow<'b, str>, Style),
54}
55
56impl<'b> Text<'b> {
57    pub fn raw<D: Into<Cow<'b, str>>>(data: D) -> Text<'b> {
58        Text::Raw(data.into())
59    }
60
61    pub fn styled<D: Into<Cow<'b, str>>>(data: D, style: Style) -> Text<'b> {
62        Text::Styled(data.into(), style)
63    }
64}
65
66/// Base requirements for a Widget
67pub trait Widget {
68    /// Draws the current state of the widget in the given buffer. That the only method required to
69    /// implement a custom widget.
70    fn draw(&mut self, area: Rect, buf: &mut Buffer);
71    /// Helper method to quickly set the background of all cells inside the specified area.
72    fn background(&self, area: Rect, buf: &mut Buffer, color: Color) {
73        for y in area.top()..area.bottom() {
74            for x in area.left()..area.right() {
75                buf.get_mut(x, y).set_bg(color);
76            }
77        }
78    }
79    /// Helper method that can be chained with a widget's builder methods to render it.
80    fn render<B>(&mut self, f: &mut Frame<B>, area: Rect)
81    where
82        Self: Sized,
83        B: Backend,
84    {
85        f.render(self, area);
86    }
87}