返回 CodeWhale
lib.rs
1 //! Codewhale's terminal component library for ratatui.
2 //!
3 //! Reusable components from Codewhale's current terminal UI. Native palettes
4 //! and layout are available through [`Theme::tui`], [`NativeComposer`],
5 //! [`Workbar`] and [`TerminalShell`]. Desktop token themes and additional
6 //! compositions remain available.
7 //!
8 //! ```no_run
9 //! use codewhale_ratatui::{Paint, Theme, KeyHint, KeyHints};
10 //! # fn draw(frame: &mut ratatui::Frame) {
11 //! let theme = Theme::detect().tui();
12 //! let hints = KeyHints::new(vec![
13 //! KeyHint::new("↑↓", "move"),
14 //! KeyHint::new("Enter", "select"),
15 //! KeyHint::new("Esc", "cancel"),
16 //! ]);
17 //! frame.render_widget(hints.themed(&theme), frame.area());
18 //! # }
19 //! ```
20
21 pub mod color;
22 pub mod detect;
23 pub mod glyphs;
24 pub mod keys;
25 pub mod ocean;
26 pub mod tui_palettes;
27 pub use tui_palettes::{TuiGround, TuiInk, TuiPalette};
28 pub mod ombre;
29 pub mod osc11;
30 #[rustfmt::skip]
31 mod roles;
32 pub mod text;
33 pub mod theme;
34 /// The vendored Codewhale design tokens (`vendor/codewhale-design/tokens.rs`):
35 /// spacing, type and motion constants alongside the colors.
36 #[path = "../vendor/codewhale-design/tokens.rs"]
37 pub mod tokens;
38
39 #[path = "../assets/avatar-pack/contract.rs"]
40 pub mod avatar;
41 pub mod avatar_sprite;
42 mod components;
43 pub mod gallery;
44 pub mod testing;
45 pub mod whale;
46 #[path = "../assets/whale-girl/player.rs"]
47 pub mod whale_girl;
48 pub mod whale_motion;
49
50 // Every component module is re-exported whole (see `components/mod.rs`), so
51 // a package that makes an item `pub` in its own file exports it from here
52 // without touching this file.
53 pub use components::*;
54 pub use ocean::{OceanColumn, OceanPhase, OceanRamp};
55 pub use ombre::{Ombre, OmbreDirection, WaterPalette};
56 pub use theme::{Caps, Ground, Role, Theme};
57 pub use whale::{Whale, WhaleState};
58
59 use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
60
61 /// A component that paints with a [`Theme`].
62 ///
63 /// Components hold only what they show. The theme arrives when they paint,
64 /// so nothing caches a color and a theme change reaches every component on
65 /// the next frame.
66 pub trait Paint {
67 fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme);
68
69 /// Rows this component wants at `width`.
70 fn height(&self, _width: u16, _theme: &Theme) -> u16 {
71 1
72 }
73
74 /// Wrap as a ratatui [`Widget`] for `frame.render_widget`.
75 ///
76 /// Render the returned wrapper by reference to reuse it across frames.
77 /// [`Themed::new`] also accepts a `dyn Paint` for heterogeneous collections.
78 fn themed<'a>(&'a self, theme: &'a Theme) -> Themed<'a, Self>
79 where
80 Self: Sized,
81 {
82 Themed::new(self, theme)
83 }
84 }
85
86 /// A component paired with the theme it paints with.
87 ///
88 /// The wrapper borrows both values. Rendering it by reference leaves the
89 /// component and wrapper available for another frame, without cloning either.
90 ///
91 /// ```no_run
92 /// use codewhale_ratatui::{NativeComposer, Paint, Theme};
93 /// # fn draw(frame: &mut ratatui::Frame<'_>) {
94 /// let theme = Theme::detect().tui();
95 /// let composer = NativeComposer::new("Review the changes");
96 /// let widget = composer.themed(&theme);
97 /// frame.render_widget(&widget, frame.area());
98 /// # }
99 /// ```
100 ///
101 /// Use [`Themed::new`] when the component's concrete type is erased:
102 ///
103 /// ```no_run
104 /// use codewhale_ratatui::{KeyHint, KeyHints, NativeComposer, Paint, Themed, Theme};
105 /// # fn draw(frame: &mut ratatui::Frame<'_>) {
106 /// let theme = Theme::detect().tui();
107 /// let composer = NativeComposer::new("Review the changes");
108 /// let hints = KeyHints::new(vec![KeyHint::new("Enter", "send")]);
109 /// let parts: [&dyn Paint; 2] = [&composer, &hints];
110 /// # let areas = [frame.area(), frame.area()];
111 /// for (part, area) in parts.into_iter().zip(areas) {
112 /// frame.render_widget(Themed::new(part, &theme), area);
113 /// }
114 /// # }
115 /// ```
116 #[must_use]
117 pub struct Themed<'a, P: Paint + ?Sized> {
118 component: &'a P,
119 theme: &'a Theme,
120 }
121
122 impl<'a, P: Paint + ?Sized> Themed<'a, P> {
123 /// Borrow a component and its theme, including a `dyn Paint` component.
124 pub const fn new(component: &'a P, theme: &'a Theme) -> Self {
125 Self { component, theme }
126 }
127 }
128
129 impl<P: Paint + ?Sized> Widget for Themed<'_, P> {
130 fn render(self, area: Rect, buf: &mut Buffer) {
131 (&self).render(area, buf);
132 }
133 }
134
135 impl<P: Paint + ?Sized> Widget for &Themed<'_, P> {
136 fn render(self, area: Rect, buf: &mut Buffer) {
137 self.component.paint(area, buf, self.theme);
138 }
139 }
140
140 lines RUST