ratatui_kit/components/
text.rs1use crate::{AnyElement, element, prelude::Fragment};
2use ratatui::{
3 buffer::Buffer,
4 layout::{Position, Rect},
5 style::Style,
6 widgets::{Paragraph, Widget},
7};
8use ratatui_kit_macros::{Props, component};
9use std::ops::{Deref, DerefMut};
10
11#[derive(Clone, Default)]
12pub struct TextParagraph<'a> {
13 inner: Paragraph<'a>,
14}
15
16unsafe impl Send for TextParagraph<'_> {}
22unsafe impl Sync for TextParagraph<'_> {}
23
24impl<'a> Deref for TextParagraph<'a> {
25 type Target = Paragraph<'a>;
26
27 fn deref(&self) -> &Self::Target {
28 &self.inner
29 }
30}
31
32impl DerefMut for TextParagraph<'_> {
33 fn deref_mut(&mut self) -> &mut Self::Target {
34 &mut self.inner
35 }
36}
37
38impl Widget for TextParagraph<'_> {
41 fn render(self, area: Rect, buf: &mut Buffer) {
42 self.inner.render(area, buf);
43 }
44}
45
46impl From<String> for TextParagraph<'_> {
47 fn from(value: String) -> Self {
48 Self {
49 inner: Paragraph::new(value),
50 }
51 }
52}
53
54impl<'a> From<Paragraph<'a>> for TextParagraph<'a> {
55 fn from(value: Paragraph<'a>) -> Self {
56 Self { inner: value }
57 }
58}
59
60#[derive(Default, Props)]
61pub struct TextProps {
62 pub text: TextParagraph<'static>,
63 pub style: Style,
64 pub alignment: ratatui::layout::Alignment,
65 pub scroll: Position,
66 pub wrap: Option<bool>,
67}
68
69#[component]
70pub fn Text(props: &TextProps) -> impl Into<AnyElement<'static>> {
71 let paragraph = props
72 .text
73 .inner
74 .clone()
75 .style(props.style)
76 .scroll((props.scroll.x, props.scroll.y))
77 .alignment(props.alignment);
78
79 let paragraph = if let Some(wrap) = props.wrap {
80 paragraph.wrap(ratatui::widgets::Wrap { trim: wrap })
81 } else {
82 paragraph
83 };
84
85 let paragraph = TextParagraph::from(paragraph);
87
88 element! {
89 Fragment{
90 $paragraph
91 }
92 }
93}