25 Commits

Author SHA1 Message Date
Josh McKinney
40e96a2a04
docs(block): add collapsed border example (#1899) 2025-07-13 01:30:57 -07:00
Josh McKinney
6dcd53bc6b
feat: add ergonomic methods for layouting Rects (#1909)
This commit introduces new methods for the `Rect` struct that simplify
the process of splitting a `Rect` into sub-rects according to a given
`Layout`. By putting these methods on the `Rect` struct, we make it a
bit more natural that a layout is applied to the `Rect` itself, rather
than passing a `Rect` to the `Layout` struct to be split.

Adds:
- `Rect::layout` and `Rect::try_layout` methods that allow splitting a
  `Rect` into an array of sub-rects according to a given `Layout`.
- `Rect::layout_vec` method that returns a `Vec` of sub-rects.
- `Layout::try_areas` method that returns an array of sub-rects, with
  compile-time checks for the number of constraints. This is added
  mainly for consistency with the new `Rect` methods.

```rust
use ratatui_core::layout::{Layout, Constraint, Rect};
let area = Rect::new(0, 0, 10, 10);
let layout = Layout::vertical([Constraint::Fill(1); 2]);

// Rect::layout() infers the number of constraints at compile time:
let [top, main] = area.layout(&layout);

// Rect::try_layout() and Layout::try_areas() do the same, but return a
// Result:
let [top, main] = area.try_layout(&layout)?;
let [top, main] = layout.try_areas(area)?;

// Rect::layout_vec() returns a Vec of sub-rects:
let areas_vec = area.layout_vec(&layout);

// you can also explicitly specify the number of constraints:
let areas = area.layout::<2>(&layout);
let areas = area.try_layout::<2>(&layout)?;
let areas = layout.try_areas::<2>(area)?;
```
2025-06-28 01:23:34 -07:00
Josh McKinney
7bc78bca1b
feat: add ratatui::run() method (#1707)
This introduces a new `ratatui::run()` method which runs a closure with
a terminal initialized with reasonable defaults for most applications.
This calls `ratatui::init()` before running the closure and
`ratatui::restore()` after the closure completes, and returns the result
of the closure.

A minimal hello world example using the new `ratatui::run()` method:

```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
    ratatui::run(|terminal| {
        loop {
            terminal.draw(|frame| frame.render_widget("Hello World!", frame.area()))?;
            if crossterm::event::read()?.is_key_press() {
                break Ok(());
            }
        }
    })
}
```

Of course, this also works both with apps that use free methods and
structs:

```rust
fn run(terminal: &mut DefaultTerminal) -> Result<(), AppError> { ... }

ratatui::run(run)?;
```

```rust
struct App { ... }

impl App {
    fn new() -> Self { ... }
    fn run(mut self, terminal: &mut DefaultTerminal) -> Result<(), AppError> { ... }
}

ratatui::run(|terminal| App::new().run(terminal))?;
```
2025-06-25 03:20:42 -07:00
Tyler Breisacher
dbfb2c3399
chore: upgrade to Rust Edition 2024 (#1863)
https://doc.rust-lang.org/edition-guide/rust-2024/index.html

Fixes #1727
2025-05-19 01:17:03 -07:00
Josh McKinney
8d60e96b2b
refactor(examples): use crossterm event methods (#1792)
Crossterm 0.29 introduced methods to easily check / extract the event
type. E.g. as_key_press_event() and is_key_press(). This commit
updates the examples to use these methods instead of matching on
the event type. This makes the code cleaner and easier to read.

Also does a general cleanup of the event handling code in the examples.
2025-05-10 10:35:12 -07:00
Tyler Breisacher
d88cd29079
chore: Add 'const' to functions where possible. (#1802)
The Clippy check for this (missing_const_for_fn) is already enabled, but
catches more cases in upcoming toolchain versions.

This is part of the work to unblock #1727
2025-04-20 18:48:49 -07:00
Jagoda Estera Ślązak
5a232a3115
feat(no_std): remove redundant std usages in ratatui-widgets (#1762) 2025-04-08 08:59:31 -07:00
Josh McKinney
2739391950
style: use Module imports_granularity (#1728)
I was swayed by the arguments about this made by the compiler team In
<https://github.com/rust-lang/compiler-team/issues/750> and decided to
look at how this organization affects ratatui. I found this reduces the
number of lines across the codebase by about 350 and makes the imports
more readable and definitely more greppable as you usually only have
to read a single line. I've found in the past that maintaining imports
regularly leads to merge conflicts which have to be resolved by hand
and this change should reduce the likelihood of that happening.

Main change is in rustfmt.toml, and the rest is just the result of
running `cargo xtask format`.

While implementing this, cargo machete brings up that the various
backend crates are unused by the example crates.

The re-export of each backend crate under ratatui is to make it possible
for libs that rely on a specific version of ratatui to use the same
version of the backend crate. Apps in general should use the backend
crate directly rather than through ratatui as this is less confusing.

- Removes all usages of `ratatui::{crossterm, termion, termwiz}`` in the
  examples.
- Adds the backend crate to the dependencies of the examples that use
  the backend crate directly.
2025-03-19 16:48:02 -07:00
Tayfun Bocek
92a19cb604
feat(list)!: highlight symbol styling (#1595)
Allow styling for `List`'s highlight symbol

This change makes it so anything that implements `Into<Line>` can be
used as a highlight symbol.

BREAKING CHANGE: `List::highlight_symbol` can no longer be used in const
context
BREAKING CHANGE: `List::highlight_symbol` accepted `&str`. Conversion
methods that rely on type inference will need to be rewritten as the
compiler cannot infer the type.

closes: https://github.com/ratatui/ratatui/issues/1443

---------

Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
2025-03-04 14:26:59 -08:00
Jens Krause
7ad9c29eac
feat(linegauge): customized symbols (#1601)
With this PR any symbol (`&str`) can be used to render `filled` and
`unfilled` parts of `LineGauge` now. Before that change, only
[`symbols::line::Set`](https://docs.rs/ratatui/latest/ratatui/symbols/line/struct.Set.html)
was accepted.

Note: New methods are introduced to define those symbols:
`filled_symbol` and `unfilled_symbol`. The method
[`line_set`](https://docs.rs/ratatui/latest/ratatui/widgets/struct.LineGauge.html#method.line_set)
is still there, but marked as `deprecated`.

![line_gauge](https://github.com/user-attachments/assets/cae308b8-151b-461d-8af6-9a20012adf2f)
2025-01-22 19:56:22 -08:00
Orhun Parmaksız
dafb716f9d
docs(widgets): add example for grouped barchart (#1566)
related #1512

---------

Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
2024-12-12 20:42:41 +03:00
Orhun Parmaksız
ed5dd73084
docs(widgets): add example for tabs (#1559)
related #1512 

Also removes the tabs example from ratatui crate since it overlaps with
this new example in terms of functionality and it was not following the
general theme of other examples.
2024-12-09 15:22:03 -08:00
Orhun Parmaksız
fab532171d
docs(widgets): add example for scrollbar (#1545)
Related to: #1512
2024-12-08 02:28:18 -08:00
Orhun Parmaksız
898aef6e2f
docs(widgets): add example for list (#1542)
Related to: #1512
2024-12-08 02:27:19 -08:00
Orhun Parmaksız
452366aa9e
docs(widgets): add example for sparkline (#1556)
related #1512

Also removes the sparkline example from ratatui crate since this example
is a simplified and easier to understand version of that
2024-12-07 18:35:45 +03:00
Orhun Parmaksız
6ddde0e8a8
docs(widgets): add example for table (#1557)
related #1512
2024-12-07 18:21:01 +03:00
Orhun Parmaksız
93ad6b828c
docs(widgets): update values in chart example (#1558)
better stonks
2024-12-07 18:20:49 +03:00
Orhun Parmaksız
15f442a71e
docs(widgets): add example for paragraph (#1544)
related #1512 

Also removes the paragraph example from `ratatui` since these examples
are more or less the same.
2024-12-04 21:16:04 +03:00
Orhun Parmaksız
17bba14540
docs(widgets): move the logo example to widgets (#1543)
related #1512 

Also updates the code to make it consistent with the other examples
2024-12-03 22:30:35 +03:00
Orhun Parmaksız
f2451e7f1e
docs(widgets): add example for gauge (#1539)
related #1512
2024-12-03 12:04:29 +03:00
Orhun Parmaksız
4f0a8b21af
docs(widgets): add example for canvas (#1533)
related #1512
2024-12-02 17:21:53 +03:00
Orhun Parmaksız
91147c4d75
docs(widgets): add example for chart (#1536)
stonks
2024-12-01 20:34:57 +03:00
Orhun Parmaksız
6dd25a3111
docs(widgets): add example for calendar (#1532)
related #1512
2024-12-01 14:12:06 +03:00
Orhun Parmaksız
d291042e69
docs(block): revise the block example (#1520)
- Moves the block example from `ratatui` to `ratatui-widgets`
- Simplifies the example (bordered, styled, custom borders)

see #1512
2024-11-26 22:38:40 +03:00
Josh McKinney
99ac005b06
docs(widgets): Add simple barchart example (#1511) 2024-11-24 01:32:54 -08:00