feat: re-export backends from the ratatui crate (#1151)

`crossterm`, `termion`, and `termwiz` can now be accessed as
`ratatui::{crossterm, termion, termwiz}` respectively. This makes it
possible to just add the Ratatui crate as a dependency and use the
backend of choice without having to add the backend crates as
dependencies.

To update existing code, replace all instances of `crossterm::` with
`ratatui::crossterm::`, `termion::` with `ratatui::termion::`, and
`termwiz::` with `ratatui::termwiz::`.
This commit is contained in:
Josh McKinney 2024-05-28 13:23:39 -07:00 committed by GitHub
parent 4ce67fc84e
commit 74a32afbae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 295 additions and 234 deletions

View file

@ -43,10 +43,10 @@ Ratatui was forked from the [tui-rs] crate in 2023 in order to continue its deve
## Installation ## Installation
Add `ratatui` and `crossterm` as dependencies to your cargo.toml: Add `ratatui` as a dependency to your cargo.toml:
```shell ```shell
cargo add ratatui crossterm cargo add ratatui
``` ```
Ratatui uses [Crossterm] by default as it works on most platforms. See the [Installation] Ratatui uses [Crossterm] by default as it works on most platforms. See the [Installation]
@ -110,7 +110,8 @@ module] and the [Backends] section of the [Ratatui Website] for more info.
The drawing logic is delegated to a closure that takes a [`Frame`] instance as argument. The The drawing logic is delegated to a closure that takes a [`Frame`] instance as argument. The
[`Frame`] provides the size of the area to draw to and allows the app to render any [`Widget`] [`Frame`] provides the size of the area to draw to and allows the app to render any [`Widget`]
using the provided [`render_widget`] method. See the [Widgets] section of the [Ratatui Website] using the provided [`render_widget`] method. After this closure returns, a diff is performed and
only the changes are drawn to the terminal. See the [Widgets] section of the [Ratatui Website]
for more info. for more info.
### Handling events ### Handling events
@ -125,12 +126,17 @@ Website] for more info. For example, if you are using [Crossterm], you can use t
```rust ```rust
use std::io::{self, stdout}; use std::io::{self, stdout};
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode}, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event, KeyCode},
ExecutableCommand, terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
},
ExecutableCommand,
},
prelude::*,
widgets::*,
}; };
use ratatui::{prelude::*, widgets::*};
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
enable_raw_mode()?; enable_raw_mode()?;
@ -161,8 +167,7 @@ fn handle_events() -> io::Result<bool> {
fn ui(frame: &mut Frame) { fn ui(frame: &mut Frame) {
frame.render_widget( frame.render_widget(
Paragraph::new("Hello World!") Paragraph::new("Hello World!").block(Block::bordered().title("Greeting")),
.block(Block::bordered().title("Greeting")),
frame.size(), frame.size(),
); );
} }
@ -206,14 +211,8 @@ fn ui(frame: &mut Frame) {
[Constraint::Percentage(50), Constraint::Percentage(50)], [Constraint::Percentage(50), Constraint::Percentage(50)],
) )
.split(main_layout[1]); .split(main_layout[1]);
frame.render_widget( frame.render_widget(Block::bordered().title("Left"), inner_layout[0]);
Block::bordered().title("Left"), frame.render_widget(Block::bordered().title("Right"), inner_layout[1]);
inner_layout[0],
);
frame.render_widget(
Block::bordered().title("Right"),
inner_layout[1],
);
} }
``` ```
@ -331,17 +330,14 @@ Running this example produces the following output:
[License Badge]: https://img.shields.io/crates/l/ratatui?style=flat-square&color=1370D3 [License Badge]: https://img.shields.io/crates/l/ratatui?style=flat-square&color=1370D3
[CI Badge]: https://img.shields.io/github/actions/workflow/status/ratatui-org/ratatui/ci.yml?style=flat-square&logo=github [CI Badge]: https://img.shields.io/github/actions/workflow/status/ratatui-org/ratatui/ci.yml?style=flat-square&logo=github
[CI Workflow]: https://github.com/ratatui-org/ratatui/actions/workflows/ci.yml [CI Workflow]: https://github.com/ratatui-org/ratatui/actions/workflows/ci.yml
[Codecov Badge]: [Codecov Badge]: https://img.shields.io/codecov/c/github/ratatui-org/ratatui?logo=codecov&style=flat-square&token=BAQ8SOKEST&color=C43AC3&logoColor=C43AC3
https://img.shields.io/codecov/c/github/ratatui-org/ratatui?logo=codecov&style=flat-square&token=BAQ8SOKEST&color=C43AC3&logoColor=C43AC3
[Codecov]: https://app.codecov.io/gh/ratatui-org/ratatui [Codecov]: https://app.codecov.io/gh/ratatui-org/ratatui
[Deps.rs Badge]: https://deps.rs/repo/github/ratatui-org/ratatui/status.svg?style=flat-square [Deps.rs Badge]: https://deps.rs/repo/github/ratatui-org/ratatui/status.svg?style=flat-square
[Deps.rs]: https://deps.rs/repo/github/ratatui-org/ratatui [Deps.rs]: https://deps.rs/repo/github/ratatui-org/ratatui
[Discord Badge]: [Discord Badge]: https://img.shields.io/discord/1070692720437383208?label=discord&logo=discord&style=flat-square&color=1370D3&logoColor=1370D3
https://img.shields.io/discord/1070692720437383208?label=discord&logo=discord&style=flat-square&color=1370D3&logoColor=1370D3
[Discord Server]: https://discord.gg/pMCEU9hNEj [Discord Server]: https://discord.gg/pMCEU9hNEj
[Docs Badge]: https://img.shields.io/docsrs/ratatui?logo=rust&style=flat-square&logoColor=E05D44 [Docs Badge]: https://img.shields.io/docsrs/ratatui?logo=rust&style=flat-square&logoColor=E05D44
[Matrix Badge]: [Matrix Badge]: https://img.shields.io/matrix/ratatui-general%3Amatrix.org?style=flat-square&logo=matrix&label=Matrix&color=C43AC3
https://img.shields.io/matrix/ratatui-general%3Amatrix.org?style=flat-square&logo=matrix&label=Matrix&color=C43AC3
[Matrix]: https://matrix.to/#/#ratatui:matrix.org [Matrix]: https://matrix.to/#/#ratatui:matrix.org
[Sponsors Badge]: https://img.shields.io/github/sponsors/ratatui-org?logo=github&style=flat-square&color=1370D3 [Sponsors Badge]: https://img.shields.io/github/sponsors/ratatui-org?logo=github&style=flat-square&color=1370D3

