From bac1118d417261ee71738553b7c508f6ea6b48a8 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 14 Feb 2024 17:12:18 +0000 Subject: feat: get_events coutdown to timeout add a ui feature to countdown to timeout so user doesn't consider exiting early --- src/client.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index 9bc171a..2b0c960 100644 --- a/src/client.rs +++ b/src/client.rs @@ -10,12 +10,12 @@ // which is currently in nightly. alternatively we can use nightly as it looks // certain that the implementation is going to make it to stable but we don't // want to inadvertlty use other features of nightly that might be removed. -use std::time::Duration; +use std::{fmt::Write, time::Duration}; use anyhow::{Context, Result}; use async_trait::async_trait; use futures::stream::{self, StreamExt}; -use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; #[cfg(test)] use mockall::*; use nostr::Event; @@ -143,7 +143,18 @@ impl Connect for Client { } let m = MultiProgress::new(); - let pb_style = ProgressStyle::with_template(" {spinner} {prefix} {msg}")?; + let pb_style = ProgressStyle::with_template(" {spinner} {prefix} {msg} {timeout_in}")? + .with_key("timeout_in", |state: &ProgressState, w: &mut dyn Write| { + if state.elapsed().as_secs() > 3 { + write!( + w, + "timeout in {:.1}s", + GET_EVENTS_TIMEOUT - state.elapsed().as_secs() + ) + .unwrap(); + } + }); + let pb_after_style = |succeed| { ProgressStyle::with_template( format!( @@ -228,6 +239,8 @@ impl Connect for Client { } } +static GET_EVENTS_TIMEOUT: u64 = 10; + async fn get_events_of( relay: &nostr_sdk::Relay, filters: Vec, @@ -244,7 +257,7 @@ async fn get_events_of( .get_events_of( filters, // 20 is nostr_sdk default - std::time::Duration::from_secs(10), + std::time::Duration::from_secs(GET_EVENTS_TIMEOUT), nostr_sdk::FilterOptions::ExitOnEOSE, ) .await?; -- cgit v1.2.3