From 819e92cd44b6bee7d21115ff465c2f3f8c82ed9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Wed, 11 Dec 2024 11:25:56 +0300 Subject: [PATCH] chore(examples): add weather demo app (#1567) related to #1512 --- Cargo.lock | 10 +++ examples/README.md | 4 ++ examples/apps/weather/Cargo.toml | 15 +++++ examples/apps/weather/README.md | 9 +++ .../apps/weather/src/main.rs | 64 ++++--------------- ratatui/Cargo.toml | 5 -- 6 files changed, 51 insertions(+), 56 deletions(-) create mode 100644 examples/apps/weather/Cargo.toml create mode 100644 examples/apps/weather/README.md rename ratatui/examples/barchart.rs => examples/apps/weather/src/main.rs (50%) diff --git a/Cargo.lock b/Cargo.lock index 13a792d6..6abc3a5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3694,6 +3694,16 @@ version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" +[[package]] +name = "weather" +version = "0.0.0" +dependencies = [ + "color-eyre", + "crossterm", + "rand 0.8.5", + "ratatui", +] + [[package]] name = "web-sys" version = "0.3.74" diff --git a/examples/README.md b/examples/README.md index 88073208..7b195c0f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -56,3 +56,7 @@ Shows how to handle mouse events. [Source](./apps/mouse-drawing/). ## Async GitHub demo Shows how to fetch data from GitHub API asynchronously. [Source](./apps/async-github/). + +## Weather demo + +Shows how to render weather data using barchart widget. [Source](./apps/weather/). diff --git a/examples/apps/weather/Cargo.toml b/examples/apps/weather/Cargo.toml new file mode 100644 index 00000000..c5fff68d --- /dev/null +++ b/examples/apps/weather/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "weather" +publish = false +license.workspace = true +edition.workspace = true +rust-version.workspace = true + +[dependencies] +color-eyre.workspace = true +crossterm.workspace = true +rand = "0.8.5" +ratatui.workspace = true + +[lints] +workspace = true diff --git a/examples/apps/weather/README.md b/examples/apps/weather/README.md new file mode 100644 index 00000000..0219e40a --- /dev/null +++ b/examples/apps/weather/README.md @@ -0,0 +1,9 @@ +# Weather demo + +This example shows how to render weather data using barchart widget. + +To run this demo: + +```shell +cargo run -p weather +``` diff --git a/ratatui/examples/barchart.rs b/examples/apps/weather/src/main.rs similarity index 50% rename from ratatui/examples/barchart.rs rename to examples/apps/weather/src/main.rs index 4941f38c..d76d945c 100644 --- a/ratatui/examples/barchart.rs +++ b/examples/apps/weather/src/main.rs @@ -1,26 +1,21 @@ -//! # [Ratatui] `BarChart` example +//! A Ratatui example that demonstrates how to render weather data using [`BarChart`] widget. //! -//! The latest version of this example is available in the [examples] folder in the repository. +//! Generates random temperature data for each hour of the day and renders it as a vertical bar. //! -//! Please note that the examples are designed to be run against the `main` branch of the Github -//! repository. This means that you may not be able to compile with the latest release version on -//! crates.io, or the one that you have installed locally. +//! This example runs with the Ratatui library code in the branch that you are currently reading. +//! See the [`latest`] branch for the code which works with the most recent Ratatui release. //! -//! See the [examples readme] for more information on finding examples that match the version of the -//! library you are using. -//! -//! [Ratatui]: https://github.com/ratatui/ratatui -//! [examples]: https://github.com/ratatui/ratatui/blob/main/examples -//! [examples readme]: https://github.com/ratatui/ratatui/blob/main/examples/README.md +//! [`latest`]: https://github.com/ratatui/ratatui/tree/latest +//! [`BarChart`]: https://docs.rs/ratatui/latest/ratatui/widgets/struct.BarChart.html use color_eyre::Result; use rand::{thread_rng, Rng}; use ratatui::{ crossterm::event::{self, Event, KeyCode, KeyEventKind}, - layout::{Constraint, Direction, Layout}, + layout::{Constraint, Layout}, style::{Color, Style, Stylize}, text::Line, - widgets::{Bar, BarChart, BarGroup, Block}, + widgets::{Bar, BarChart, BarGroup}, DefaultTerminal, Frame, }; @@ -65,17 +60,12 @@ impl App { } fn draw(&self, frame: &mut Frame) { - let [title, vertical, horizontal] = Layout::vertical([ - Constraint::Length(1), - Constraint::Fill(1), - Constraint::Fill(1), - ]) - .spacing(1) - .areas(frame.area()); + let [title, main] = Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]) + .spacing(1) + .areas(frame.area()); - frame.render_widget("Barchart".bold().into_centered_line(), title); - frame.render_widget(vertical_barchart(&self.temperatures), vertical); - frame.render_widget(horizontal_barchart(&self.temperatures), horizontal); + frame.render_widget("Weather demo".bold().into_centered_line(), title); + frame.render_widget(vertical_barchart(&self.temperatures), main); } } @@ -86,10 +76,8 @@ fn vertical_barchart(temperatures: &[u8]) -> BarChart { .enumerate() .map(|(hour, value)| vertical_bar(hour, value)) .collect(); - let title = Line::from("Weather (Vertical)").centered(); BarChart::default() .data(BarGroup::default().bars(&bars)) - .block(Block::new().title(title)) .bar_width(5) } @@ -102,32 +90,6 @@ fn vertical_bar(hour: usize, temperature: &u8) -> Bar { .value_style(temperature_style(*temperature).reversed()) } -/// Create a horizontal bar chart from the temperatures data. -fn horizontal_barchart(temperatures: &[u8]) -> BarChart { - let bars: Vec = temperatures - .iter() - .enumerate() - .map(|(hour, value)| horizontal_bar(hour, value)) - .collect(); - let title = Line::from("Weather (Horizontal)").centered(); - BarChart::default() - .block(Block::new().title(title)) - .data(BarGroup::default().bars(&bars)) - .bar_width(1) - .bar_gap(0) - .direction(Direction::Horizontal) -} - -fn horizontal_bar(hour: usize, temperature: &u8) -> Bar { - let style = temperature_style(*temperature); - Bar::default() - .value(u64::from(*temperature)) - .label(Line::from(format!("{hour:>02}:00"))) - .text_value(format!("{temperature:>3}°")) - .style(style) - .value_style(style.reversed()) -} - /// create a yellow to red value based on the value (50-90) fn temperature_style(value: u8) -> Style { let green = (255.0 * (1.0 - f64::from(value - 50) / 40.0)) as u8; diff --git a/ratatui/Cargo.toml b/ratatui/Cargo.toml index 2d593ff6..29b0ffee 100644 --- a/ratatui/Cargo.toml +++ b/ratatui/Cargo.toml @@ -142,11 +142,6 @@ bench = false name = "main" harness = false -[[example]] -name = "barchart" -required-features = ["crossterm"] -doc-scrape-examples = true - [[example]] name = "barchart-grouped" required-features = ["crossterm"]