mirror of
https://github.com/ratatui/ratatui.git
synced 2025-10-02 15:25:54 +00:00
Fix examples
This commit is contained in:
parent
224eb2d8e0
commit
662e2dd102
2
Makefile
2
Makefile
@ -8,7 +8,7 @@ watch:
|
|||||||
watchman-make -p 'src/**/*.rs' -t build -p 'test/**/*.rs' -t test
|
watchman-make -p 'src/**/*.rs' -t build -p 'test/**/*.rs' -t test
|
||||||
|
|
||||||
watch-test:
|
watch-test:
|
||||||
watchman-make -p 'src/**/*.rs' 'tests/**/*.rs' -t test
|
watchman-make -p 'src/**/*.rs' 'tests/**/*.rs' 'examples/**/*.rs' -t test
|
||||||
|
|
||||||
watch-doc:
|
watch-doc:
|
||||||
watchman-make -p 'src/**/*.rs' -t doc
|
watchman-make -p 'src/**/*.rs' -t doc
|
||||||
|
@ -8,7 +8,7 @@ use termion::input::TermRead;
|
|||||||
use tui::{Terminal, TermionBackend};
|
use tui::{Terminal, TermionBackend};
|
||||||
use tui::widgets::{Widget, Block, border};
|
use tui::widgets::{Widget, Block, border};
|
||||||
use tui::layout::{Group, Direction, Size};
|
use tui::layout::{Group, Direction, Size};
|
||||||
use tui::style::Color;
|
use tui::style::{Style, Color};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut terminal = Terminal::new(TermionBackend::new().unwrap()).unwrap();
|
let mut terminal = Terminal::new(TermionBackend::new().unwrap()).unwrap();
|
||||||
@ -36,31 +36,33 @@ fn draw(t: &mut Terminal<TermionBackend>) {
|
|||||||
.render(t, &size, |t, chunks| {
|
.render(t, &size, |t, chunks| {
|
||||||
Block::default()
|
Block::default()
|
||||||
.title("Top")
|
.title("Top")
|
||||||
.title_color(Color::Magenta)
|
.title_style(Style::default().fg(Color::Magenta))
|
||||||
.background_color(Color::White)
|
.border_style(Style::default().fg(Color::Magenta))
|
||||||
.border_color(Color::Magenta)
|
|
||||||
.borders(border::BOTTOM)
|
.borders(border::BOTTOM)
|
||||||
.render(t, &chunks[0]);
|
.render(t, &chunks[0]);
|
||||||
Group::default()
|
Group::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.sizes(&[Size::Fixed(7), Size::Min(0), Size::Fixed(7)])
|
.sizes(&[Size::Fixed(7), Size::Min(0), Size::Fixed(7)])
|
||||||
.render(t, &chunks[1], |t, chunks| {
|
.render(t, &chunks[1], |t, chunks| {
|
||||||
Block::default().title("Left").title_color(Color::Yellow).render(t, &chunks[0]);
|
Block::default()
|
||||||
|
.title("Left")
|
||||||
|
.title_style(Style::default().fg(Color::Yellow))
|
||||||
|
.render(t, &chunks[0]);
|
||||||
Block::default()
|
Block::default()
|
||||||
.title("Middle")
|
.title("Middle")
|
||||||
.title_color(Color::Cyan)
|
.title_style(Style::default().fg(Color::Cyan))
|
||||||
.border_color(Color::Cyan)
|
.border_style(Style::default().fg(Color::Cyan))
|
||||||
.borders(border::LEFT | border::RIGHT)
|
.borders(border::LEFT | border::RIGHT)
|
||||||
.render(t, &chunks[1]);
|
.render(t, &chunks[1]);
|
||||||
Block::default()
|
Block::default()
|
||||||
.title("Right")
|
.title("Right")
|
||||||
.title_color(Color::Green)
|
.title_style(Style::default().fg(Color::Green))
|
||||||
.render(t, &chunks[2]);
|
.render(t, &chunks[2]);
|
||||||
});
|
});
|
||||||
Block::default()
|
Block::default()
|
||||||
.title("Bottom")
|
.title("Bottom")
|
||||||
.title_color(Color::Red)
|
.title_style(Style::default().fg(Color::Green))
|
||||||
.border_color(Color::Red)
|
.border_style(Style::default().fg(Color::Green))
|
||||||
.borders(border::TOP)
|
.borders(border::TOP)
|
||||||
.render(t, &chunks[2]);
|
.render(t, &chunks[2]);
|
||||||
});
|
});
|
||||||
|
@ -4,7 +4,7 @@ use tui::{Terminal, TermionBackend};
|
|||||||
use tui::widgets::Widget;
|
use tui::widgets::Widget;
|
||||||
use tui::buffer::Buffer;
|
use tui::buffer::Buffer;
|
||||||
use tui::layout::Rect;
|
use tui::layout::Rect;
|
||||||
use tui::style::Color;
|
use tui::style::Style;
|
||||||
|
|
||||||
struct Label<'a> {
|
struct Label<'a> {
|
||||||
text: &'a str,
|
text: &'a str,
|
||||||
@ -18,11 +18,7 @@ impl<'a> Default for Label<'a> {
|
|||||||
|
|
||||||
impl<'a> Widget for Label<'a> {
|
impl<'a> Widget for Label<'a> {
|
||||||
fn draw(&self, area: &Rect, buf: &mut Buffer) {
|
fn draw(&self, area: &Rect, buf: &mut Buffer) {
|
||||||
buf.set_string(area.left(),
|
buf.set_string(area.left(), area.top(), self.text, &Style::default());
|
||||||
area.top(),
|
|
||||||
self.text,
|
|
||||||
Color::Reset,
|
|
||||||
Color::Reset);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,7 +469,12 @@ fn draw_main(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
|
|||||||
});
|
});
|
||||||
if app.show_chart {
|
if app.show_chart {
|
||||||
Chart::default()
|
Chart::default()
|
||||||
.block(Block::default().title("Chart").borders(border::ALL))
|
.block(Block::default()
|
||||||
|
.title("Chart")
|
||||||
|
.title_style(Style::default()
|
||||||
|
.fg(Color::Cyan)
|
||||||
|
.modifier(Modifier::Bold))
|
||||||
|
.borders(border::ALL))
|
||||||
.x_axis(Axis::default()
|
.x_axis(Axis::default()
|
||||||
.title("X Axis")
|
.title("X Axis")
|
||||||
.style(Style::default().fg(Color::Gray))
|
.style(Style::default().fg(Color::Gray))
|
||||||
|
@ -75,18 +75,20 @@ impl Default for Cell {
|
|||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// use tui::buffer::{Buffer, Cell};
|
/// use tui::buffer::{Buffer, Cell};
|
||||||
/// use tui::layout::Rect;
|
/// use tui::layout::Rect;
|
||||||
/// use tui::style::{Color, Style};
|
/// use tui::style::{Color, Style, Modifier};
|
||||||
///
|
///
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
|
/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
|
||||||
/// buf.get_mut(0, 2).set_symbol("x");
|
/// buf.get_mut(0, 2).set_symbol("x");
|
||||||
/// assert_eq!(buf.get(0, 2).symbol, "x");
|
/// assert_eq!(buf.get(0, 2).symbol, "x");
|
||||||
/// buf.set_string(3, 0, "string", Color::Red, Color::White);
|
/// buf.set_string(3, 0, "string", &Style::default().fg(Color::Red).bg(Color::White));
|
||||||
/// assert_eq!(buf.get(5, 0), &Cell{
|
/// assert_eq!(buf.get(5, 0), &Cell{
|
||||||
/// symbol: String::from("r"),
|
/// symbol: String::from("r"),
|
||||||
|
/// style: Style {
|
||||||
/// fg: Color::Red,
|
/// fg: Color::Red,
|
||||||
/// bg: Color::White,
|
/// bg: Color::White,
|
||||||
/// style: Style::Reset});
|
/// modifier: Modifier::Reset
|
||||||
|
/// }});
|
||||||
/// buf.get_mut(5, 0).set_char('x');
|
/// buf.get_mut(5, 0).set_char('x');
|
||||||
/// assert_eq!(buf.get(5, 0).symbol, "x");
|
/// assert_eq!(buf.get(5, 0).symbol, "x");
|
||||||
/// # }
|
/// # }
|
||||||
|
@ -15,16 +15,15 @@ use symbols::bar;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, BarChart};
|
/// # use tui::widgets::{Block, border, BarChart};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color, Modifier};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// BarChart::default()
|
/// BarChart::default()
|
||||||
/// .block(Block::default().title("BarChart").borders(border::ALL))
|
/// .block(Block::default().title("BarChart").borders(border::ALL))
|
||||||
/// .bar_width(3)
|
/// .bar_width(3)
|
||||||
/// .bar_gap(1)
|
/// .bar_gap(1)
|
||||||
/// .bar_color(Color::Yellow)
|
/// .style(Style::default().fg(Color::Yellow).bg(Color::Red))
|
||||||
/// .value_color(Color::Red)
|
/// .value_style(Style::default().fg(Color::Red).modifier(Modifier::Bold))
|
||||||
/// .label_color(Color::White)
|
/// .label_style(Style::default().fg(Color::White))
|
||||||
/// .background_color(Color::Black)
|
|
||||||
/// .data(&[("B0", 0), ("B1", 2), ("B2", 4), ("B3", 3)])
|
/// .data(&[("B0", 0), ("B1", 2), ("B2", 4), ("B3", 3)])
|
||||||
/// .max(4);
|
/// .max(4);
|
||||||
/// # }
|
/// # }
|
||||||
|
@ -12,14 +12,14 @@ use symbols::line;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border};
|
/// # use tui::widgets::{Block, border};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Block::default()
|
/// Block::default()
|
||||||
/// .title("Block")
|
/// .title("Block")
|
||||||
/// .title_color(Color::Red)
|
/// .title_style(Style::default().fg(Color::Red))
|
||||||
/// .borders(border::LEFT | border::RIGHT)
|
/// .borders(border::LEFT | border::RIGHT)
|
||||||
/// .border_color(Color::White)
|
/// .border_style(Style::default().fg(Color::White))
|
||||||
/// .background_color(Color::Black);
|
/// .style(Style::default().bg(Color::Black));
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
@ -159,29 +159,31 @@ impl Default for ChartLayout {
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, Chart, Axis, Dataset, Marker};
|
/// # use tui::widgets::{Block, border, Chart, Axis, Dataset, Marker};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Chart::default()
|
/// Chart::default()
|
||||||
/// .block(Block::default().title("Chart"))
|
/// .block(Block::default().title("Chart"))
|
||||||
/// .x_axis(Axis::default()
|
/// .x_axis(Axis::default()
|
||||||
/// .title("X Axis")
|
/// .title("X Axis")
|
||||||
/// .color(Color::Gray)
|
/// .title_style(Style::default().fg(Color::Red))
|
||||||
|
/// .style(Style::default().fg(Color::Gray))
|
||||||
/// .bounds([0.0, 10.0])
|
/// .bounds([0.0, 10.0])
|
||||||
/// .labels(&["0.0", "5.0", "10.0"]))
|
/// .labels(&["0.0", "5.0", "10.0"]))
|
||||||
/// .y_axis(Axis::default()
|
/// .y_axis(Axis::default()
|
||||||
/// .title("Y Axis")
|
/// .title("Y Axis")
|
||||||
/// .color(Color::Gray)
|
/// .title_style(Style::default().fg(Color::Red))
|
||||||
|
/// .style(Style::default().fg(Color::Gray))
|
||||||
/// .bounds([0.0, 10.0])
|
/// .bounds([0.0, 10.0])
|
||||||
/// .labels(&["0.0", "5.0", "10.0"]))
|
/// .labels(&["0.0", "5.0", "10.0"]))
|
||||||
/// .datasets(&[Dataset::default()
|
/// .datasets(&[Dataset::default()
|
||||||
/// .name("data1")
|
/// .name("data1")
|
||||||
/// .marker(Marker::Dot)
|
/// .marker(Marker::Dot)
|
||||||
/// .color(Color::Cyan)
|
/// .style(Style::default().fg(Color::Cyan))
|
||||||
/// .data(&[(0.0, 5.0), (1.0, 6.0), (1.5, 6.434)]),
|
/// .data(&[(0.0, 5.0), (1.0, 6.0), (1.5, 6.434)]),
|
||||||
/// Dataset::default()
|
/// Dataset::default()
|
||||||
/// .name("data2")
|
/// .name("data2")
|
||||||
/// .marker(Marker::Braille)
|
/// .marker(Marker::Braille)
|
||||||
/// .color(Color::Magenta)
|
/// .style(Style::default().fg(Color::Magenta))
|
||||||
/// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]);
|
/// .data(&[(4.0, 5.0), (5.0, 8.0), (7.66, 13.5)])]);
|
||||||
/// # }
|
/// # }
|
||||||
pub struct Chart<'a> {
|
pub struct Chart<'a> {
|
||||||
|
@ -12,12 +12,11 @@ use layout::Rect;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Widget, Gauge, Block, border};
|
/// # use tui::widgets::{Widget, Gauge, Block, border};
|
||||||
/// # use tui::style::{Style, Color, ;
|
/// # use tui::style::{Style, Color, Modifier};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Gauge::default()
|
/// Gauge::default()
|
||||||
/// .block(Block::default().borders(border::ALL).title("Progress"))
|
/// .block(Block::default().borders(border::ALL).title("Progress"))
|
||||||
/// .style(Style::default().fg(Color::White).bg(Color::Black).modifier(Modifier::Italic))
|
/// .style(Style::default().fg(Color::White).bg(Color::Black).modifier(Modifier::Italic))
|
||||||
/// .background_color(Color::Black)
|
|
||||||
/// .percent(20);
|
/// .percent(20);
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -14,14 +14,14 @@ use style::Style;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, List};
|
/// # use tui::widgets::{Block, border, List};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color, Modifier};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// List::default()
|
/// List::default()
|
||||||
/// .block(Block::default().title("List").borders(border::ALL))
|
/// .block(Block::default().title("List").borders(border::ALL))
|
||||||
/// .items(&["Item 1", "Item 2", "Item 3"])
|
/// .items(&["Item 1", "Item 2", "Item 3"])
|
||||||
/// .select(1)
|
/// .select(1)
|
||||||
/// .color(Color::White)
|
/// .style(Style::default().fg(Color::White))
|
||||||
/// .highlight_color(Color::Yellow)
|
/// .highlight_style(Style::default().modifier(Modifier::Italic))
|
||||||
/// .highlight_symbol(">>");
|
/// .highlight_symbol(">>");
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -14,12 +14,11 @@ use style::{Style, Color, Modifier};
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, Paragraph};
|
/// # use tui::widgets::{Block, border, Paragraph};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Paragraph::default()
|
/// Paragraph::default()
|
||||||
/// .block(Block::default().title("Paragraph").borders(border::ALL))
|
/// .block(Block::default().title("Paragraph").borders(border::ALL))
|
||||||
/// .color(Color::White)
|
/// .style(Style::default().fg(Color::White).bg(Color::Black))
|
||||||
/// .background_color(Color::Black)
|
|
||||||
/// .wrap(true)
|
/// .wrap(true)
|
||||||
/// .text("First line\nSecond line\n{red Colored text}.");
|
/// .text("First line\nSecond line\n{red Colored text}.");
|
||||||
/// # }
|
/// # }
|
||||||
|
@ -13,14 +13,13 @@ use symbols::bar;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, Sparkline};
|
/// # use tui::widgets::{Block, border, Sparkline};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Sparkline::default()
|
/// Sparkline::default()
|
||||||
/// .block(Block::default().title("Sparkline").borders(border::ALL))
|
/// .block(Block::default().title("Sparkline").borders(border::ALL))
|
||||||
/// .data(&[0, 2, 3, 4, 1, 4, 10])
|
/// .data(&[0, 2, 3, 4, 1, 4, 10])
|
||||||
/// .max(5)
|
/// .max(5)
|
||||||
/// .color(Color::Yellow)
|
/// .style(Style::default().fg(Color::Red).bg(Color::White));
|
||||||
/// .background_color(Color::Red);
|
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Sparkline<'a> {
|
pub struct Sparkline<'a> {
|
||||||
|
@ -14,19 +14,18 @@ use style::Style;
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use tui::widgets::{Block, border, Table};
|
/// # use tui::widgets::{Block, border, Table};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Table::default()
|
/// Table::default()
|
||||||
/// .block(Block::default().title("Table"))
|
/// .block(Block::default().title("Table"))
|
||||||
/// .header(&["Col1", "Col2", "Col3"])
|
/// .header(&["Col1", "Col2", "Col3"])
|
||||||
/// .header_color(Color::Yellow)
|
/// .header_style(Style::default().fg(Color::Yellow))
|
||||||
/// .widths(&[5, 5, 10])
|
/// .widths(&[5, 5, 10])
|
||||||
|
/// .style(Style::default().fg(Color::White))
|
||||||
/// .column_spacing(1)
|
/// .column_spacing(1)
|
||||||
/// .rows(vec![["Row11", "Row12", "Row13"].as_ref(),
|
/// .rows(vec![["Row11", "Row12", "Row13"].as_ref(),
|
||||||
/// ["Row21", "Row22", "Row23"].as_ref(),
|
/// ["Row21", "Row22", "Row23"].as_ref(),
|
||||||
/// ["Row31", "Row32", "Row33"].as_ref()])
|
/// ["Row31", "Row32", "Row33"].as_ref()]);
|
||||||
/// .color(Color::White)
|
|
||||||
/// .background_color(Color::Black);
|
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Table<'a> {
|
pub struct Table<'a> {
|
||||||
@ -97,6 +96,12 @@ impl<'a> Table<'a> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn style(&mut self, style: Style) -> &mut Table<'a> {
|
||||||
|
self.style = style;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn column_spacing(&mut self, spacing: u16) -> &mut Table<'a> {
|
pub fn column_spacing(&mut self, spacing: u16) -> &mut Table<'a> {
|
||||||
self.column_spacing = spacing;
|
self.column_spacing = spacing;
|
||||||
self
|
self
|
||||||
|
@ -13,14 +13,13 @@ use symbols::line;
|
|||||||
/// ```
|
/// ```
|
||||||
/// # extern crate tui;
|
/// # extern crate tui;
|
||||||
/// # use tui::widgets::{Block, border, Tabs};
|
/// # use tui::widgets::{Block, border, Tabs};
|
||||||
/// # use tui::style::Color;
|
/// # use tui::style::{Style, Color};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// Tabs::default()
|
/// Tabs::default()
|
||||||
/// .block(Block::default().title("Tabs").borders(border::ALL))
|
/// .block(Block::default().title("Tabs").borders(border::ALL))
|
||||||
/// .titles(&["Tab1", "Tab2", "Tab3", "Tab4"])
|
/// .titles(&["Tab1", "Tab2", "Tab3", "Tab4"])
|
||||||
/// .color(Color::White)
|
/// .style(Style::default().fg(Color::White))
|
||||||
/// .highlight_color(Color::Yellow)
|
/// .highlight_style(Style::default().fg(Color::Yellow));
|
||||||
/// .background_color(Color::Black);
|
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Tabs<'a> {
|
pub struct Tabs<'a> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user