From b5f7e4418364c9710d14567c73122af67e0a63ae Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Mon, 9 Dec 2024 07:30:37 -0800 Subject: [PATCH] chore(examples): move async example to apps (#1503) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move async example to examples/apps/async as full project. Simplify a little by removing the need for the github api token. Co-authored-by: Orhun Parmaksız --- Cargo.lock | 28 +++++++++++++++++-- deny.toml | 13 +++++++++ examples/README.md | 6 +++- examples/apps/async-github/Cargo.toml | 22 +++++++++++++++ examples/apps/async-github/README.md | 9 ++++++ .../apps/async-github/src/main.rs | 23 +++------------ ratatui/Cargo.toml | 6 ---- 7 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 examples/apps/async-github/Cargo.toml create mode 100644 examples/apps/async-github/README.md rename ratatui/examples/async.rs => examples/apps/async-github/src/main.rs (91%) diff --git a/Cargo.lock b/Cargo.lock index 7d3c3199..52287f5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,18 @@ dependencies = [ "serde", ] +[[package]] +name = "async-github" +version = "0.1.0" +dependencies = [ + "color-eyre", + "crossterm", + "octocrab", + "ratatui", + "tokio", + "tokio-stream", +] + [[package]] name = "async-trait" version = "0.1.83" @@ -1828,9 +1840,9 @@ dependencies = [ [[package]] name = "octocrab" -version = "0.42.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b97f949a7cb04608441c2ddb28e15a377e8b5142c2d1835ad2686d434de8558" +checksum = "5235d5839910001bef2c3df99a88688c7c781e5b1fd5fe40c5d8fa8bd786ac5a" dependencies = [ "arc-swap", "async-trait", @@ -2367,7 +2379,6 @@ dependencies = [ "indoc", "instability", "itertools 0.13.0", - "octocrab", "palette", "pretty_assertions", "rand 0.8.5", @@ -3287,6 +3298,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.12" diff --git a/deny.toml b/deny.toml index 0de58660..b026012a 100644 --- a/deny.toml +++ b/deny.toml @@ -9,6 +9,7 @@ allow = [ "BSD-3-Clause", "ISC", "MIT", + "OpenSSL", "Unicode-3.0", "Unicode-DFS-2016", "WTFPL", @@ -25,3 +26,15 @@ multiple-versions = "allow" unknown-registry = "deny" unknown-git = "warn" allow-registry = ["https://github.com/rust-lang/crates.io-index"] + +[[licenses.clarify]] +crate = "ring" +# SPDX considers OpenSSL to encompass both the OpenSSL and SSLeay licenses +# https://spdx.org/licenses/OpenSSL.html +# ISC - Both BoringSSL and ring use this for their new files +# MIT - "Files in third_party/ have their own licenses, as described therein. The MIT +# license, for third_party/fiat, which, unlike other third_party directories, is +# compiled into non-test libraries, is included below." +# OpenSSL - Obviously +expression = "ISC AND MIT AND OpenSSL" +license-files = [{ path = "LICENSE", hash = 0xbd0eed23 }] diff --git a/examples/README.md b/examples/README.md index f096592c..88073208 100644 --- a/examples/README.md +++ b/examples/README.md @@ -24,7 +24,7 @@ of the `main` branch for code which is guaranteed to work with the released rata > > - View the examples as they were when the latest version was release by selecting the tag that > matches that version. E.g. . -> - If you're viewing this file on GitHub, there is a combo box at the top of this page which +> - If you're viewing this file on GitHub, there is a combo box at the top of this page which > allows you to select any previous tagged version. > - To view the code locally, checkout the tag. E.g. `git switch --detach v0.26.1`. > - Use the latest [alpha version of Ratatui] in your app. These are released weekly on Saturdays. @@ -52,3 +52,7 @@ This is the demo example from the main README and crate page. [Source](./apps/de ## Mouse Drawing demo 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/). diff --git a/examples/apps/async-github/Cargo.toml b/examples/apps/async-github/Cargo.toml new file mode 100644 index 00000000..3c0278af --- /dev/null +++ b/examples/apps/async-github/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "async-github" +version = "0.1.0" +authors.workspace = true +documentation.workspace = true +repository.workspace = true +homepage.workspace = true +keywords.workspace = true +categories.workspace = true +readme.workspace = true +license.workspace = true +exclude.workspace = true +edition.workspace = true +rust-version.workspace = true + +[dependencies] +color-eyre = "0.6.3" +crossterm = { workspace = true, features = ["event-stream"] } +octocrab = "0.42.0" +ratatui.workspace = true +tokio = { version = "1.41.1", features = ["rt-multi-thread", "macros"] } +tokio-stream = "0.1.16" diff --git a/examples/apps/async-github/README.md b/examples/apps/async-github/README.md new file mode 100644 index 00000000..b6a39c2f --- /dev/null +++ b/examples/apps/async-github/README.md @@ -0,0 +1,9 @@ +# Async GitHub demo + +This example demonstrates how to use Ratatui with widgets that fetch data from GitHub API asynchronously. + +To run this demo: + +```shell +cargo run -p async-github +``` diff --git a/ratatui/examples/async.rs b/examples/apps/async-github/src/main.rs similarity index 91% rename from ratatui/examples/async.rs rename to examples/apps/async-github/src/main.rs index c01761f5..ba453399 100644 --- a/ratatui/examples/async.rs +++ b/examples/apps/async-github/src/main.rs @@ -1,9 +1,7 @@ //! # [Ratatui] Async example //! //! This example demonstrates how to use Ratatui with widgets that fetch data asynchronously. It -//! uses the `octocrab` crate to fetch a list of pull requests from the GitHub API. You will need an -//! environment variable named `GITHUB_TOKEN` with a valid GitHub personal access token. The token -//! does not need any special permissions. +//! uses the `octocrab` crate to fetch a list of pull requests from the GitHub API. //! //! //! to create a new token (select classic, and no scopes) @@ -34,11 +32,10 @@ use std::{ time::Duration, }; -use color_eyre::{eyre::Context, Result, Section}; -use futures::StreamExt; +use color_eyre::Result; use octocrab::{ params::{pulls::Sort, Direction}, - OctocrabBuilder, Page, + Page, }; use ratatui::{ buffer::Buffer, @@ -49,29 +46,17 @@ use ratatui::{ widgets::{Block, HighlightSpacing, Row, StatefulWidget, Table, TableState, Widget}, DefaultTerminal, Frame, }; +use tokio_stream::StreamExt; #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; - init_octocrab()?; let terminal = ratatui::init(); let app_result = App::default().run(terminal).await; ratatui::restore(); app_result } -fn init_octocrab() -> Result<()> { - let token = std::env::var("GITHUB_TOKEN") - .wrap_err("The GITHUB_TOKEN environment variable was not found") - .suggestion( - "Go to https://github.com/settings/tokens/new to create a token, and re-run: - GITHUB_TOKEN=ghp_... cargo run --example async --features crossterm", - )?; - let crab = OctocrabBuilder::new().personal_token(token).build()?; - octocrab::initialise(crab); - Ok(()) -} - #[derive(Debug, Default)] struct App { should_quit: bool, diff --git a/ratatui/Cargo.toml b/ratatui/Cargo.toml index 3022c35e..d9931a3c 100644 --- a/ratatui/Cargo.toml +++ b/ratatui/Cargo.toml @@ -122,7 +122,6 @@ fakeit = "1.1" font8x8 = "0.3.1" futures = "0.3.30" indoc = "2" -octocrab = "0.42.1" pretty_assertions = "1.4.0" rand = "0.8.5" rand_chacha = "0.3.1" @@ -143,11 +142,6 @@ bench = false name = "main" harness = false -[[example]] -name = "async" -required-features = ["crossterm"] -doc-scrape-examples = true - [[example]] name = "barchart" required-features = ["crossterm"]