From 98df774d7f9b69a2a474d25adf38e38a428f1b77 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Tue, 5 Nov 2024 22:10:49 -0800 Subject: [PATCH] chore(core): move core types to ratatui-core (#1460) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The buffer, layout, style, symbols, text, and the top level of widgets modules are moved to ratatui-core. This is the first step in modularizing the library so that the core types can be used in other projects without the need for the backend / widgets types. This helps reduce the need for updating other crates as often due to semver changes outside of the core types. --------- Co-authored-by: Orhun Parmaksız Co-authored-by: Orhun Parmaksız --- BREAKING-CHANGES.md | 4 +- Cargo.lock | 35 +- Cargo.toml | 18 +- ratatui-core/Cargo.toml | 70 ++ ratatui-core/README.md | 34 + {ratatui => ratatui-core}/src/buffer.rs | 0 .../src/buffer/assert.rs | 0 .../src/buffer/buffer.rs | 18 +- {ratatui => ratatui-core}/src/buffer/cell.rs | 0 {ratatui => ratatui-core}/src/layout.rs | 0 .../src/layout/alignment.rs | 0 .../src/layout/constraint.rs | 16 +- .../src/layout/direction.rs | 0 {ratatui => ratatui-core}/src/layout/flex.rs | 0 .../src/layout/layout.rs | 73 +- .../src/layout/margin.rs | 0 .../src/layout/position.rs | 2 +- {ratatui => ratatui-core}/src/layout/rect.rs | 30 +- .../src/layout/rect/iter.rs | 0 {ratatui => ratatui-core}/src/layout/size.rs | 0 ratatui-core/src/lib.rs | 7 + {ratatui => ratatui-core}/src/style.rs | 49 +- {ratatui => ratatui-core}/src/style/color.rs | 16 +- .../src/style/palette.rs | 0 .../src/style/palette/material.rs | 2 +- .../src/style/palette/tailwind.rs | 2 +- .../src/style/palette_conversion.rs | 4 +- .../src/style/stylize.rs | 4 +- {ratatui => ratatui-core}/src/symbols.rs | 0 .../src/symbols/border.rs | 0 {ratatui => ratatui-core}/src/symbols/line.rs | 0 {ratatui => ratatui-core}/src/text.rs | 16 +- .../src/text/grapheme.rs | 2 +- {ratatui => ratatui-core}/src/text/line.rs | 48 +- {ratatui => ratatui-core}/src/text/masked.rs | 12 +- {ratatui => ratatui-core}/src/text/span.rs | 33 +- {ratatui => ratatui-core}/src/text/text.rs | 51 +- ratatui-core/src/widgets.rs | 693 +++++++++++++++++ ratatui/Cargo.toml | 35 +- ratatui/src/backend/termwiz.rs | 23 +- ratatui/src/backend/test.rs | 2 +- ratatui/src/lib.rs | 8 +- ratatui/src/widgets.rs | 694 +----------------- 43 files changed, 1073 insertions(+), 928 deletions(-) create mode 100644 ratatui-core/Cargo.toml create mode 100644 ratatui-core/README.md rename {ratatui => ratatui-core}/src/buffer.rs (100%) rename {ratatui => ratatui-core}/src/buffer/assert.rs (100%) rename {ratatui => ratatui-core}/src/buffer/buffer.rs (99%) rename {ratatui => ratatui-core}/src/buffer/cell.rs (100%) rename {ratatui => ratatui-core}/src/layout.rs (100%) rename {ratatui => ratatui-core}/src/layout/alignment.rs (100%) rename {ratatui => ratatui-core}/src/layout/constraint.rs (97%) rename {ratatui => ratatui-core}/src/layout/direction.rs (100%) rename {ratatui => ratatui-core}/src/layout/flex.rs (100%) rename {ratatui => ratatui-core}/src/layout/layout.rs (98%) rename {ratatui => ratatui-core}/src/layout/margin.rs (100%) rename {ratatui => ratatui-core}/src/layout/position.rs (98%) rename {ratatui => ratatui-core}/src/layout/rect.rs (96%) rename {ratatui => ratatui-core}/src/layout/rect/iter.rs (100%) rename {ratatui => ratatui-core}/src/layout/size.rs (100%) create mode 100644 ratatui-core/src/lib.rs rename {ratatui => ratatui-core}/src/style.rs (96%) rename {ratatui => ratatui-core}/src/style/color.rs (98%) rename {ratatui => ratatui-core}/src/style/palette.rs (100%) rename {ratatui => ratatui-core}/src/style/palette/material.rs (99%) rename {ratatui => ratatui-core}/src/style/palette/tailwind.rs (99%) rename {ratatui => ratatui-core}/src/style/palette_conversion.rs (96%) rename {ratatui => ratatui-core}/src/style/stylize.rs (99%) rename {ratatui => ratatui-core}/src/symbols.rs (100%) rename {ratatui => ratatui-core}/src/symbols/border.rs (100%) rename {ratatui => ratatui-core}/src/symbols/line.rs (100%) rename {ratatui => ratatui-core}/src/text.rs (84%) rename {ratatui => ratatui-core}/src/text/grapheme.rs (97%) rename {ratatui => ratatui-core}/src/text/line.rs (98%) rename {ratatui => ratatui-core}/src/text/masked.rs (93%) rename {ratatui => ratatui-core}/src/text/span.rs (97%) rename {ratatui => ratatui-core}/src/text/text.rs (97%) create mode 100644 ratatui-core/src/widgets.rs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 50cde3e0..1d1c609d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -10,6 +10,8 @@ GitHub with a [breaking change] label. This is a quick summary of the sections below: +- [Unreleased](#unreleased) + - The `From` impls for backend types are now replaced with more specific traits - [v0.29.0](#v0290) - `Sparkline::data` takes `IntoIterator` instead of `&[u64]` and is no longer const - Removed public fields from `Rect` iterators @@ -104,7 +106,7 @@ let crossterm_attribute = crossterm::style::types::Attribute::Bold; + let crossterm_attribute = ratatui_modifier.into_crossterm(); ``` -Similar conversions for `ContentStyle` -> `Style` and `Attributes` -> `Modifier` exist for +Similar conversions for `ContentStyle` -> `Style` and `Attributes` -> `Modifier` exist for Crossterm and the various Termion and Termwiz types as well. ## [v0.29.0](https://github.com/ratatui/ratatui/releases/tag/v0.29.0) diff --git a/Cargo.lock b/Cargo.lock index 1c965aae..5d14a0b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2090,9 +2090,7 @@ version = "0.29.0" dependencies = [ "argh", "bitflags 2.6.0", - "cassowary", "color-eyre", - "compact_str", "criterion", "crossterm", "document-features", @@ -2102,13 +2100,12 @@ dependencies = [ "indoc", "instability", "itertools 0.13.0", - "lru", "octocrab", "palette", - "paste", "pretty_assertions", "rand 0.8.5", "rand_chacha 0.3.1", + "ratatui-core", "rstest", "serde", "serde_json", @@ -2121,6 +2118,28 @@ dependencies = [ "tracing-appender", "tracing-subscriber", "unicode-segmentation", + "unicode-width 0.2.0", +] + +[[package]] +name = "ratatui-core" +version = "0.3.0" +dependencies = [ + "bitflags 2.6.0", + "cassowary", + "compact_str", + "indoc", + "instability", + "itertools 0.13.0", + "lru", + "palette", + "paste", + "pretty_assertions", + "rstest", + "serde", + "serde_json", + "strum", + "unicode-segmentation", "unicode-truncate", "unicode-width 0.2.0", ] @@ -2454,18 +2473,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.213" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" +checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.213" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" +checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 67cae2b6..10dbc885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" -members = ["ratatui", "xtask"] -default-members = ["ratatui"] +members = ["ratatui", "ratatui-core", "xtask"] +default-members = ["ratatui", "ratatui-core"] [workspace.package] authors = ["Florian Dehau ", "The Ratatui Developers"] @@ -23,6 +23,20 @@ exclude = [ edition = "2021" rust-version = "1.74.0" +[workspace.dependencies] +bitflags = "2.6.0" +indoc = "2.0.5" +instability = "0.3.1" +itertools = "0.13.0" +pretty_assertions = "1.4.1" +ratatui-core = { path = "ratatui-core" } +rstest = "0.23.0" +serde = { version = "1.0.214", features = ["derive"] } +strum = { version = "0.26.3", features = ["derive"] } +unicode-segmentation = "1.12.0" +# See for information about why we pin unicode-width +unicode-width = "=0.2.0" + # Improve benchmark consistency [profile.bench] codegen-units = 1 diff --git a/ratatui-core/Cargo.toml b/ratatui-core/Cargo.toml new file mode 100644 index 00000000..a1d5c0b9 --- /dev/null +++ b/ratatui-core/Cargo.toml @@ -0,0 +1,70 @@ +[package] +name = "ratatui-core" +description = """ + Core types and traits for the Ratatui Terminal UI library. + Widget libraries should use this crate. Applications should use the main Ratatui crate. +""" +version = "0.3.0" +authors.workspace = true +documentation.workspace = true +repository.workspace = true +homepage.workspace = true +keywords.workspace = true +categories.workspace = true +readme.workspace = true +license.workspace = true +exclude.workspace = true +edition.workspace = true +rust-version.workspace = true + +[features] +## enables conversions from colors in the [`palette`] crate to [`Color`](crate::style::Color). +palette = ["dep:palette"] + +## enables the backend code that sets the underline color. Underline color is only supported by +## the Crossterm backend, and is not supported on Windows 7. +underline-color = [] + +## 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", "bitflags/serde", "compact_str/serde"] + +## enable all unstable features. +unstable = ["unstable-widget-ref"] + +## enables the [`WidgetRef`] and [`StatefulWidgetRef`] traits which are experimental and may change +## in the future. +## +## [`WidgetRef`]: widgets::WidgetRef +## [`StatefulWidgetRef`]: widgets::StatefulWidgetRef +unstable-widget-ref = [] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[dependencies] +bitflags = "2.3" +cassowary = "0.3" +compact_str = "0.8.0" +instability.workspace = true +indoc.workspace = true +itertools.workspace = true +lru = "0.12.0" +palette = { version = "0.7.6", optional = true } +paste = "1.0.2" +serde = { workspace = true, optional = true } +strum.workspace = true +unicode-segmentation.workspace = true +unicode-truncate = "1" +unicode-width.workspace = true + +[dev-dependencies] +rstest.workspace = true +pretty_assertions.workspace = true +serde_json = "1.0.132" + +[lints.clippy] +# 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" diff --git a/ratatui-core/README.md b/ratatui-core/README.md new file mode 100644 index 00000000..0315c4d0 --- /dev/null +++ b/ratatui-core/README.md @@ -0,0 +1,34 @@ +# ratatui-core + +[![Crates.io](https://img.shields.io/crates/v/ratatui-core)](https://crates.io/crates/ratatui-core) +[![Documentation](https://docs.rs/ratatui-core/badge.svg)](https://docs.rs/ratatui-core) +[![License](https://img.shields.io/crates/l/ratatui-core)](../LICENSE) + +## Overview + +**ratatui-core** is the core library of the [ratatui](https://github.com/ratatui/ratatui) project, +providing the essential building blocks for creating rich terminal user interfaces in Rust. + +### Why ratatui-core? + +The `ratatui-core` crate is split from the main [`ratatui`](https://crates.io/crates/ratatui) crate +to offer better stability for widget library authors. Widget libraries should generally depend on +`ratatui-core`, benefiting from a stable API and reducing the need for frequent updates. +Applications, on the other hand, should depend on the main `ratatui` crate, which includes built-in +widgets and additional features. + +## Installation + +Add `ratatui-core` to your `Cargo.toml`: + +```shell +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. + +## License + +This project is licensed under the MIT License. See the [LICENSE](../LICENSE) file for details. diff --git a/ratatui/src/buffer.rs b/ratatui-core/src/buffer.rs similarity index 100% rename from ratatui/src/buffer.rs rename to ratatui-core/src/buffer.rs diff --git a/ratatui/src/buffer/assert.rs b/ratatui-core/src/buffer/assert.rs similarity index 100% rename from ratatui/src/buffer/assert.rs rename to ratatui-core/src/buffer/assert.rs diff --git a/ratatui/src/buffer/buffer.rs b/ratatui-core/src/buffer/buffer.rs similarity index 99% rename from ratatui/src/buffer/buffer.rs rename to ratatui-core/src/buffer/buffer.rs index dc73aeb3..a31da0e4 100644 --- a/ratatui/src/buffer/buffer.rs +++ b/ratatui-core/src/buffer/buffer.rs @@ -23,7 +23,7 @@ use crate::{ /// # Examples: /// /// ``` -/// use ratatui::{ +/// use ratatui_core::{ /// buffer::{Buffer, Cell}, /// layout::{Position, Rect}, /// style::{Color, Style}, @@ -168,7 +168,7 @@ impl Buffer { /// # Examples /// /// ```rust - /// use ratatui::{ + /// use ratatui_core::{ /// buffer::{Buffer, Cell}, /// layout::{Position, Rect}, /// }; @@ -199,7 +199,7 @@ impl Buffer { /// # Examples /// /// ```rust - /// use ratatui::{ + /// use ratatui_core::{ /// buffer::{Buffer, Cell}, /// layout::{Position, Rect}, /// style::{Color, Style}, @@ -227,7 +227,7 @@ impl Buffer { /// # Examples /// /// ``` - /// use ratatui::{buffer::Buffer, layout::Rect}; + /// use ratatui_core::{buffer::Buffer, layout::Rect}; /// /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); /// // Global coordinates to the top corner of this buffer's area @@ -239,7 +239,7 @@ impl Buffer { /// Panics when given an coordinate that is outside of this Buffer's area. /// /// ```should_panic - /// use ratatui::{buffer::Buffer, layout::Rect}; + /// use ratatui_core::{buffer::Buffer, layout::Rect}; /// /// let buffer = Buffer::empty(Rect::new(200, 100, 10, 10)); /// // Top coordinate is outside of the buffer in global coordinate space, as the Buffer's area @@ -282,7 +282,7 @@ impl Buffer { /// # Examples /// /// ``` - /// use ratatui::{buffer::Buffer, layout::Rect}; + /// use ratatui_core::{buffer::Buffer, layout::Rect}; /// /// let rect = Rect::new(200, 100, 10, 10); /// let buffer = Buffer::empty(rect); @@ -295,7 +295,7 @@ impl Buffer { /// Panics when given an index that is outside the Buffer's content. /// /// ```should_panic - /// use ratatui::{buffer::Buffer, layout::Rect}; + /// use ratatui_core::{buffer::Buffer, layout::Rect}; /// /// let rect = Rect::new(0, 0, 10, 10); // 100 cells in total /// let buffer = Buffer::empty(rect); @@ -526,7 +526,7 @@ impl> Index

for Buffer { /// # Examples /// /// ``` - /// use ratatui::{ + /// use ratatui_core::{ /// buffer::{Buffer, Cell}, /// layout::{Position, Rect}, /// }; @@ -556,7 +556,7 @@ impl> IndexMut

for Buffer { /// # Examples /// /// ``` - /// use ratatui::{ + /// use ratatui_core::{ /// buffer::{Buffer, Cell}, /// layout::{Position, Rect}, /// }; diff --git a/ratatui/src/buffer/cell.rs b/ratatui-core/src/buffer/cell.rs similarity index 100% rename from ratatui/src/buffer/cell.rs rename to ratatui-core/src/buffer/cell.rs diff --git a/ratatui/src/layout.rs b/ratatui-core/src/layout.rs similarity index 100% rename from ratatui/src/layout.rs rename to ratatui-core/src/layout.rs diff --git a/ratatui/src/layout/alignment.rs b/ratatui-core/src/layout/alignment.rs similarity index 100% rename from ratatui/src/layout/alignment.rs rename to ratatui-core/src/layout/alignment.rs diff --git a/ratatui/src/layout/constraint.rs b/ratatui-core/src/layout/constraint.rs similarity index 97% rename from ratatui/src/layout/constraint.rs rename to ratatui-core/src/layout/constraint.rs index 6c401520..c17dac73 100644 --- a/ratatui/src/layout/constraint.rs +++ b/ratatui-core/src/layout/constraint.rs @@ -26,7 +26,7 @@ use strum::EnumIs; /// `Constraint` provides helper methods to create lists of constraints from various input formats. /// /// ```rust -/// use ratatui::layout::Constraint; +/// use ratatui_core::layout::Constraint; /// /// // Create a layout with specified lengths for each element /// let constraints = Constraint::from_lengths([10, 20, 10]); @@ -224,7 +224,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_lengths([1, 2, 3]); @@ -242,7 +242,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_ratios([(1, 4), (1, 2), (1, 4)]); @@ -260,7 +260,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_percentages([25, 50, 25]); @@ -278,7 +278,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_maxes([1, 2, 3]); @@ -296,7 +296,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_mins([1, 2, 3]); @@ -314,7 +314,7 @@ impl Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// # let area = Rect::default(); /// let constraints = Constraint::from_fills([1, 2, 3]); @@ -337,7 +337,7 @@ impl From for Constraint { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Direction, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Direction, Layout, Rect}; /// /// # let area = Rect::default(); /// let layout = Layout::new(Direction::Vertical, [1, 2, 3]).split(area); diff --git a/ratatui/src/layout/direction.rs b/ratatui-core/src/layout/direction.rs similarity index 100% rename from ratatui/src/layout/direction.rs rename to ratatui-core/src/layout/direction.rs diff --git a/ratatui/src/layout/flex.rs b/ratatui-core/src/layout/flex.rs similarity index 100% rename from ratatui/src/layout/flex.rs rename to ratatui-core/src/layout/flex.rs diff --git a/ratatui/src/layout/layout.rs b/ratatui-core/src/layout/layout.rs similarity index 98% rename from ratatui/src/layout/layout.rs rename to ratatui-core/src/layout/layout.rs index aace07ad..f282b337 100644 --- a/ratatui/src/layout/layout.rs +++ b/ratatui-core/src/layout/layout.rs @@ -148,20 +148,18 @@ impl From for Spacing { /// # Example /// /// ```rust -/// use ratatui::{ +/// use ratatui_core::{ +/// buffer::Buffer, /// layout::{Constraint, Direction, Layout, Rect}, -/// widgets::Paragraph, -/// Frame, +/// text::Text, +/// widgets::Widget, /// }; /// -/// fn render(frame: &mut Frame, area: Rect) { -/// let layout = Layout::new( -/// Direction::Vertical, -/// [Constraint::Length(5), Constraint::Min(0)], -/// ) -/// .split(Rect::new(0, 0, 10, 10)); -/// frame.render_widget(Paragraph::new("foo"), layout[0]); -/// frame.render_widget(Paragraph::new("bar"), layout[1]); +/// fn render(area: Rect, buf: &mut ratatui_core::buffer::Buffer) { +/// let layout = Layout::vertical([Constraint::Length(5), Constraint::Min(0)]); +/// let [left, right] = layout.areas(area); +/// Text::from("foo").render(left, buf); +/// Text::from("bar").render(right, buf); /// } /// ``` /// @@ -206,7 +204,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Direction, Layout}; + /// use ratatui_core::layout::{Constraint, Direction, Layout}; /// /// Layout::new( /// Direction::Horizontal, @@ -240,7 +238,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout}; + /// use ratatui_core::layout::{Constraint, Layout}; /// /// let layout = Layout::vertical([Constraint::Length(5), Constraint::Min(0)]); /// ``` @@ -260,7 +258,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout}; + /// use ratatui_core::layout::{Constraint, Layout}; /// /// let layout = Layout::horizontal([Constraint::Length(5), Constraint::Min(0)]); /// ``` @@ -289,7 +287,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Direction, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Direction, Layout, Rect}; /// /// let layout = Layout::default() /// .direction(Direction::Horizontal) @@ -324,7 +322,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// let layout = Layout::default() /// .constraints([ @@ -369,7 +367,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// let layout = Layout::default() /// .constraints([Constraint::Min(0)]) @@ -391,7 +389,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// let layout = Layout::default() /// .constraints([Constraint::Min(0)]) @@ -410,7 +408,7 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::layout::{Constraint, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// /// let layout = Layout::default() /// .constraints([Constraint::Min(0)]) @@ -442,7 +440,7 @@ impl Layout { /// In this example, the items in the layout will be aligned to the start. /// /// ```rust - /// use ratatui::layout::{Constraint::*, Flex, Layout}; + /// use ratatui_core::layout::{Constraint::*, Flex, Layout}; /// /// let layout = Layout::horizontal([Length(20), Length(20), Length(20)]).flex(Flex::Start); /// ``` @@ -451,7 +449,7 @@ impl Layout { /// space. /// /// ```rust - /// use ratatui::layout::{Constraint::*, Flex, Layout}; + /// use ratatui_core::layout::{Constraint::*, Flex, Layout}; /// /// let layout = Layout::horizontal([Length(20), Length(20), Length(20)]).flex(Flex::Legacy); /// ``` @@ -479,7 +477,7 @@ impl Layout { /// In this example, the spacing between each item in the layout is set to 2 cells. /// /// ```rust - /// use ratatui::layout::{Constraint::*, Layout}; + /// use ratatui_core::layout::{Constraint::*, Layout}; /// /// let layout = Layout::horizontal([Length(20), Length(20), Length(20)]).spacing(2); /// ``` @@ -488,7 +486,7 @@ impl Layout { /// three segments will have an overlapping border. /// /// ```rust - /// use ratatui::layout::{Constraint::*, Layout}; + /// use ratatui_core::layout::{Constraint::*, Layout}; /// let layout = Layout::horizontal([Length(20), Length(20), Length(20)]).spacing(-1); /// ``` #[must_use = "method moves the value of self and returns the modified value"] @@ -515,16 +513,14 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::{layout::{Layout, Constraint}, Frame}; + /// use ratatui_core::layout::{Layout, Constraint, Rect}; /// - /// # fn render(frame: &mut Frame) { - /// let area = frame.area(); + /// let area = Rect::new(0, 0, 10, 10); /// let layout = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]); /// let [top, main] = layout.areas(area); /// /// // or explicitly specify the number of constraints: /// let areas = layout.areas::<2>(area); - /// # } pub fn areas(&self, area: Rect) -> [Rect; N] { let (areas, _) = self.split_with_spacers(area); areas.as_ref().try_into().expect("invalid number of rects") @@ -548,17 +544,16 @@ impl Layout { /// # Examples /// /// ```rust - /// use ratatui::{layout::{Layout, Constraint}, Frame}; + /// use ratatui_core::layout::{Constraint, Layout, Rect}; /// - /// # fn render(frame: &mut Frame) { - /// let area = frame.area(); + /// let area = Rect::new(0, 0, 10, 10); /// let layout = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]); /// let [top, main] = layout.areas(area); /// let [before, inbetween, after] = layout.spacers(area); /// /// // or explicitly specify the number of constraints: - /// let spacers = layout.spacers::<2>(area); - /// # } + /// let spacers = layout.spacers::<3>(area); + /// ``` pub fn spacers(&self, area: Rect) -> [Rect; N] { let (_, spacers) = self.split_with_spacers(area); spacers @@ -588,7 +583,7 @@ impl Layout { /// # Examples /// /// ``` - /// use ratatui::layout::{Constraint, Direction, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Direction, Layout, Rect}; /// let layout = Layout::default() /// .direction(Direction::Vertical) /// .constraints([Constraint::Length(5), Constraint::Min(0)]) @@ -620,7 +615,7 @@ impl Layout { /// # Examples /// /// ``` - /// use ratatui::layout::{Constraint, Direction, Layout, Rect}; + /// use ratatui_core::layout::{Constraint, Direction, Layout, Rect}; /// /// let (areas, spacers) = Layout::default() /// .direction(Direction::Vertical) @@ -1413,8 +1408,12 @@ mod tests { use crate::{ buffer::Buffer, - layout::{Constraint, Constraint::*, Direction, Flex, Layout, Rect}, - widgets::{Paragraph, Widget}, + layout::{ + Constraint::{self, *}, + Direction, Flex, Layout, Rect, + }, + text::Text, + widgets::Widget, }; /// Test that the given constraints applied to the given area result in the expected layout. @@ -1436,7 +1435,7 @@ mod tests { let mut buffer = Buffer::empty(area); for (c, &area) in ('a'..='z').take(constraints.len()).zip(layout.iter()) { let s = c.to_string().repeat(area.width as usize); - Paragraph::new(s).render(area, &mut buffer); + Text::from(s).render(area, &mut buffer); } assert_eq!(buffer, Buffer::with_lines([expected])); } diff --git a/ratatui/src/layout/margin.rs b/ratatui-core/src/layout/margin.rs similarity index 100% rename from ratatui/src/layout/margin.rs rename to ratatui-core/src/layout/margin.rs diff --git a/ratatui/src/layout/position.rs b/ratatui-core/src/layout/position.rs similarity index 98% rename from ratatui/src/layout/position.rs rename to ratatui-core/src/layout/position.rs index dc23cf5b..bd327473 100644 --- a/ratatui/src/layout/position.rs +++ b/ratatui-core/src/layout/position.rs @@ -12,7 +12,7 @@ use crate::layout::Rect; /// # Examples /// /// ``` -/// use ratatui::layout::{Position, Rect}; +/// use ratatui_core::layout::{Position, Rect}; /// /// // the following are all equivalent /// let position = Position { x: 1, y: 2 }; diff --git a/ratatui/src/layout/rect.rs b/ratatui-core/src/layout/rect.rs similarity index 96% rename from ratatui/src/layout/rect.rs rename to ratatui-core/src/layout/rect.rs index b7053899..93b9b5e9 100644 --- a/ratatui/src/layout/rect.rs +++ b/ratatui-core/src/layout/rect.rs @@ -64,7 +64,7 @@ impl Rect { /// # Examples /// /// ``` - /// use ratatui::layout::Rect; + /// use ratatui_core::layout::Rect; /// /// let rect = Rect::new(1, 2, 3, 4); /// ``` @@ -213,7 +213,7 @@ impl Rect { /// # Examples /// /// ```rust - /// use ratatui::layout::{Position, Rect}; + /// use ratatui_core::layout::{Position, Rect}; /// /// let rect = Rect::new(1, 2, 3, 4); /// assert!(rect.contains(Position { x: 1, y: 2 })); @@ -243,12 +243,11 @@ impl Rect { /// # Examples /// /// ```rust - /// use ratatui::{layout::Rect, Frame}; + /// use ratatui_core::layout::Rect; /// - /// # fn render(frame: &mut Frame) { - /// let area = frame.area(); - /// let rect = Rect::new(0, 0, 100, 100).clamp(area); - /// # } + /// let area = Rect::new(0, 0, 100, 100); + /// let rect = Rect::new(80, 80, 30, 30).clamp(area); + /// assert_eq!(rect, Rect::new(70, 70, 30, 30)); /// ``` #[must_use = "method returns the modified value"] pub fn clamp(self, other: Self) -> Self { @@ -264,7 +263,7 @@ impl Rect { /// # Example /// /// ``` - /// use ratatui::{buffer::Buffer, layout::Rect, text::Line, widgets::Widget}; + /// use ratatui_core::{buffer::Buffer, layout::Rect, text::Line, widgets::Widget}; /// /// fn render(area: Rect, buf: &mut Buffer) { /// for row in area.rows() { @@ -281,14 +280,11 @@ impl Rect { /// # Example /// /// ``` - /// use ratatui::{ - /// buffer::Buffer, - /// layout::Rect, - /// widgets::{Block, Borders, Widget}, - /// }; + /// use ratatui_core::{buffer::Buffer, layout::Rect, text::Text, widgets::Widget}; + /// /// fn render(area: Rect, buf: &mut Buffer) { - /// if let Some(left) = area.columns().next() { - /// Block::new().borders(Borders::LEFT).render(left, buf); + /// for (i, column) in area.columns().enumerate() { + /// Text::from(format!("{}", i)).render(column, buf); /// } /// } /// ``` @@ -303,7 +299,7 @@ impl Rect { /// # Example /// /// ``` - /// use ratatui::{buffer::Buffer, layout::Rect}; + /// use ratatui_core::{buffer::Buffer, layout::Rect}; /// /// fn render(area: Rect, buf: &mut Buffer) { /// for position in area.positions() { @@ -320,7 +316,7 @@ impl Rect { /// # Examples /// /// ``` - /// use ratatui::layout::Rect; + /// use ratatui_core::layout::Rect; /// /// let rect = Rect::new(1, 2, 3, 4); /// let position = rect.as_position(); diff --git a/ratatui/src/layout/rect/iter.rs b/ratatui-core/src/layout/rect/iter.rs similarity index 100% rename from ratatui/src/layout/rect/iter.rs rename to ratatui-core/src/layout/rect/iter.rs diff --git a/ratatui/src/layout/size.rs b/ratatui-core/src/layout/size.rs similarity index 100% rename from ratatui/src/layout/size.rs rename to ratatui-core/src/layout/size.rs diff --git a/ratatui-core/src/lib.rs b/ratatui-core/src/lib.rs new file mode 100644 index 00000000..0c936973 --- /dev/null +++ b/ratatui-core/src/lib.rs @@ -0,0 +1,7 @@ +#![doc = include_str!("../README.md")] +pub mod buffer; +pub mod layout; +pub mod style; +pub mod symbols; +pub mod text; +pub mod widgets; diff --git a/ratatui/src/style.rs b/ratatui-core/src/style.rs similarity index 96% rename from ratatui/src/style.rs rename to ratatui-core/src/style.rs index 8d8ec492..a9a85239 100644 --- a/ratatui/src/style.rs +++ b/ratatui-core/src/style.rs @@ -13,7 +13,7 @@ //! ## Example //! //! ``` -//! use ratatui::{ +//! use ratatui_core::{ //! style::{Color, Modifier, Style}, //! text::Span, //! }; @@ -43,10 +43,9 @@ //! ## Example //! //! ``` -//! use ratatui::{ +//! use ratatui_core::{ //! style::{Color, Modifier, Style, Stylize}, -//! text::Span, -//! widgets::Paragraph, +//! text::{Span, Text}, //! }; //! //! assert_eq!( @@ -61,8 +60,8 @@ //! ); //! //! assert_eq!( -//! Paragraph::new("hello").red().on_blue().bold(), -//! Paragraph::new("hello").style( +//! Text::from("hello").red().on_blue().bold(), +//! Text::from("hello").style( //! Style::default() //! .fg(Color::Red) //! .bg(Color::Blue) @@ -97,7 +96,7 @@ bitflags! { /// ## Examples /// /// ```rust - /// use ratatui::style::Modifier; + /// use ratatui_core::style::Modifier; /// /// let m = Modifier::BOLD | Modifier::ITALIC; /// ``` @@ -133,7 +132,7 @@ impl fmt::Debug for Modifier { /// Style lets you control the main characteristics of the displayed elements. /// /// ```rust -/// use ratatui::style::{Color, Modifier, Style}; +/// use ratatui_core::style::{Color, Modifier, Style}; /// /// Style::default() /// .fg(Color::Black) @@ -144,7 +143,7 @@ impl fmt::Debug for Modifier { /// Styles can also be created with a [shorthand notation](crate::style#using-style-shorthands). /// /// ```rust -/// use ratatui::style::{Style, Stylize}; +/// use ratatui_core::style::{Style, Stylize}; /// /// Style::new().black().on_green().italic().bold(); /// ``` @@ -155,7 +154,7 @@ impl fmt::Debug for Modifier { /// anywhere that accepts `Into