2017-12-26 11:03:25 +00:00
|
|
|
//! [tui](https://github.com/fdehau/tui-rs) is a library used to build rich
|
|
|
|
//! terminal users interfaces and dashboards.
|
|
|
|
//!
|
2018-09-09 06:55:51 +00:00
|
|
|
//! ![](https://raw.githubusercontent.com/fdehau/tui-rs/master/assets/demo.gif)
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
|
|
|
//! # Get started
|
|
|
|
//!
|
2019-03-24 20:37:55 +00:00
|
|
|
//! ## Adding `tui` as a dependency
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies]
|
2020-11-14 16:30:11 +00:00
|
|
|
//! tui = "0.13"
|
2019-03-24 20:37:55 +00:00
|
|
|
//! termion = "1.5"
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-12-13 19:19:59 +00:00
|
|
|
//! The crate is using the `termion` backend by default but if for example you want your
|
|
|
|
//! application to work on Windows, you might want to use the `crossterm` backend instead. This can
|
|
|
|
//! be done by changing your dependencies specification to the following:
|
2019-03-24 20:37:55 +00:00
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies]
|
2020-09-29 10:13:25 +00:00
|
|
|
//! crossterm = "0.18"
|
2020-11-14 16:30:11 +00:00
|
|
|
//! tui = { version = "0.13", default-features = false, features = ['crossterm'] }
|
2019-03-24 20:37:55 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The same logic applies for all other available backends.
|
|
|
|
//!
|
2017-12-26 11:03:25 +00:00
|
|
|
//! ## Creating a `Terminal`
|
|
|
|
//!
|
2019-03-24 20:37:55 +00:00
|
|
|
//! Every application using `tui` should start by instantiating a `Terminal`. It is a light
|
|
|
|
//! abstraction over available backends that provides basic functionalities such as clearing the
|
|
|
|
//! screen, hiding the cursor, etc.
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use std::io;
|
2017-12-26 11:03:25 +00:00
|
|
|
//! use tui::Terminal;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use tui::backend::TermionBackend;
|
|
|
|
//! use termion::raw::IntoRawMode;
|
|
|
|
//!
|
|
|
|
//! fn main() -> Result<(), io::Error> {
|
|
|
|
//! let stdout = io::stdout().into_raw_mode()?;
|
|
|
|
//! let backend = TermionBackend::new(stdout);
|
|
|
|
//! let mut terminal = Terminal::new(backend)?;
|
|
|
|
//! Ok(())
|
2017-12-26 11:03:25 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-04-16 16:30:25 +00:00
|
|
|
//! If you had previously chosen `crossterm` as a backend, the terminal can be created in a similar
|
2019-03-24 20:37:55 +00:00
|
|
|
//! way:
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
2020-04-16 16:30:25 +00:00
|
|
|
//! use std::io;
|
2017-12-26 11:03:25 +00:00
|
|
|
//! use tui::Terminal;
|
2020-04-16 16:30:25 +00:00
|
|
|
//! use tui::backend::CrosstermBackend;
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
2018-09-23 18:55:50 +00:00
|
|
|
//! fn main() -> Result<(), io::Error> {
|
2020-04-16 16:30:25 +00:00
|
|
|
//! let stdout = io::stdout();
|
|
|
|
//! let backend = CrosstermBackend::new(stdout);
|
|
|
|
//! let mut terminal = Terminal::new(backend)?;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! Ok(())
|
2017-12-26 11:03:25 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-03-24 20:37:55 +00:00
|
|
|
//! You may also refer to the examples to find out how to create a `Terminal` for each available
|
|
|
|
//! backend.
|
|
|
|
//!
|
2017-12-26 11:03:25 +00:00
|
|
|
//! ## Building a User Interface (UI)
|
|
|
|
//!
|
2020-04-16 16:30:25 +00:00
|
|
|
//! Every component of your interface will be implementing the `Widget` trait. The library comes
|
2020-03-16 09:41:00 +00:00
|
|
|
//! with a predefined set of widgets that should meet most of your use cases. You are also free to
|
|
|
|
//! implement your own.
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
2019-03-24 20:37:55 +00:00
|
|
|
//! Each widget follows a builder pattern API providing a default configuration along with methods
|
2020-04-16 16:30:25 +00:00
|
|
|
//! to customize them. The widget is then rendered using the [`Frame::render_widget`] which take
|
|
|
|
//! your widget instance an area to draw to.
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
|
|
|
//! The following example renders a block of the size of the terminal:
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
|
|
|
//! use std::io;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use termion::raw::IntoRawMode;
|
2017-12-26 11:03:25 +00:00
|
|
|
//! use tui::Terminal;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use tui::backend::TermionBackend;
|
2017-12-26 16:09:04 +00:00
|
|
|
//! use tui::widgets::{Widget, Block, Borders};
|
2018-08-12 17:44:52 +00:00
|
|
|
//! use tui::layout::{Layout, Constraint, Direction};
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
2018-09-23 18:55:50 +00:00
|
|
|
//! fn main() -> Result<(), io::Error> {
|
|
|
|
//! let stdout = io::stdout().into_raw_mode()?;
|
|
|
|
//! let backend = TermionBackend::new(stdout);
|
|
|
|
//! let mut terminal = Terminal::new(backend)?;
|
2020-06-15 20:57:23 +00:00
|
|
|
//! terminal.draw(|f| {
|
2018-12-07 20:20:27 +00:00
|
|
|
//! let size = f.size();
|
2019-12-15 20:38:18 +00:00
|
|
|
//! let block = Block::default()
|
2018-08-12 17:44:52 +00:00
|
|
|
//! .title("Block")
|
2019-12-15 20:38:18 +00:00
|
|
|
//! .borders(Borders::ALL);
|
|
|
|
//! f.render_widget(block, size);
|
2018-09-01 12:05:33 +00:00
|
|
|
//! })
|
2017-12-26 11:03:25 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Layout
|
|
|
|
//!
|
2018-08-12 17:44:52 +00:00
|
|
|
//! The library comes with a basic yet useful layout management object called `Layout`. As you may
|
|
|
|
//! see below and in the examples, the library makes heavy use of the builder pattern to provide
|
|
|
|
//! full customization. And `Layout` is no exception:
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
|
|
|
//! use std::io;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use termion::raw::IntoRawMode;
|
2017-12-26 11:03:25 +00:00
|
|
|
//! use tui::Terminal;
|
2018-09-23 18:55:50 +00:00
|
|
|
//! use tui::backend::TermionBackend;
|
2017-12-26 16:09:04 +00:00
|
|
|
//! use tui::widgets::{Widget, Block, Borders};
|
2018-08-12 17:44:52 +00:00
|
|
|
//! use tui::layout::{Layout, Constraint, Direction};
|
2017-12-26 11:03:25 +00:00
|
|
|
//!
|
2018-09-23 18:55:50 +00:00
|
|
|
//! fn main() -> Result<(), io::Error> {
|
|
|
|
//! let stdout = io::stdout().into_raw_mode()?;
|
|
|
|
//! let backend = TermionBackend::new(stdout);
|
|
|
|
//! let mut terminal = Terminal::new(backend)?;
|
2020-06-15 20:57:23 +00:00
|
|
|
//! terminal.draw(|f| {
|
2018-09-01 12:05:33 +00:00
|
|
|
//! let chunks = Layout::default()
|
|
|
|
//! .direction(Direction::Vertical)
|
|
|
|
//! .margin(1)
|
|
|
|
//! .constraints(
|
|
|
|
//! [
|
|
|
|
//! Constraint::Percentage(10),
|
|
|
|
//! Constraint::Percentage(80),
|
|
|
|
//! Constraint::Percentage(10)
|
|
|
|
//! ].as_ref()
|
|
|
|
//! )
|
2018-12-07 20:20:27 +00:00
|
|
|
//! .split(f.size());
|
2019-12-15 20:38:18 +00:00
|
|
|
//! let block = Block::default()
|
2018-09-01 12:05:33 +00:00
|
|
|
//! .title("Block")
|
2019-12-15 20:38:18 +00:00
|
|
|
//! .borders(Borders::ALL);
|
|
|
|
//! f.render_widget(block, chunks[0]);
|
|
|
|
//! let block = Block::default()
|
2018-09-01 12:05:33 +00:00
|
|
|
//! .title("Block 2")
|
2019-12-15 20:38:18 +00:00
|
|
|
//! .borders(Borders::ALL);
|
|
|
|
//! f.render_widget(block, chunks[1]);
|
2018-09-01 12:05:33 +00:00
|
|
|
//! })
|
2017-12-26 11:03:25 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-03-24 20:37:55 +00:00
|
|
|
//! This let you describe responsive terminal UI by nesting layouts. You should note that by
|
|
|
|
//! default the computed layout tries to fill the available space completely. So if for any reason
|
|
|
|
//! you might need a blank space somewhere, try to pass an additional constraint and don't use the
|
|
|
|
//! corresponding area.
|
2017-12-26 11:03:25 +00:00
|
|
|
|
2018-05-06 10:59:24 +00:00
|
|
|
pub mod backend;
|
2016-11-02 18:16:53 +00:00
|
|
|
pub mod buffer;
|
2018-05-06 10:59:24 +00:00
|
|
|
pub mod layout;
|
|
|
|
pub mod style;
|
2016-10-12 14:12:42 +00:00
|
|
|
pub mod symbols;
|
2016-10-09 17:46:53 +00:00
|
|
|
pub mod terminal;
|
2020-05-10 13:44:30 +00:00
|
|
|
pub mod text;
|
2016-10-09 17:46:53 +00:00
|
|
|
pub mod widgets;
|
|
|
|
|
2020-08-02 18:40:51 +00:00
|
|
|
pub use self::terminal::{Frame, Terminal, TerminalOptions, Viewport};
|