From 40b439ae4d69b858274be51dd5af513c3b4f46f0 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 13 Feb 2026 10:51:25 +0000 Subject: feat: add spinner for git fetch in non-verbose mode Shows a progress spinner when fetching from git remotes in non-verbose mode. Suppresses git fetch output and listing messages when not in verbose mode. Uses NGITTEST environment variable for test timeouts. --- src/bin/git_remote_nostr/list.rs | 6 ++-- src/bin/git_remote_nostr/main.rs | 13 +++++--- src/bin/ngit/main.rs | 6 +--- src/bin/ngit/sub_commands/apply.rs | 55 ++++++++++++++++++++++++++++---- src/bin/ngit/sub_commands/checkout.rs | 55 ++++++++++++++++++++++++++++---- src/bin/ngit/sub_commands/list.rs | 60 ++++++++++++++++++++++++++++------- 6 files changed, 159 insertions(+), 36 deletions(-) (limited to 'src/bin') diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index 7753ba1..4a7c1ec 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -4,7 +4,7 @@ use anyhow::{Context, Result}; use client::get_state_from_cache; use git::RepoActions; use ngit::{ - client, + client::{self, is_verbose}, fetch::fetch_from_git_server, git::{self}, git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value}, @@ -27,7 +27,9 @@ pub async fn run_list( let term = console::Term::stderr(); - term.write_line("git servers: listing refs...")?; + if is_verbose() { + term.write_line("git servers: listing refs...")?; + } let remote_states = list_from_remotes( &term, git_repo, diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index b26981d..3663d5b 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -12,7 +12,7 @@ use std::{ }; use anyhow::{Context, Result, bail}; -use client::{Connect, consolidate_fetch_reports, get_repo_ref_from_cache}; +use client::{Connect, consolidate_fetch_reports, get_repo_ref_from_cache, is_verbose}; use git::{RepoActions, nostr_url::NostrUrlDecoded}; use ngit::{ client::{self, Params}, @@ -156,7 +156,10 @@ async fn fetching_with_report_for_helper( trusted_maintainer_coordinate: &Nip19Coordinate, ) -> Result<()> { let term = console::Term::stderr(); - term.write_line("nostr: fetching...")?; + let verbose = is_verbose(); + if verbose { + term.write_line("nostr: fetching...")?; + } let (relay_reports, progress_reporter) = client .fetch_all( Some(git_repo_path), @@ -166,10 +169,12 @@ async fn fetching_with_report_for_helper( .await?; let report = consolidate_fetch_reports(relay_reports); if report.to_string().is_empty() { - term.write_line("nostr: no updates")?; + if verbose { + term.write_line("nostr: no updates")?; + } } else { term.write_line(&format!("nostr updates: {report}"))?; } - progress_reporter.clear()?; + let _ = progress_reporter.clear(); Ok(()) } diff --git a/src/bin/ngit/main.rs b/src/bin/ngit/main.rs index 65f6bca..77ef955 100644 --- a/src/bin/ngit/main.rs +++ b/src/bin/ngit/main.rs @@ -15,10 +15,6 @@ use ngit::{ mod sub_commands; -fn is_verbose() -> bool { - std::env::var("NGIT_VERBOSE").is_ok() || std::env::var("NGIT_TEST").is_ok() -} - #[tokio::main] async fn main() { let cli = Cli::parse(); @@ -29,7 +25,7 @@ async fn main() { std::env::set_var("NGIT_INTERACTIVE_MODE", "1"); } - if cli.verbose || std::env::var("NGIT_TEST").is_ok() { + if cli.verbose || std::env::var("NGITTEST").is_ok() { std::env::set_var("NGIT_VERBOSE", "1"); } diff --git a/src/bin/ngit/sub_commands/apply.rs b/src/bin/ngit/sub_commands/apply.rs index 4ed6caa..2d5d8d5 100644 --- a/src/bin/ngit/sub_commands/apply.rs +++ b/src/bin/ngit/sub_commands/apply.rs @@ -1,9 +1,11 @@ use std::{ io::Write, process::{Command, Stdio}, + time::Duration, }; use anyhow::{Context, Result, bail}; +use indicatif::{ProgressBar, ProgressStyle}; use ngit::{ client::get_all_proposal_patch_pr_pr_update_events_from_cache, git_events::get_pr_tip_event_or_most_recent_patch_with_ancestors, @@ -18,16 +20,55 @@ use crate::{ }; fn run_git_fetch(remote_name: &str) -> Result<()> { - println!("fetching from {remote_name}..."); - let exit_status = Command::new("git") + let verbose = ngit::client::is_verbose(); + if verbose { + println!("fetching from {remote_name}..."); + } + + let spinner = if verbose { + None + } else { + let pb = ProgressBar::new_spinner() + .with_style( + ProgressStyle::with_template("{spinner} {msg}") + .unwrap() + .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈"), + ) + .with_message(format!("Fetching from {remote_name}...")); + pb.enable_steady_tick(Duration::from_millis(100)); + Some(pb) + }; + + let output = Command::new("git") .args(["fetch", remote_name]) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .status() + .stdout(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .stderr(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .output() .context("failed to run git fetch")?; - if !exit_status.success() { - bail!("git fetch {remote_name} exited with error: {exit_status}"); + if let Some(spinner) = spinner { + spinner.finish_and_clear(); + } + + if !output.status.success() { + if !verbose { + let stderr = String::from_utf8_lossy(&output.stderr); + if !stderr.is_empty() { + eprintln!("{stderr}"); + } + } + bail!( + "git fetch {remote_name} exited with error: {}", + output.status + ); } Ok(()) } diff --git a/src/bin/ngit/sub_commands/checkout.rs b/src/bin/ngit/sub_commands/checkout.rs index 6ded778..2fc9a09 100644 --- a/src/bin/ngit/sub_commands/checkout.rs +++ b/src/bin/ngit/sub_commands/checkout.rs @@ -1,9 +1,11 @@ use std::{ collections::HashSet, process::{Command, Stdio}, + time::Duration, }; use anyhow::{Context, Result, bail}; +use indicatif::{ProgressBar, ProgressStyle}; use ngit::{ client::{ Params, get_all_proposal_patch_pr_pr_update_events_from_cache, @@ -97,16 +99,55 @@ pub async fn launch(id: &str) -> Result<()> { } fn run_git_fetch(remote_name: &str) -> Result<()> { - println!("fetching from {remote_name}..."); - let exit_status = Command::new("git") + let verbose = ngit::client::is_verbose(); + if verbose { + println!("fetching from {remote_name}..."); + } + + let spinner = if verbose { + None + } else { + let pb = ProgressBar::new_spinner() + .with_style( + ProgressStyle::with_template("{spinner} {msg}") + .unwrap() + .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈"), + ) + .with_message(format!("Fetching from {remote_name}...")); + pb.enable_steady_tick(Duration::from_millis(100)); + Some(pb) + }; + + let output = Command::new("git") .args(["fetch", remote_name]) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .status() + .stdout(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .stderr(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .output() .context("failed to run git fetch")?; - if !exit_status.success() { - bail!("git fetch {remote_name} exited with error: {exit_status}"); + if let Some(spinner) = spinner { + spinner.finish_and_clear(); + } + + if !output.status.success() { + if !verbose { + let stderr = String::from_utf8_lossy(&output.stderr); + if !stderr.is_empty() { + eprintln!("{stderr}"); + } + } + bail!( + "git fetch {remote_name} exited with error: {}", + output.status + ); } Ok(()) } diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs index 68a10cc..ff43bd9 100644 --- a/src/bin/ngit/sub_commands/list.rs +++ b/src/bin/ngit/sub_commands/list.rs @@ -3,9 +3,11 @@ use std::{ io::Write, ops::Add, process::{Command, Stdio}, + time::Duration, }; use anyhow::{Context, Result, bail}; +use indicatif::{ProgressBar, ProgressStyle}; use ngit::{ client::{ Params, get_all_proposal_patch_pr_pr_update_events_from_cache, @@ -39,16 +41,55 @@ use crate::{ }; fn run_git_fetch(remote_name: &str) -> Result<()> { - println!("fetching from {remote_name}..."); - let exit_status = Command::new("git") + let verbose = ngit::client::is_verbose(); + if verbose { + println!("fetching from {remote_name}..."); + } + + let spinner = if verbose { + None + } else { + let pb = ProgressBar::new_spinner() + .with_style( + ProgressStyle::with_template("{spinner} {msg}") + .unwrap() + .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈"), + ) + .with_message(format!("Fetching from {remote_name}...")); + pb.enable_steady_tick(Duration::from_millis(100)); + Some(pb) + }; + + let output = Command::new("git") .args(["fetch", remote_name]) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .status() + .stdout(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .stderr(if verbose { + Stdio::inherit() + } else { + Stdio::piped() + }) + .output() .context("failed to run git fetch")?; - if !exit_status.success() { - bail!("git fetch {remote_name} exited with error: {exit_status}"); + if let Some(spinner) = spinner { + spinner.finish_and_clear(); + } + + if !output.status.success() { + if !verbose { + let stderr = String::from_utf8_lossy(&output.stderr); + if !stderr.is_empty() { + eprintln!("{stderr}"); + } + } + bail!( + "git fetch {remote_name} exited with error: {}", + output.status + ); } Ok(()) } @@ -297,10 +338,7 @@ fn show_proposal_details( } println!(); - println!( - "To checkout: ngit checkout {}", - proposal.id - ); + println!("To checkout: ngit checkout {}", proposal.id); println!("To apply: ngit apply {}", proposal.id); Ok(()) -- cgit v1.2.3