chore(examples): move async example to apps (#1503)

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 <orhunparmaksiz@gmail.com>
This commit is contained in:
Josh McKinney 2024-12-09 07:30:37 -08:00 committed by GitHub
parent fab532171d
commit b5f7e44183
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 29 deletions

28
Cargo.lock generated
View File

@ -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"

View File

@ -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 }]

View File

@ -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. <https://github.com/ratatui/ratatui/tree/v0.26.1/examples>.
> - 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/).

View File

@ -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"

View File

@ -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
```

View File

@ -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.
//!
//! <https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token>
//! <https://github.com/settings/tokens/new> 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,

View File

@ -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"]