From fcac19d6c5ced34582d9430179200e4665e81ae6 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Fri, 4 Nov 2016 17:54:57 +0100 Subject: [PATCH] Documentation --- Makefile | 5 +++++ src/widgets/block.rs | 34 +++++++++++++++++++++++++++++----- src/widgets/chart.rs | 13 +++++++------ src/widgets/tabs.rs | 23 +++++++++++++++++++++++ src/widgets/text.rs | 31 ++++++++++++++++++++++++------- 5 files changed, 88 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index c0c87eb9..8d511bab 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,13 @@ build: cargo build test: cargo test +doc: + cargo doc watch: watchman-make -p 'src/**/*.rs' -t build -p 'test/**/*.rs' -t test watch-test: watchman-make -p 'src/**/*.rs' 'tests/**/*.rs' -t test + +watch-doc: + watchman-make -p 'src/**/*.rs' -t doc diff --git a/src/widgets/block.rs b/src/widgets/block.rs index 0d9da2cf..75402d1f 100644 --- a/src/widgets/block.rs +++ b/src/widgets/block.rs @@ -4,13 +4,36 @@ use style::Color; use widgets::{Widget, border}; use symbols::line; +/// Base widget to be used with all upper level ones. It may be used to display a box border around +/// the widget and/or add a title. +/// +/// # Examples +/// +/// ``` +/// # extern crate tui; +/// # use tui::widgets::{Block, border}; +/// # use tui::style::Color; +/// # fn main() { +/// Block::default() +/// .title("Block") +/// .title_color(Color::Red) +/// .borders(border::LEFT | border::RIGHT) +/// .border_color(Color::White) +/// .background_color(Color::Black); +/// # } +/// ``` #[derive(Clone, Copy)] pub struct Block<'a> { - pub title: Option<&'a str>, - pub title_color: Color, - pub borders: border::Flags, - pub border_color: Color, - pub background_color: Color, + /// Optional title place on the upper left of the block + title: Option<&'a str>, + /// Title color + title_color: Color, + /// Visible borders + borders: border::Flags, + /// Borders color + border_color: Color, + /// Background color + background_color: Color, } impl<'a> Default for Block<'a> { @@ -51,6 +74,7 @@ impl<'a> Block<'a> { self } + /// Compute the inner area of a block based on its border visibility rules. pub fn inner(&self, area: &Rect) -> Rect { if area.width < 2 || area.height < 2 { return Rect::default(); diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index f07c199a..1d941e83 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -3,7 +3,7 @@ use std::cmp::max; use unicode_width::UnicodeWidthStr; use widgets::{Widget, Block, border}; -use widgets::canvas::{Canvas, Shape, Points}; +use widgets::canvas::{Canvas, Points}; use buffer::Buffer; use layout::Rect; use style::Color; @@ -388,11 +388,12 @@ impl<'a> Widget for Chart<'a> { .background_color(self.background_color) .x_bounds(self.x_axis.bounds) .y_bounds(self.y_axis.bounds) - .layer([&Points { - coords: dataset.data, - color: dataset.color, - } as &Shape] - .as_ref()) + .paint(|ctx| { + ctx.draw(&Points { + coords: dataset.data, + color: dataset.color, + }); + }) .draw(&graph_area, buf); } } diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index d0f30132..bc266104 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -6,12 +6,35 @@ use layout::Rect; use style::Color; use symbols::line; +/// A widget to display available tabs in a multiple panels context. +/// +/// # Examples +/// +/// ``` +/// # extern crate tui; +/// # use tui::widgets::{Block, border, Tabs}; +/// # use tui::style::Color; +/// # fn main() { +/// Tabs::default() +/// .block(Block::default().title("Tabs").borders(border::ALL)) +/// .titles(&["Tab1", "Tab2", "Tab3", "Tab4"]) +/// .color(Color::White) +/// .highlight_color(Color::Yellow) +/// .background_color(Color::Black); +/// # } +/// ``` pub struct Tabs<'a> { + /// A block to wrap this widget in if necessary block: Option>, + /// One title for each tab titles: &'a [&'a str], + /// The index of the selected tabs selected: usize, + /// The color used to draw the text color: Color, + /// The background color of this widget background_color: Color, + /// The color used to display the selected item highlight_color: Color, } diff --git a/src/widgets/text.rs b/src/widgets/text.rs index ed4edc0d..aa7fe907 100644 --- a/src/widgets/text.rs +++ b/src/widgets/text.rs @@ -1,17 +1,40 @@ use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; use widgets::{Widget, Block}; use buffer::Buffer; use layout::Rect; use style::Color; +/// A widget to display some text. You can specify colors using commands embedded in +/// the text such as "{[color] [text]}". +/// +/// # Examples +/// +/// ``` +/// # extern crate tui; +/// # use tui::widgets::{Block, border, Text}; +/// # use tui::style::Color; +/// # fn main() { +/// Text::default() +/// .block(Block::default().title("Text").borders(border::ALL)) +/// .color(Color::White) +/// .background_color(Color::Black) +/// .wrap(true) +/// .text("First line\nSecond line\n{red Colored text}."); +/// # } +/// ``` pub struct Text<'a> { + /// A block to wrap the widget in block: Option>, + /// The base color used to render the text color: Color, + /// Background color of the widget background_color: Color, + /// Wrap the text or not wrapping: bool, + /// The text to display text: &'a str, - colors: &'a [(u16, u16, u16, Color, Color)], } impl<'a> Default for Text<'a> { @@ -22,7 +45,6 @@ impl<'a> Default for Text<'a> { background_color: Color::Reset, wrapping: false, text: "", - colors: &[], } } } @@ -48,11 +70,6 @@ impl<'a> Text<'a> { self } - pub fn colors(&mut self, colors: &'a [(u16, u16, u16, Color, Color)]) -> &mut Text<'a> { - self.colors = colors; - self - } - pub fn wrap(&mut self, flag: bool) -> &mut Text<'a> { self.wrapping = flag; self