From e7085e3a3ec4b3b90a4e69d49add96e7ba65616c Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Fri, 15 Nov 2024 19:42:07 -0800 Subject: [PATCH] chore: move widgets into ratatui-widgets crate (#1474) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All the widgets now live in their own ratatui-widgets crate, but are re-exported in the main ratatui crate. This makes it easier to use portions of the ratatui library and is part of the effort to modularize Part of: #1388 --------- Co-authored-by: Orhun Parmaksız Co-authored-by: Orhun Parmaksız --- Cargo.lock | 22 ++- Cargo.toml | 6 +- ratatui-core/README.md | 3 +- ratatui-widgets/Cargo.toml | 104 ++++++++++++++ ratatui-widgets/README.md | 76 ++++++++++ .../src}/barchart.rs | 38 ++--- .../src}/barchart/bar.rs | 14 +- .../src}/barchart/bar_group.rs | 6 +- .../widgets => ratatui-widgets/src}/block.rs | 133 +++--------------- .../src}/block/padding.rs | 4 +- .../src}/block/title.rs | 25 ++-- .../src}/borders.rs | 103 +++++++++++++- .../src}/calendar.rs | 23 +-- .../widgets => ratatui-widgets/src}/canvas.rs | 46 +++--- .../src}/canvas/circle.rs | 20 +-- .../src}/canvas/line.rs | 20 +-- .../src}/canvas/map.rs | 20 +-- .../src}/canvas/points.rs | 7 +- .../src}/canvas/rectangle.rs | 17 +-- .../src}/canvas/world.rs | 0 .../widgets => ratatui-widgets/src}/chart.rs | 27 ++-- .../widgets => ratatui-widgets/src}/clear.rs | 9 +- .../widgets => ratatui-widgets/src}/gauge.rs | 13 +- ratatui-widgets/src/lib.rs | 89 ++++++++++++ .../list => ratatui-widgets/src}/list.rs | 42 +++--- .../src}/list/item.rs | 28 ++-- .../src}/list/rendering.rs | 78 +++++----- .../src}/list/state.rs | 6 +- .../widgets => ratatui-widgets/src}/logo.rs | 4 +- .../src}/paragraph.rs | 43 +++--- .../widgets => ratatui-widgets/src}/reflow.rs | 15 +- .../src}/scrollbar.rs | 20 +-- .../src}/sparkline.rs | 28 ++-- .../table => ratatui-widgets/src}/table.rs | 61 ++++---- .../src}/table/cell.rs | 17 +-- .../src}/table/highlight_spacing.rs | 0 .../src}/table/row.rs | 22 +-- .../src/table/state.rs | 7 +- .../widgets => ratatui-widgets/src}/tabs.rs | 15 +- ratatui/Cargo.toml | 12 +- ratatui/src/lib.rs | 1 + ratatui/src/widgets.rs | 39 +++-- ratatui/src/widgets/list.rs | 10 -- ratatui/src/widgets/table.rs | 11 -- 44 files changed, 773 insertions(+), 511 deletions(-) create mode 100644 ratatui-widgets/Cargo.toml create mode 100644 ratatui-widgets/README.md rename {ratatui/src/widgets => ratatui-widgets/src}/barchart.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/barchart/bar.rs (93%) rename {ratatui/src/widgets => ratatui-widgets/src}/barchart/bar_group.rs (97%) rename {ratatui/src/widgets => ratatui-widgets/src}/block.rs (94%) rename {ratatui/src/widgets => ratatui-widgets/src}/block/padding.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/block/title.rs (85%) rename {ratatui/src/widgets => ratatui-widgets/src}/borders.rs (64%) rename {ratatui/src/widgets => ratatui-widgets/src}/calendar.rs (96%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/circle.rs (85%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/line.rs (96%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/map.rs (97%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/points.rs (86%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/rectangle.rs (96%) rename {ratatui/src/widgets => ratatui-widgets/src}/canvas/world.rs (100%) rename {ratatui/src/widgets => ratatui-widgets/src}/chart.rs (99%) rename {ratatui/src/widgets => ratatui-widgets/src}/clear.rs (87%) rename {ratatui/src/widgets => ratatui-widgets/src}/gauge.rs (98%) create mode 100644 ratatui-widgets/src/lib.rs rename {ratatui/src/widgets/list => ratatui-widgets/src}/list.rs (94%) rename {ratatui/src/widgets => ratatui-widgets/src}/list/item.rs (93%) rename {ratatui/src/widgets => ratatui-widgets/src}/list/rendering.rs (96%) rename {ratatui/src/widgets => ratatui-widgets/src}/list/state.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/logo.rs (97%) rename {ratatui/src/widgets => ratatui-widgets/src}/paragraph.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/reflow.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/scrollbar.rs (99%) rename {ratatui/src/widgets => ratatui-widgets/src}/sparkline.rs (97%) rename {ratatui/src/widgets/table => ratatui-widgets/src}/table.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/table/cell.rs (95%) rename {ratatui/src/widgets => ratatui-widgets/src}/table/highlight_spacing.rs (100%) rename {ratatui/src/widgets => ratatui-widgets/src}/table/row.rs (96%) rename ratatui/src/widgets/table/table_state.rs => ratatui-widgets/src/table/state.rs (98%) rename {ratatui/src/widgets => ratatui-widgets/src}/tabs.rs (98%) delete mode 100644 ratatui/src/widgets/list.rs delete mode 100644 ratatui/src/widgets/table.rs diff --git a/Cargo.lock b/Cargo.lock index c7c6fb87..f1fdba7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2089,7 +2089,6 @@ name = "ratatui" version = "0.29.0" dependencies = [ "argh", - "bitflags 2.6.0", "color-eyre", "criterion", "crossterm", @@ -2106,6 +2105,7 @@ dependencies = [ "rand 0.8.5", "rand_chacha 0.3.1", "ratatui-core", + "ratatui-widgets", "rstest", "serde", "serde_json", @@ -2117,7 +2117,6 @@ dependencies = [ "tracing", "tracing-appender", "tracing-subscriber", - "unicode-segmentation", "unicode-width", ] @@ -2144,6 +2143,25 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "ratatui-widgets" +version = "0.3.0" +dependencies = [ + "bitflags 2.6.0", + "indoc", + "instability", + "itertools 0.13.0", + "pretty_assertions", + "ratatui", + "ratatui-core", + "rstest", + "serde", + "strum", + "time", + "unicode-segmentation", + "unicode-width", +] + [[package]] name = "rayon" version = "1.10.0" diff --git a/Cargo.toml b/Cargo.toml index 10dbc885..22f85c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" -members = ["ratatui", "ratatui-core", "xtask"] -default-members = ["ratatui", "ratatui-core"] +members = ["ratatui", "ratatui-core", "ratatui-widgets", "xtask"] +default-members = ["ratatui", "ratatui-core", "ratatui-widgets"] [workspace.package] authors = ["Florian Dehau ", "The Ratatui Developers"] @@ -29,7 +29,9 @@ indoc = "2.0.5" instability = "0.3.1" itertools = "0.13.0" pretty_assertions = "1.4.1" +ratatui = { path = "ratatui" } ratatui-core = { path = "ratatui-core" } +ratatui-widgets = { path = "ratatui-widgets" } rstest = "0.23.0" serde = { version = "1.0.214", features = ["derive"] } strum = { version = "0.26.3", features = ["derive"] } diff --git a/ratatui-core/README.md b/ratatui-core/README.md index 0315c4d0..b98a46f7 100644 --- a/ratatui-core/README.md +++ b/ratatui-core/README.md @@ -27,7 +27,8 @@ cargo add ratatui-core ## Contributing -We welcome contributions from the community! Please see our [CONTRIBUTING](../CONTRIBUTING.md) guide for more details on how to get involved. +We welcome contributions from the community! Please see our [CONTRIBUTING](../CONTRIBUTING.md) guide +for more details on how to get involved. ## License diff --git a/ratatui-widgets/Cargo.toml b/ratatui-widgets/Cargo.toml new file mode 100644 index 00000000..97e003b8 --- /dev/null +++ b/ratatui-widgets/Cargo.toml @@ -0,0 +1,104 @@ +[package] +name = "ratatui-widgets" +description = "A collection of Ratatui widgets for building terminal user interfaces." +version = "0.3.0" +authors.workspace = true +documentation.workspace = true +repository.workspace = true +homepage.workspace = true +keywords.workspace = true +categories.workspace = true +license.workspace = true +exclude.workspace = true +edition.workspace = true +rust-version.workspace = true + +[features] +## enables serialization and deserialization of style and color types using the [`serde`] crate. +## This is useful if you want to save themes to a file. +serde = ["dep:serde", "ratatui-core/serde"] + +#! Widgets that add dependencies are gated behind feature flags to prevent unused transitive +#! dependencies. The available features are: + +## enables all widgets. +all-widgets = ["calendar"] + +## enables the [`calendar`](widgets::calendar) widget module and adds a dependency on [`time`]. +calendar = ["dep:time"] + +## Enable all unstable features. +unstable = ["unstable-rendered-line-info", "unstable-widget-ref"] + +## enables the [`WidgetRef`] and [`StatefulWidgetRef`] traits which are experimental and may change +## in the future. +unstable-widget-ref = ["ratatui-core/unstable-widget-ref"] + +## Enables the [`Paragraph::line_count`](widgets::Paragraph::line_count) +## [`Paragraph::line_width`](widgets::Paragraph::line_width) methods +## which are experimental and may change in the future. +## See [Issue 293](https://github.com/ratatui/ratatui/issues/293) for more details. +unstable-rendered-line-info = [] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[dependencies] +bitflags.workspace = true +itertools.workspace = true +indoc.workspace = true +instability.workspace = true +ratatui-core = { workspace = true } +strum.workspace = true +time = { version = "0.3.11", optional = true, features = ["local-offset"] } +unicode-segmentation.workspace = true +unicode-width.workspace = true +serde = { workspace = true, optional = true } + +[dev-dependencies] +rstest.workspace = true +pretty_assertions.workspace = true +ratatui.workspace = true + +[lints.rust] +unsafe_code = "forbid" + +[lints.clippy] +cargo = { level = "warn", priority = -1 } +pedantic = { level = "warn", priority = -1 } +cast_possible_truncation = "allow" +cast_possible_wrap = "allow" +cast_precision_loss = "allow" +cast_sign_loss = "allow" +missing_errors_doc = "allow" +missing_panics_doc = "allow" +module_name_repetitions = "allow" +must_use_candidate = "allow" + +# we often split up a module into multiple files with the main type in a file named after the +# module, so we want to allow this pattern +module_inception = "allow" + +# nursery or restricted +as_underscore = "warn" +deref_by_slicing = "warn" +else_if_without_else = "warn" +empty_line_after_doc_comments = "warn" +equatable_if_let = "warn" +fn_to_numeric_cast_any = "warn" +format_push_string = "warn" +map_err_ignore = "warn" +missing_const_for_fn = "warn" +mixed_read_write_in_expression = "warn" +mod_module_files = "warn" +needless_pass_by_ref_mut = "warn" +needless_raw_strings = "warn" +or_fun_call = "warn" +redundant_type_annotations = "warn" +rest_pat_in_fully_bound_structs = "warn" +string_lit_chars_any = "warn" +string_slice = "warn" +string_to_string = "warn" +unnecessary_self_imports = "warn" +use_self = "warn" diff --git a/ratatui-widgets/README.md b/ratatui-widgets/README.md new file mode 100644 index 00000000..bd99a5ce --- /dev/null +++ b/ratatui-widgets/README.md @@ -0,0 +1,76 @@ +# Ratatui-widgets + + + + +Ratatui-widgets contains all the widgets that were previously part of the Ratatui crate. It is +meant to be used in conjunction with the [Ratatui] crate, which provides the core functionality +for building terminal user interfaces. + +[Ratatui]: https://crates.io/crates/ratatui + +Most applications shouldn't need to depend directly on Ratatui-widgets, as all the Ratatui crate +re-exports all the widgets from this crate. However, if you are building a widget library that +internally uses Ratatui widgets, or if you prefer finer grained dependencies, you may want to +depend on this crate rather than transitively through the Ratatui crate. + +Previously, a crate named `Ratatui-widgets` was published with some formative ideas about an +eventual Ratatui framework. That crate is now move to [tui-framework-experiment], pending a new +name. + +[tui-framework-experiment]: https://crates.io/crates/tui-framework-experiment + +## Installation + +Run the following command to add this crate to your project: + +```sh +cargo add ratatui-widgets +``` + +## Available Widgets + +- [`BarChart`]: displays multiple datasets as bars with optional grouping. +- [`Block`]: a basic widget that draws a block with optional borders, titles, and styles. +- [`calendar::Monthly`]: displays a single month. +- [`Canvas`]: draws arbitrary shapes using drawing characters. +- [`Chart`]: displays multiple datasets as lines or scatter graphs. +- [`Clear`]: clears the area it occupies. Useful to render over previously drawn widgets. +- [`Gauge`]: displays progress percentage using block characters. +- [`LineGauge`]: displays progress as a line. +- [`List`]: displays a list of items and allows selection. +- [`RatatuiLogo`]: displays the Ratatui logo. +- [`Paragraph`]: displays a paragraph of optionally styled and wrapped text. +- [`Scrollbar`]: displays a scrollbar. +- [`Sparkline`]: displays a single dataset as a sparkline. +- [`Table`]: displays multiple rows and columns in a grid and allows selection. +- [`Tabs`]: displays a tab bar and allows selection. + +[`BarChart`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/barchart/struct.BarChart.html +[`Block`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/block/struct.Block.html +[`calendar::Monthly`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/calendar/struct.Monthly.html +[`Canvas`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/canvas/struct.Canvas.html +[`Chart`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/chart/struct.Chart.html +[`Clear`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/clear/struct.Clear.html +[`Gauge`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/gauge/struct.Gauge.html +[`LineGauge`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/gauge/struct.LineGauge.html +[`List`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/list/struct.List.html +[`RatatuiLogo`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/logo/struct.RatatuiLogo.html +[`Paragraph`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/paragraph/struct.Paragraph.html +[`Scrollbar`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/scrollbar/struct.Scrollbar.html +[`Sparkline`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/sparkline/struct.Sparkline.html +[`Table`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/table/struct.Table.html +[`Tabs`]: https://docs.rs/ratatui-widgets/latest/ratatui_widgets/tabs/struct.Tabs.html + +All these widgets are re-exported directly under `ratatui::widgets` in the Ratatui crate. + +## Contributing + +Contributions are welcome! Please open an issue or submit a pull request on GitHub. For more +details on contributing, please see the [CONTRIBUTING](CONTRIBUTING.md) document. + +## License + +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. + + diff --git a/ratatui/src/widgets/barchart.rs b/ratatui-widgets/src/barchart.rs similarity index 98% rename from ratatui/src/widgets/barchart.rs rename to ratatui-widgets/src/barchart.rs index bb5b6321..65b845ec 100644 --- a/ratatui/src/widgets/barchart.rs +++ b/ratatui-widgets/src/barchart.rs @@ -1,18 +1,20 @@ -use crate::{ +//! The [`BarChart`] widget and its related types (e.g. [`Bar`], [`BarGroup`]). + +use ratatui_core::{ buffer::Buffer, layout::{Direction, Rect}, style::{Style, Styled}, symbols::{self}, text::Line, - widgets::{block::BlockExt, Block, Widget, WidgetRef}, + widgets::{Widget, WidgetRef}, }; +pub use self::{bar::Bar, bar_group::BarGroup}; +use crate::block::{Block, BlockExt}; + mod bar; mod bar_group; -pub use bar::Bar; -pub use bar_group::BarGroup; - /// A chart showing values as [bars](Bar). /// /// Here is a possible `BarChart` output. @@ -189,7 +191,7 @@ impl<'a> BarChart<'a> { /// It is also possible to set individually the style of each [`Bar`]. /// In this case the default style will be patched by the individual style /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn bar_style>(mut self, style: S) -> Self { self.bar_style = style.into(); @@ -198,8 +200,8 @@ impl<'a> BarChart<'a> { /// Set the width of the displayed bars. /// - /// For [`Horizontal`](crate::layout::Direction::Horizontal) bars this becomes the height of - /// the bar. + /// For [`Horizontal`](ratatui_core::layout::Direction::Horizontal) bars this becomes the height + /// of the bar. /// /// If not set, this defaults to `1`. /// The bar label also uses this value as its width. @@ -234,9 +236,9 @@ impl<'a> BarChart<'a> { self } - /// The [`bar::Set`](crate::symbols::bar::Set) to use for displaying the bars. + /// The [`bar::Set`](ratatui_core::symbols::bar::Set) to use for displaying the bars. /// - /// If not set, the default is [`bar::NINE_LEVELS`](crate::symbols::bar::NINE_LEVELS). + /// If not set, the default is [`bar::NINE_LEVELS`](ratatui_core::symbols::bar::NINE_LEVELS). #[must_use = "method moves the value of self and returns the modified value"] pub const fn bar_set(mut self, bar_set: symbols::bar::Set) -> Self { self.bar_set = bar_set; @@ -255,7 +257,7 @@ impl<'a> BarChart<'a> { /// /// [`Bar::value_style`] to set the value style individually. /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn value_style>(mut self, style: S) -> Self { self.value_style = style.into(); @@ -274,7 +276,7 @@ impl<'a> BarChart<'a> { /// /// [`Bar::label`] to set the label style individually. /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn label_style>(mut self, style: S) -> Self { self.label_style = style.into(); @@ -295,7 +297,7 @@ impl<'a> BarChart<'a> { /// /// The style will be applied to everything that isn't styled (borders, bars, labels, ...). /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn style>(mut self, style: S) -> Self { self.style = style.into(); @@ -304,7 +306,7 @@ impl<'a> BarChart<'a> { /// Set the direction of the bars. /// - /// [`Vertical`](crate::layout::Direction::Vertical) bars are the default. + /// [`Vertical`](ratatui_core::layout::Direction::Vertical) bars are the default. /// /// # Examples /// @@ -634,15 +636,15 @@ impl<'a> Styled for BarChart<'a> { #[cfg(test)] mod tests { use itertools::iproduct; - - use super::*; - use crate::{ + use ratatui_core::{ layout::Alignment, style::{Color, Modifier, Stylize}, text::Span, - widgets::BorderType, }; + use super::*; + use crate::borders::BorderType; + #[test] fn default() { let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 3)); diff --git a/ratatui/src/widgets/barchart/bar.rs b/ratatui-widgets/src/barchart/bar.rs similarity index 93% rename from ratatui/src/widgets/barchart/bar.rs rename to ratatui-widgets/src/barchart/bar.rs index 592f2973..6e403b73 100644 --- a/ratatui/src/widgets/barchart/bar.rs +++ b/ratatui-widgets/src/barchart/bar.rs @@ -1,7 +1,7 @@ +use ratatui_core::{buffer::Buffer, layout::Rect, style::Style, text::Line, widgets::Widget}; use unicode_width::UnicodeWidthStr; -use crate::{buffer::Buffer, layout::Rect, style::Style, text::Line, widgets::Widget}; -/// A bar to be shown by the [`BarChart`](crate::widgets::BarChart) widget. +/// A bar to be shown by the [`BarChart`](crate::barchart::BarChart) widget. /// /// Here is an explanation of a `Bar`'s components. /// ```plain @@ -59,11 +59,11 @@ impl<'a> Bar<'a> { /// Set the label of the bar. /// - /// For [`Vertical`](crate::layout::Direction::Vertical) bars, + /// For [`Vertical`](ratatui_core::layout::Direction::Vertical) bars, /// display the label **under** the bar. - /// For [`Horizontal`](crate::layout::Direction::Horizontal) bars, + /// For [`Horizontal`](ratatui_core::layout::Direction::Horizontal) bars, /// display the label **in** the bar. - /// See [`BarChart::direction`](crate::widgets::BarChart::direction) to set the direction. + /// See [`BarChart::direction`](crate::barchart::BarChart::direction) to set the direction. #[must_use = "method moves the value of self and returns the modified value"] pub fn label(mut self, label: Line<'a>) -> Self { self.label = Some(label); @@ -77,7 +77,7 @@ impl<'a> Bar<'a> { /// /// This will apply to every non-styled element. It can be seen and used as a default value. /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn style>(mut self, style: S) -> Self { self.style = style.into(); @@ -93,7 +93,7 @@ impl<'a> Bar<'a> { /// /// [`Bar::value`] to set the value. /// - /// [`Color`]: crate::style::Color + /// [`Color`]: ratatui_core::style::Color #[must_use = "method moves the value of self and returns the modified value"] pub fn value_style>(mut self, style: S) -> Self { self.value_style = style.into(); diff --git a/ratatui/src/widgets/barchart/bar_group.rs b/ratatui-widgets/src/barchart/bar_group.rs similarity index 97% rename from ratatui/src/widgets/barchart/bar_group.rs rename to ratatui-widgets/src/barchart/bar_group.rs index f8bfe103..b5875e90 100644 --- a/ratatui/src/widgets/barchart/bar_group.rs +++ b/ratatui-widgets/src/barchart/bar_group.rs @@ -1,11 +1,13 @@ -use crate::{ +use ratatui_core::{ buffer::Buffer, layout::{Alignment, Rect}, style::Style, text::Line, - widgets::{barchart::Bar, Widget}, + widgets::Widget, }; +use crate::barchart::Bar; + /// A group of bars to be shown by the Barchart. /// /// # Examples diff --git a/ratatui/src/widgets/block.rs b/ratatui-widgets/src/block.rs similarity index 94% rename from ratatui/src/widgets/block.rs rename to ratatui-widgets/src/block.rs index 670720aa..1dedca0d 100644 --- a/ratatui/src/widgets/block.rs +++ b/ratatui-widgets/src/block.rs @@ -6,24 +6,25 @@ //! [title](Block::title) and [padding](Block::padding). use itertools::Itertools; -use strum::{Display, EnumString}; - -use crate::{ +use ratatui_core::{ buffer::Buffer, layout::{Alignment, Rect}, style::{Style, Styled}, symbols::border, text::Line, - widgets::{Borders, Widget, WidgetRef}, + widgets::{Widget, WidgetRef}, }; +pub use self::{ + padding::Padding, + title::{Position, Title}, +}; +use crate::borders::{BorderType, Borders}; + mod padding; pub mod title; -pub use padding::Padding; -pub use title::{Position, Title}; - -/// Base widget to be used to display a box border around all [upper level ones](crate::widgets). +/// Base widget to be used to display a box border around all other built-in widgets. /// /// The borders can be configured with [`Block::borders`] and others. A block can have multiple /// [`Title`] using [`Block::title`]. It can also be [styled](Block::style) and @@ -132,79 +133,6 @@ pub struct Block<'a> { padding: Padding, } -/// The type of border of a [`Block`]. -/// -/// See the [`borders`](Block::borders) method of `Block` to configure its borders. -#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)] -pub enum BorderType { - /// A plain, simple border. - /// - /// This is the default - /// - /// # Example - /// - /// ```plain - /// ┌───────┐ - /// │ │ - /// └───────┘ - /// ``` - #[default] - Plain, - /// A plain border with rounded corners. - /// - /// # Example - /// - /// ```plain - /// ╭───────╮ - /// │ │ - /// ╰───────╯ - /// ``` - Rounded, - /// A doubled border. - /// - /// Note this uses one character that draws two lines. - /// - /// # Example - /// - /// ```plain - /// ╔═══════╗ - /// ║ ║ - /// ╚═══════╝ - /// ``` - Double, - /// A thick border. - /// - /// # Example - /// - /// ```plain - /// ┏━━━━━━━┓ - /// ┃ ┃ - /// ┗━━━━━━━┛ - /// ``` - Thick, - /// A border with a single line on the inside of a half block. - /// - /// # Example - /// - /// ```plain - /// ▗▄▄▄▄▄▄▄▖ - /// ▐ ▌ - /// ▐ ▌ - /// ▝▀▀▀▀▀▀▀▘ - QuadrantInside, - - /// A border with a single line on the outside of a half block. - /// - /// # Example - /// - /// ```plain - /// ▛▀▀▀▀▀▀▀▜ - /// ▌ ▐ - /// ▌ ▐ - /// ▙▄▄▄▄▄▄▄▟ - QuadrantOutside, -} - impl<'a> Block<'a> { /// Creates a new block with no [`Borders`] or [`Padding`]. pub const fn new() -> Self { @@ -244,8 +172,8 @@ impl<'a> Block<'a> { /// space is calculated based on the full width of the block, rather than the leftover width. /// /// You can provide any type that can be converted into [`Title`] including: strings, string - /// slices (`&str`), borrowed strings (`Cow`), [spans](crate::text::Span), or vectors of - /// [spans](crate::text::Span) (`Vec`). + /// slices (`&str`), borrowed strings (`Cow`), [spans](ratatui_core::text::Span), or + /// vectors of [spans](ratatui_core::text::Span) (`Vec`). /// /// By default, the titles will avoid being rendered in the corners of the block but will align /// against the left or right edge of the block if there is no border on that edge. The @@ -319,8 +247,8 @@ impl<'a> Block<'a> { /// Adds a title to the top of the block. /// /// You can provide any type that can be converted into [`Line`] including: strings, string - /// slices (`&str`), borrowed strings (`Cow`), [spans](crate::text::Span), or vectors of - /// [spans](crate::text::Span) (`Vec`). + /// slices (`&str`), borrowed strings (`Cow`), [spans](ratatui_core::text::Span), or + /// vectors of [spans](ratatui_core::text::Span) (`Vec`). /// /// # Example /// @@ -348,8 +276,8 @@ impl<'a> Block<'a> { /// Adds a title to the bottom of the block. /// /// You can provide any type that can be converted into [`Line`] including: strings, string - /// slices (`&str`), borrowed strings (`Cow`), [spans](crate::text::Span), or vectors of - /// [spans](crate::text::Span) (`Vec`). + /// slices (`&str`), borrowed strings (`Cow`), [spans](ratatui_core::text::Span), or + /// vectors of [spans](ratatui_core::text::Span) (`Vec`). /// /// # Example /// @@ -385,7 +313,7 @@ impl<'a> Block<'a> { /// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], [`Color`], or /// your own type that implements [`Into