View file

@ -19,12 +19,12 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Bar, BarChart, BarGroup, Block, Paragraph}, widgets::{Bar, BarChart, BarGroup, Block, Paragraph},
}; };

View file

@ -20,13 +20,13 @@ use std::{
time::Duration, time::Duration,
}; };
use crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{ widgets::{
block::{Position, Title}, block::{Position, Title},

View file

@ -17,12 +17,15 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode}, crossterm::{
execute, event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
widgets::calendar::*,
}; };
use ratatui::{prelude::*, widgets::calendar::*};
use time::{Date, Month, OffsetDateTime}; use time::{Date, Month, OffsetDateTime};
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {

View file

@ -20,12 +20,12 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*, prelude::*,
widgets::{canvas::*, *}, widgets::{canvas::*, *},
}; };

View file

@ -19,12 +19,12 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{block::Title, Axis, Block, Chart, Dataset, GraphType, LegendPosition}, widgets::{block::Title, Axis, Block, Chart, Dataset, GraphType, LegendPosition},
}; };

View file

@ -23,13 +23,13 @@ use std::{
time::Duration, time::Duration,
}; };
use crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, Borders, Paragraph}, widgets::{Block, Borders, Paragraph},
}; };

View file

@ -33,13 +33,15 @@ use std::{
}; };
use color_eyre::{config::HookBuilder, eyre, Result}; use color_eyre::{config::HookBuilder, eyre, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use palette::{convert::FromColorUnclamped, Okhsv, Srgb}; use palette::{convert::FromColorUnclamped, Okhsv, Srgb};
use ratatui::prelude::*; use ratatui::{
crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*,
};
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct App { struct App {

View file

@ -18,13 +18,13 @@
use std::io::{self, stdout}; use std::io::{self, stdout};
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
layout::{Constraint::*, Flex}, layout::{Constraint::*, Flex},
prelude::*, prelude::*,
style::palette::tailwind::*, style::palette::tailwind::*,

View file

@ -18,12 +18,17 @@
use std::io::{self, stdout}; use std::io::{self, stdout};
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode, KeyEventKind}, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event, KeyCode, KeyEventKind},
ExecutableCommand, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
layout::Constraint::*,
prelude::*,
style::palette::tailwind,
widgets::*,
}; };
use ratatui::{layout::Constraint::*, prelude::*, style::palette::tailwind, widgets::*};
use strum::{Display, EnumIter, FromRepr, IntoEnumIterator}; use strum::{Display, EnumIter, FromRepr, IntoEnumIterator};
const SPACER_HEIGHT: u16 = 0; const SPACER_HEIGHT: u16 = 0;

