diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-12 15:13:50 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-12 15:13:50 +0100 |
| commit | e882fde7d3fc7f2113196585b450f7065061abb9 (patch) | |
| tree | 222ecde50be06a5873a3080762c072b28f50666c /src/bin/git_remote_nostr/utils.rs | |
| parent | 1a38f98807a3244b77d2525136f3d6976f61dcc4 (diff) | |
fix(remote): improve `push` status updates
to bring them more into line to the native git client
Diffstat (limited to 'src/bin/git_remote_nostr/utils.rs')
| -rw-r--r-- | src/bin/git_remote_nostr/utils.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 9be2580..1d65e3c 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | use core::str; | ||
| 1 | use std::{ | 2 | use std::{ |
| 2 | collections::HashMap, | 3 | collections::HashMap, |
| 3 | io::{self, Stdin}, | 4 | io::{self, Stdin}, |
| @@ -307,6 +308,87 @@ pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool { | |||
| 307 | false | 308 | false |
| 308 | } | 309 | } |
| 309 | 310 | ||
| 311 | pub enum TransferDirection { | ||
| 312 | Fetch, | ||
| 313 | Push, | ||
| 314 | } | ||
| 315 | |||
| 316 | pub enum ProgressStatus { | ||
| 317 | InProgress, | ||
| 318 | Complete, | ||
| 319 | } | ||
| 320 | |||
| 321 | #[allow(clippy::cast_precision_loss)] | ||
| 322 | #[allow(clippy::float_cmp)] | ||
| 323 | #[allow(clippy::needless_pass_by_value)] | ||
| 324 | pub fn report_on_transfer_progress( | ||
| 325 | progress_stats: &git2::Progress<'_>, | ||
| 326 | term: &console::Term, | ||
| 327 | direction: TransferDirection, | ||
| 328 | status: ProgressStatus, | ||
| 329 | ) { | ||
| 330 | let total = progress_stats.total_objects() as f64; | ||
| 331 | if total == 0.0 { | ||
| 332 | return; | ||
| 333 | } | ||
| 334 | let received = progress_stats.received_objects() as f64; | ||
| 335 | let percentage = (received / total) * 100.0; | ||
| 336 | |||
| 337 | // Get the total received bytes | ||
| 338 | let received_bytes = progress_stats.received_bytes() as f64; | ||
| 339 | |||
| 340 | // Determine whether to use KiB or MiB | ||
| 341 | let (size, unit) = if received_bytes >= (1024.0 * 1024.0) { | ||
| 342 | // Convert to MiB | ||
| 343 | (received_bytes / (1024.0 * 1024.0), "MiB") | ||
| 344 | } else { | ||
| 345 | // Convert to KiB | ||
| 346 | (received_bytes / 1024.0, "KiB") | ||
| 347 | }; | ||
| 348 | |||
| 349 | // Format the output for receiving objects | ||
| 350 | if received < total || matches!(status, ProgressStatus::Complete) { | ||
| 351 | let _ = term.write_line( | ||
| 352 | format!( | ||
| 353 | "{} objects: {percentage:.0}% ({received}/{total}) {size:.2} {unit}, done.\r", | ||
| 354 | if matches!(direction, TransferDirection::Fetch) { | ||
| 355 | "Receiving" | ||
| 356 | } else { | ||
| 357 | "Writing" | ||
| 358 | }, | ||
| 359 | ) | ||
| 360 | .as_str(), | ||
| 361 | ); | ||
| 362 | } | ||
| 363 | if received == total || matches!(status, ProgressStatus::Complete) { | ||
| 364 | let indexed_deltas = progress_stats.indexed_deltas() as f64; | ||
| 365 | let total_deltas = progress_stats.total_deltas() as f64; | ||
| 366 | let percentage = (indexed_deltas / total_deltas) * 100.0; | ||
| 367 | let _ = term.write_line( | ||
| 368 | format!("Resolving deltas: {percentage:.0}% ({indexed_deltas}/{total_deltas}) done.\r") | ||
| 369 | .as_str(), | ||
| 370 | ); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | pub fn report_on_sideband_progress(data: &[u8], term: &console::Term) { | ||
| 375 | if let Ok(data) = str::from_utf8(data) { | ||
| 376 | let data = data | ||
| 377 | .split(['\n', '\r']) | ||
| 378 | .find(|line| !line.is_empty()) | ||
| 379 | .unwrap_or(""); | ||
| 380 | if !data.is_empty() { | ||
| 381 | let s = format!("remote: {data}"); | ||
| 382 | let _ = term.clear_last_lines(1); | ||
| 383 | let _ = term.write_line(s.as_str()); | ||
| 384 | if !s.contains('%') || s.contains("100%") { | ||
| 385 | // print it twice so the next sideband_progress doesn't delete it | ||
| 386 | let _ = term.write_line(s.as_str()); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 310 | #[cfg(test)] | 392 | #[cfg(test)] |
| 311 | mod tests { | 393 | mod tests { |
| 312 | use super::*; | 394 | use super::*; |