Skip to main content

rumatui_tui/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 scroll;
13mod sparkline;
14mod table;
15mod tabs;
16
17pub use self::barchart::BarChart;
18pub use self::block::{Block, BorderType};
19pub use self::chart::{Axis, Chart, Dataset, GraphType, Marker};
20pub use self::gauge::Gauge;
21pub use self::list::{List, ListState};
22pub use self::paragraph::Paragraph;
23pub use self::sparkline::Sparkline;
24pub use self::table::{Row, Table, TableState};
25pub use self::tabs::Tabs;
26
27use crate::buffer::Buffer;
28use crate::layout::Rect;
29use crate::style::Style;
30
31bitflags! {
32    /// Bitflags that can be composed to set the visible borders essentially on the block widget.
33    pub struct Borders: u32 {
34        /// Show no border (default)
35        const NONE  = 0b0000_0001;
36        /// Show the top border
37        const TOP   = 0b0000_0010;
38        /// Show the right border
39        const RIGHT = 0b0000_0100;
40        /// Show the bottom border
41        const BOTTOM = 0b000_1000;
42        /// Show the left border
43        const LEFT = 0b0001_0000;
44        /// Show all borders
45        const ALL = Self::TOP.bits | Self::RIGHT.bits | Self::BOTTOM.bits | Self::LEFT.bits;
46    }
47}
48
49#[derive(Clone, Debug, PartialEq)]
50pub enum Text<'b> {
51    Raw(Cow<'b, str>),
52    Styled(Cow<'b, str>, Style),
53}
54
55impl<'b> Text<'b> {
56    pub fn raw<D: Into<Cow<'b, str>>>(data: D) -> Text<'b> {
57        Text::Raw(data.into())
58    }
59
60    pub fn styled<D: Into<Cow<'b, str>>>(data: D, style: Style) -> Text<'b> {
61        Text::Styled(data.into(), style)
62    }
63}
64
65/// Base requirements for a Widget
66pub trait Widget {
67    /// Draws the current state of the widget in the given buffer. That the only method required to
68    /// implement a custom widget.
69    fn render(self, area: Rect, buf: &mut Buffer);
70}
71
72pub trait StatefulWidget {
73    type State;
74    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State);
75}