View file

@ -15,15 +15,18 @@
use std::{error::Error, io, ops::ControlFlow, time::Duration}; use std::{error::Error, io, ops::ControlFlow, time::Duration};
use crossterm::{ use ratatui::{
event::{ crossterm::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, MouseButton, MouseEvent, event::{
MouseEventKind, self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, MouseButton, MouseEvent,
MouseEventKind,
},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}, },
execute, prelude::*,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, widgets::Paragraph,
}; };
use ratatui::{prelude::*, widgets::Paragraph};
/// A custom widget that renders a button with a label, theme and state. /// A custom widget that renders a button with a label, theme and state.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -4,12 +4,14 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{ use ratatui::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, crossterm::{
execute, event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
}; };
use ratatui::prelude::*;
use crate::{app::App, ui}; use crate::{app::App, ui};

View file

@ -1,11 +1,13 @@
use std::{error::Error, io, sync::mpsc, thread, time::Duration}; use std::{error::Error, io, sync::mpsc, thread, time::Duration};
use ratatui::prelude::*; use ratatui::{
use termion::{ prelude::*,
event::Key, termion::{
input::{MouseTerminal, TermRead}, event::Key,
raw::IntoRawMode, input::{MouseTerminal, TermRead},
screen::IntoAlternateScreen, raw::IntoRawMode,
screen::IntoAlternateScreen,
},
}; };
use crate::{app::App, ui}; use crate::{app::App, ui};

View file

@ -3,10 +3,12 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use ratatui::prelude::*; use ratatui::{
use termwiz::{ prelude::*,
input::{InputEvent, KeyCode}, termwiz::{
terminal::Terminal as TermwizTerminal, input::{InputEvent, KeyCode},
terminal::Terminal as TermwizTerminal,
},
}; };
use crate::{app::App, ui}; use crate::{app::App, ui};

View file

@ -1,9 +1,12 @@
use std::time::Duration; use std::time::Duration;
use color_eyre::{eyre::Context, Result}; use color_eyre::{eyre::Context, Result};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{prelude::*, widgets::*}; use ratatui::{
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind},
prelude::*,
widgets::*,
};
use strum::{Display, EnumIter, FromRepr, IntoEnumIterator}; use strum::{Display, EnumIter, FromRepr, IntoEnumIterator};
use crate::{destroy, tabs::*, term, THEME}; use crate::{destroy, tabs::*, term, THEME};

View file

