mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-21 20:23:11 +00:00
chore: move async example to examples folder
Simplify a little by removing the need for the github api token.
This commit is contained in:
parent
e7085e3a3e
commit
27b1ca5784
5 changed files with 113 additions and 28 deletions
78
Cargo.lock
generated
78
Cargo.lock
generated
|
@ -154,6 +154,18 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"color-eyre",
|
||||||
|
"crossterm",
|
||||||
|
"octocrab 0.42.0",
|
||||||
|
"ratatui",
|
||||||
|
"tokio",
|
||||||
|
"tokio-stream",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.83"
|
version = "0.1.83"
|
||||||
|
@ -367,8 +379,10 @@ checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"android-tzdata",
|
"android-tzdata",
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
"windows-targets",
|
"windows-targets",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1598,6 +1612,46 @@ dependencies = [
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "octocrab"
|
||||||
|
version = "0.42.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5235d5839910001bef2c3df99a88688c7c781e5b1fd5fe40c5d8fa8bd786ac5a"
|
||||||
|
dependencies = [
|
||||||
|
"arc-swap",
|
||||||
|
"async-trait",
|
||||||
|
"base64 0.22.1",
|
||||||
|
"bytes",
|
||||||
|
"cfg-if",
|
||||||
|
"chrono",
|
||||||
|
"either",
|
||||||
|
"futures",
|
||||||
|
"futures-util",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper",
|
||||||
|
"hyper-rustls",
|
||||||
|
"hyper-timeout",
|
||||||
|
"hyper-util",
|
||||||
|
"jsonwebtoken",
|
||||||
|
"once_cell",
|
||||||
|
"percent-encoding",
|
||||||
|
"pin-project",
|
||||||
|
"secrecy",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"serde_path_to_error",
|
||||||
|
"serde_urlencoded",
|
||||||
|
"snafu",
|
||||||
|
"tokio",
|
||||||
|
"tower",
|
||||||
|
"tower-http",
|
||||||
|
"tracing",
|
||||||
|
"url",
|
||||||
|
"web-time",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.20.2"
|
version = "1.20.2"
|
||||||
|
@ -2099,7 +2153,7 @@ dependencies = [
|
||||||
"indoc",
|
"indoc",
|
||||||
"instability",
|
"instability",
|
||||||
"itertools 0.13.0",
|
"itertools 0.13.0",
|
||||||
"octocrab",
|
"octocrab 0.41.2",
|
||||||
"palette",
|
"palette",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
|
@ -2978,6 +3032,17 @@ dependencies = [
|
||||||
"tokio",
|
"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]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.12"
|
version = "0.7.12"
|
||||||
|
@ -3356,6 +3421,17 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "web-time"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
|
||||||
|
dependencies = [
|
||||||
|
"js-sys",
|
||||||
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wezterm-bidi"
|
name = "wezterm-bidi"
|
||||||
version = "0.2.3"
|
version = "0.2.3"
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = ["ratatui", "ratatui-core", "ratatui-widgets", "xtask"]
|
members = [
|
||||||
|
"examples/*/*",
|
||||||
|
"ratatui",
|
||||||
|
"ratatui-core",
|
||||||
|
"ratatui-widgets",
|
||||||
|
"xtask",
|
||||||
|
]
|
||||||
default-members = ["ratatui", "ratatui-core", "ratatui-widgets"]
|
default-members = ["ratatui", "ratatui-core", "ratatui-widgets"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
|
@ -25,6 +31,7 @@ rust-version = "1.74.0"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
bitflags = "2.6.0"
|
bitflags = "2.6.0"
|
||||||
|
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
||||||
indoc = "2.0.5"
|
indoc = "2.0.5"
|
||||||
instability = "0.3.1"
|
instability = "0.3.1"
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
|
|
22
examples/apps/async/Cargo.toml
Normal file
22
examples/apps/async/Cargo.toml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
[package]
|
||||||
|
name = "async-example"
|
||||||
|
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"
|
|
@ -1,9 +1,7 @@
|
||||||
//! # [Ratatui] Async example
|
//! # [Ratatui] Async example
|
||||||
//!
|
//!
|
||||||
//! This example demonstrates how to use Ratatui with widgets that fetch data asynchronously. It
|
//! 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
|
//! uses the `octocrab` crate to fetch a list of pull requests from the GitHub API.
|
||||||
//! environment variable named `GITHUB_TOKEN` with a valid GitHub personal access token. The token
|
|
||||||
//! does not need any special permissions.
|
|
||||||
//!
|
//!
|
||||||
//! <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://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)
|
//! <https://github.com/settings/tokens/new> to create a new token (select classic, and no scopes)
|
||||||
|
@ -34,11 +32,10 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use color_eyre::{eyre::Context, Result, Section};
|
use color_eyre::Result;
|
||||||
use futures::StreamExt;
|
|
||||||
use octocrab::{
|
use octocrab::{
|
||||||
params::{pulls::Sort, Direction},
|
params::{pulls::Sort, Direction},
|
||||||
OctocrabBuilder, Page,
|
Page,
|
||||||
};
|
};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
|
@ -49,29 +46,17 @@ use ratatui::{
|
||||||
widgets::{Block, HighlightSpacing, Row, StatefulWidget, Table, TableState, Widget},
|
widgets::{Block, HighlightSpacing, Row, StatefulWidget, Table, TableState, Widget},
|
||||||
DefaultTerminal, Frame,
|
DefaultTerminal, Frame,
|
||||||
};
|
};
|
||||||
|
use tokio_stream::StreamExt;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
init_octocrab()?;
|
|
||||||
let terminal = ratatui::init();
|
let terminal = ratatui::init();
|
||||||
let app_result = App::default().run(terminal).await;
|
let app_result = App::default().run(terminal).await;
|
||||||
ratatui::restore();
|
ratatui::restore();
|
||||||
app_result
|
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)]
|
#[derive(Debug, Default)]
|
||||||
struct App {
|
struct App {
|
||||||
should_quit: bool,
|
should_quit: bool,
|
|
@ -15,7 +15,7 @@ edition.workspace = true
|
||||||
rust-version.workspace = true
|
rust-version.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm = { version = "0.28.1", optional = true }
|
crossterm = { workspace = true, optional = true }
|
||||||
document-features = { version = "0.2.7", optional = true }
|
document-features = { version = "0.2.7", optional = true }
|
||||||
indoc = "2"
|
indoc = "2"
|
||||||
instability.workspace = true
|
instability.workspace = true
|
||||||
|
@ -38,7 +38,7 @@ termion = { version = "4.0.0", optional = true }
|
||||||
argh = "0.1.12"
|
argh = "0.1.12"
|
||||||
color-eyre = "0.6.2"
|
color-eyre = "0.6.2"
|
||||||
criterion = { version = "0.5.1", features = ["html_reports"] }
|
criterion = { version = "0.5.1", features = ["html_reports"] }
|
||||||
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
crossterm = { workspace = true, features = ["event-stream"] }
|
||||||
fakeit = "1.1"
|
fakeit = "1.1"
|
||||||
font8x8 = "0.3.1"
|
font8x8 = "0.3.1"
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
|
@ -181,11 +181,6 @@ bench = false
|
||||||
name = "main"
|
name = "main"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "async"
|
|
||||||
required-features = ["crossterm"]
|
|
||||||
doc-scrape-examples = true
|
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "barchart"
|
name = "barchart"
|
||||||
required-features = ["crossterm"]
|
required-features = ["crossterm"]
|
||||||
|
|
Loading…
Reference in a new issue