@ -4,12 +4,14 @@ use std::{
}; };
use color_eyre::{eyre::WrapErr, Result}; use color_eyre::{eyre::WrapErr, Result};
use crossterm::{ use ratatui::{
event::{self, Event}, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event},
ExecutableCommand, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*,
}; };
use ratatui::prelude::*;
pub fn init() -> Result<Terminal<impl Backend>> { pub fn init() -> Result<Terminal<impl Backend>> {
// this size is to match the size of the terminal when running the demo // this size is to match the size of the terminal when running the demo

View file

@ -15,11 +15,12 @@
use std::io::{self, stdout}; use std::io::{self, stdout};
use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*, prelude::*,
widgets::{Block, Borders, Paragraph}, widgets::{Block, Borders, Paragraph},
}; };
@ -56,7 +57,6 @@ fn hello_world(frame: &mut Frame) {
); );
} }
use crossterm::event::{self, Event, KeyCode};
fn handle_events() -> io::Result<bool> { fn handle_events() -> io::Result<bool> {
if event::poll(std::time::Duration::from_millis(50))? { if event::poll(std::time::Duration::from_millis(50))? {
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {

View file

@ -18,12 +18,12 @@
use std::io::{self, stdout}; use std::io::{self, stdout};
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
layout::{Constraint::*, Flex}, layout::{Constraint::*, Flex},
prelude::*, prelude::*,
style::palette::tailwind, style::palette::tailwind,

View file

@ -18,12 +18,12 @@
use std::{io::stdout, time::Duration}; use std::{io::stdout, time::Duration};
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*, prelude::*,
style::palette::tailwind, style::palette::tailwind,
widgets::{block::Title, Block, Borders, Gauge, Padding, Paragraph}, widgets::{block::Title, Block, Borders, Gauge, Padding, Paragraph},

View file

@ -19,12 +19,15 @@ use std::{
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode}, crossterm::{
execute, event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
widgets::Paragraph,
}; };
use ratatui::{prelude::*, widgets::Paragraph};
/// This is a bare minimum example. There are many approaches to running an application loop, so /// This is a bare minimum example. There are many approaches to running an application loop, so
/// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and /// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and

View file

@ -17,13 +17,13 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
layout::Constraint::*, layout::Constraint::*,
prelude::*, prelude::*,
widgets::{Block, Paragraph}, widgets::{Block, Paragraph},

View file

@ -16,12 +16,12 @@
use std::{io::stdout, time::Duration}; use std::{io::stdout, time::Duration};
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*, prelude::*,
style::palette::tailwind, style::palette::tailwind,
widgets::{block::Title, *}, widgets::{block::Title, *},

View file

@ -18,12 +18,16 @@
use std::{error::Error, io, io::stdout}; use std::{error::Error, io, io::stdout};
use color_eyre::config::HookBuilder; use color_eyre::config::HookBuilder;
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode, KeyEventKind}, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event, KeyCode, KeyEventKind},
ExecutableCommand, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*,
style::palette::tailwind,
widgets::*,
}; };
use ratatui::{prelude::*, style::palette::tailwind, widgets::*};
const TODO_HEADER_BG: Color = tailwind::BLUE.c950; const TODO_HEADER_BG: Color = tailwind::BLUE.c950;
const NORMAL_ROW_COLOR: Color = tailwind::SLATE.c950; const NORMAL_ROW_COLOR: Color = tailwind::SLATE.c950;

View file

@ -13,12 +13,16 @@
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples //! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md //! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode, KeyEventKind}, backend::CrosstermBackend,
execute, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
text::Text,
Terminal,
}; };
use ratatui::{backend::CrosstermBackend, text::Text, Terminal};
/// This is a bare minimum example. There are many approaches to running an application loop, so /// This is a bare minimum example. There are many approaches to running an application loop, so
/// this is not meant to be prescriptive. See the [examples] folder for more complete examples. /// this is not meant to be prescriptive. See the [examples] folder for more complete examples.

View file

@ -25,13 +25,16 @@ use std::{
time::Duration, time::Duration,
}; };
use crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{prelude::*, widgets::Paragraph}; use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
widgets::Paragraph,
};
type Result<T> = result::Result<T, Box<dyn Error>>; type Result<T> = result::Result<T, Box<dyn Error>>;

View file

@ -31,11 +31,11 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, Paragraph}, widgets::{Block, Paragraph},
}; };

View file

@ -19,12 +19,12 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, Paragraph, Wrap}, widgets::{Block, Paragraph, Wrap},
}; };

View file

@ -18,12 +18,12 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, Clear, Paragraph, Wrap}, widgets::{Block, Clear, Paragraph, Wrap},
}; };

View file

@ -19,10 +19,13 @@ use std::{
time::Duration, time::Duration,
}; };
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use indoc::indoc; use indoc::indoc;
use itertools::izip; use itertools::izip;
use ratatui::{prelude::*, widgets::Paragraph}; use ratatui::{
crossterm::terminal::{disable_raw_mode, enable_raw_mode},
prelude::*,
widgets::Paragraph,
};
/// A fun example of using half block characters to draw a logo /// A fun example of using half block characters to draw a logo
#[allow(clippy::many_single_char_names)] #[allow(clippy::many_single_char_names)]

View file

@ -22,12 +22,16 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{ use ratatui::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, crossterm::{
execute, event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
symbols::scrollbar,
widgets::*,
}; };
use ratatui::{prelude::*, symbols::scrollbar, widgets::*};
#[derive(Default)] #[derive(Default)]
struct App { struct App {

View file

@ -19,16 +19,16 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use rand::{ use rand::{
distributions::{Distribution, Uniform}, distributions::{Distribution, Uniform},
rngs::ThreadRng, rngs::ThreadRng,
}; };
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, Borders, Sparkline}, widgets::{Block, Borders, Sparkline},
}; };

View file

@ -17,13 +17,16 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use itertools::Itertools; use itertools::Itertools;
use ratatui::{prelude::*, widgets::*}; use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*,
widgets::*,
};
use style::palette::tailwind; use style::palette::tailwind;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;

View file

@ -18,12 +18,16 @@
use std::io::stdout; use std::io::stdout;
use color_eyre::{config::HookBuilder, Result}; use color_eyre::{config::HookBuilder, Result};
use crossterm::{ use ratatui::{
event::{self, Event, KeyCode, KeyEventKind}, crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, event::{self, Event, KeyCode, KeyEventKind},
ExecutableCommand, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
prelude::*,
style::palette::tailwind,
widgets::*,
}; };
use ratatui::{prelude::*, style::palette::tailwind, widgets::*};
use strum::{Display, EnumIter, FromRepr, IntoEnumIterator}; use strum::{Display, EnumIter, FromRepr, IntoEnumIterator};
#[derive(Default)] #[derive(Default)]

View file

@ -29,12 +29,12 @@
use std::{error::Error, io}; use std::{error::Error, io};
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{ use ratatui::{
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
prelude::*, prelude::*,
widgets::{Block, List, ListItem, Paragraph}, widgets::{Block, List, ListItem, Paragraph},
}; };

View file

@ -6,19 +6,19 @@ use std::io::{self, Write};
#[cfg(feature = "underline-color")] #[cfg(feature = "underline-color")]
use crossterm::style::SetUnderlineColor; use crossterm::style::SetUnderlineColor;
use crossterm::{
cursor::{Hide, MoveTo, Show},
execute, queue,
style::{
Attribute as CAttribute, Attributes as CAttributes, Color as CColor, Colors, ContentStyle,
Print, SetAttribute, SetBackgroundColor, SetColors, SetForegroundColor,
},
terminal::{self, Clear},
};
use crate::{ use crate::{
backend::{Backend, ClearType, WindowSize}, backend::{Backend, ClearType, WindowSize},
buffer::Cell, buffer::Cell,
crossterm::{
cursor::{Hide, MoveTo, Show},
execute, queue,
style::{
Attribute as CAttribute, Attributes as CAttributes, Color as CColor, Colors,
ContentStyle, Print, SetAttribute, SetBackgroundColor, SetColors, SetForegroundColor,
},
terminal::{self, Clear},
},
layout::Size, layout::Size,
prelude::Rect, prelude::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
@ -45,11 +45,15 @@ use crate::{
/// ```rust,no_run /// ```rust,no_run
/// use std::io::{stderr, stdout}; /// use std::io::{stderr, stdout};
/// ///
/// use crossterm::{ /// use ratatui::{
/// terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, /// crossterm::{
/// ExecutableCommand, /// terminal::{
/// disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
/// },
/// ExecutableCommand,
/// },
/// prelude::*,
/// }; /// };
/// use ratatui::prelude::*;
/// ///
/// let mut backend = CrosstermBackend::new(stdout()); /// let mut backend = CrosstermBackend::new(stdout());
/// // or /// // or

View file

@ -9,13 +9,12 @@ use std::{
io::{self, Write}, io::{self, Write},
}; };
use termion::{color as tcolor, style as tstyle};
use crate::{ use crate::{
backend::{Backend, ClearType, WindowSize}, backend::{Backend, ClearType, WindowSize},
buffer::Cell, buffer::Cell,
prelude::Rect, prelude::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
termion::{self, color as tcolor, color::Color as _, style as tstyle},
}; };
/// A [`Backend`] implementation that uses [Termion] to render to the terminal. /// A [`Backend`] implementation that uses [Termion] to render to the terminal.
@ -40,8 +39,10 @@ use crate::{
/// ```rust,no_run /// ```rust,no_run
/// use std::io::{stderr, stdout}; /// use std::io::{stderr, stdout};
/// ///
/// use ratatui::prelude::*; /// use ratatui::{
/// use termion::{raw::IntoRawMode, screen::IntoAlternateScreen}; /// prelude::*,
/// termion::{raw::IntoRawMode, screen::IntoAlternateScreen},
/// };
/// ///
/// let writer = stdout().into_raw_mode()?.into_alternate_screen()?; /// let writer = stdout().into_raw_mode()?.into_alternate_screen()?;
/// let mut backend = TermionBackend::new(writer); /// let mut backend = TermionBackend::new(writer);
@ -243,7 +244,6 @@ struct ModifierDiff {
impl fmt::Display for Fg { impl fmt::Display for Fg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use termion::color::Color as TermionColor;
match self.0 { match self.0 {
Color::Reset => termion::color::Reset.write_fg(f), Color::Reset => termion::color::Reset.write_fg(f),
Color::Black => termion::color::Black.write_fg(f), Color::Black => termion::color::Black.write_fg(f),
@ -269,7 +269,6 @@ impl fmt::Display for Fg {
} }
impl fmt::Display for Bg { impl fmt::Display for Bg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use termion::color::Color as TermionColor;
match self.0 { match self.0 {
Color::Reset => termion::color::Reset.write_bg(f), Color::Reset => termion::color::Reset.write_bg(f),
Color::Black => termion::color::Black.write_bg(f), Color::Black => termion::color::Black.write_bg(f),

View file

@ -7,20 +7,19 @@
use std::{error::Error, io}; use std::{error::Error, io};
use termwiz::{
caps::Capabilities,
cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline},
color::{AnsiColor, ColorAttribute, ColorSpec, LinearRgba, RgbColor, SrgbaTuple},
surface::{Change, CursorVisibility, Position},
terminal::{buffered::BufferedTerminal, ScreenSize, SystemTerminal, Terminal},
};
use crate::{ use crate::{
backend::{Backend, WindowSize}, backend::{Backend, WindowSize},
buffer::Cell, buffer::Cell,
layout::Size, layout::Size,
prelude::Rect, prelude::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
termwiz::{
caps::Capabilities,
cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline},
color::{AnsiColor, ColorAttribute, ColorSpec, LinearRgba, RgbColor, SrgbaTuple},
surface::{Change, CursorVisibility, Position},
terminal::{buffered::BufferedTerminal, ScreenSize, SystemTerminal, Terminal},
},
}; };
/// A [`Backend`] implementation that uses [Termwiz] to render to the terminal. /// A [`Backend`] implementation that uses [Termwiz] to render to the terminal.

View file

@ -20,10 +20,10 @@
//! //!
//! ## Installation //! ## Installation
//! //!
//! Add `ratatui` and `crossterm` as dependencies to your cargo.toml: //! Add `ratatui` as a dependency to your cargo.toml:
//! //!
//! ```shell //! ```shell
//! cargo add ratatui crossterm //! cargo add ratatui
//! ``` //! ```
//! //!
//! Ratatui uses [Crossterm] by default as it works on most platforms. See the [Installation] //! Ratatui uses [Crossterm] by default as it works on most platforms. See the [Installation]
@ -103,12 +103,17 @@
//! ```rust,no_run //! ```rust,no_run
//! use std::io::{self, stdout}; //! use std::io::{self, stdout};
//! //!
//! use crossterm::{ //! use ratatui::{
//! event::{self, Event, KeyCode}, //! crossterm::{
//! terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, //! event::{self, Event, KeyCode},
//! ExecutableCommand, //! terminal::{
//! disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
//! },
//! ExecutableCommand,
//! },
//! prelude::*,
//! widgets::*,
//! }; //! };
//! use ratatui::{prelude::*, widgets::*};
//! //!
//! fn main() -> io::Result<()> { //! fn main() -> io::Result<()> {
//! enable_raw_mode()?; //! enable_raw_mode()?;
@ -300,24 +305,20 @@
//! [Termwiz]: https://crates.io/crates/termwiz //! [Termwiz]: https://crates.io/crates/termwiz
//! [tui-rs]: https://crates.io/crates/tui //! [tui-rs]: https://crates.io/crates/tui
//! [GitHub Sponsors]: https://github.com/sponsors/ratatui-org //! [GitHub Sponsors]: https://github.com/sponsors/ratatui-org
//! [Crate Badge]: https://img.shields.io/crates/v/ratatui?logo=rust&style=flat-square //! [Crate Badge]: https://img.shields.io/crates/v/ratatui?logo=rust&style=flat-square&logoColor=E05D44&color=E05D44
//! [License Badge]: https://img.shields.io/crates/l/ratatui?style=flat-square //! [License Badge]: https://img.shields.io/crates/l/ratatui?style=flat-square&color=1370D3
//! [CI Badge]: //! [CI Badge]: https://img.shields.io/github/actions/workflow/status/ratatui-org/ratatui/ci.yml?style=flat-square&logo=github
//! https://img.shields.io/github/actions/workflow/status/ratatui-org/ratatui/ci.yml?style=flat-square&logo=github
//! [CI Workflow]: https://github.com/ratatui-org/ratatui/actions/workflows/ci.yml //! [CI Workflow]: https://github.com/ratatui-org/ratatui/actions/workflows/ci.yml
//! [Codecov Badge]: //! [Codecov Badge]: https://img.shields.io/codecov/c/github/ratatui-org/ratatui?logo=codecov&style=flat-square&token=BAQ8SOKEST&color=C43AC3&logoColor=C43AC3
//! https://img.shields.io/codecov/c/github/ratatui-org/ratatui?logo=codecov&style=flat-square&token=BAQ8SOKEST
//! [Codecov]: https://app.codecov.io/gh/ratatui-org/ratatui //! [Codecov]: https://app.codecov.io/gh/ratatui-org/ratatui
//! [Deps.rs Badge]: https://deps.rs/repo/github/ratatui-org/ratatui/status.svg?style=flat-square //! [Deps.rs Badge]: https://deps.rs/repo/github/ratatui-org/ratatui/status.svg?style=flat-square
//! [Deps.rs]: https://deps.rs/repo/github/ratatui-org/ratatui //! [Deps.rs]: https://deps.rs/repo/github/ratatui-org/ratatui
//! [Discord Badge]: //! [Discord Badge]: https://img.shields.io/discord/1070692720437383208?label=discord&logo=discord&style=flat-square&color=1370D3&logoColor=1370D3
//! https://img.shields.io/discord/1070692720437383208?label=discord&logo=discord&style=flat-square
//! [Discord Server]: https://discord.gg/pMCEU9hNEj //! [Discord Server]: https://discord.gg/pMCEU9hNEj
//! [Docs Badge]: https://img.shields.io/docsrs/ratatui?logo=rust&style=flat-square //! [Docs Badge]: https://img.shields.io/docsrs/ratatui?logo=rust&style=flat-square&logoColor=E05D44
//! [Matrix Badge]: //! [Matrix Badge]: https://img.shields.io/matrix/ratatui-general%3Amatrix.org?style=flat-square&logo=matrix&label=Matrix&color=C43AC3
//! https://img.shields.io/matrix/ratatui-general%3Amatrix.org?style=flat-square&logo=matrix&label=Matrix
//! [Matrix]: https://matrix.to/#/#ratatui:matrix.org //! [Matrix]: https://matrix.to/#/#ratatui:matrix.org
//! [Sponsors Badge]: https://img.shields.io/github/sponsors/ratatui-org?logo=github&style=flat-square //! [Sponsors Badge]: https://img.shields.io/github/sponsors/ratatui-org?logo=github&style=flat-square&color=1370D3
// show the feature flags in the generated documentation // show the feature flags in the generated documentation
#![cfg_attr(docsrs, feature(doc_auto_cfg))] #![cfg_attr(docsrs, feature(doc_auto_cfg))]
@ -339,3 +340,13 @@ pub mod widgets;
pub use self::terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, Viewport}; pub use self::terminal::{CompletedFrame, Frame, Terminal, TerminalOptions, Viewport};
pub mod prelude; pub mod prelude;
/// re-export the `crossterm` crate so that users don't have to add it as a dependency
#[cfg(feature = "crossterm")]
pub use crossterm;
/// re-export the `termion` crate so that users don't have to add it as a dependency
#[cfg(feature = "termion")]
pub use termion;
/// re-export the `termwiz` crate so that users don't have to add it as a dependency
#[cfg(feature = "termwiz")]
pub use termwiz;

View file

@ -30,7 +30,7 @@ fn backend_termion_should_only_write_diffs() -> Result<(), Box<dyn std::error::E
} }
let expected = { let expected = {
use termion::{color, cursor, style}; use ratatui::termion::{color, cursor, style};
let mut s = String::new(); let mut s = String::new();
// First draw // First draw
write!(s, "{}", cursor::Goto(1, 1))?; write!(s, "{}", cursor::Goto(1, 1))?;