From c7838f952f9e32bb871ea6453595b8b14e8fdd3e Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 18 Sep 2024 11:11:41 +0100 Subject: fix(remote): reporter lines on narrow terminals remove the correct number of lines when reporter prints to narrow terminals --- src/bin/git_remote_nostr/fetch.rs | 13 ++++++++----- src/bin/git_remote_nostr/push.rs | 16 +++++++++------- src/bin/git_remote_nostr/utils.rs | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index fed9925..5dd6ce8 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs @@ -20,9 +20,9 @@ use ngit::{ }; use crate::utils::{ - fetch_or_list_error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, - get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and, - set_protocol_preference, Direction, + count_lines_per_msg_vec, fetch_or_list_error_is_not_authentication_failure, + find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch, get_open_proposals, + get_read_protocols_to_try, join_with_and, set_protocol_preference, Direction, }; pub async fn run_fetch( @@ -270,7 +270,9 @@ impl<'a> FetchReporter<'a> { } } fn count_all_existing_lines(&self) -> usize { - self.remote_msgs.len() + self.transfer_progress_msgs.len() + let width = self.term.size().1; + count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len()) + + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0) } fn just_write_transfer_progress(&self, lines_to_clear: usize) { let _ = self.term.clear_last_lines(lines_to_clear); @@ -279,7 +281,8 @@ impl<'a> FetchReporter<'a> { } } fn just_count_transfer_progress(&self) -> usize { - self.transfer_progress_msgs.len() + let width = self.term.size().1; + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0) } fn process_remote_msg(&mut self, data: &[u8]) { if let Ok(data) = str::from_utf8(data) { diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index a0bcea3..05c9197 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -39,9 +39,10 @@ use crate::{ git::Repo, list::list_from_remotes, utils::{ - find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, - get_short_git_server_name, get_write_protocols_to_try, join_with_and, - push_error_is_not_authentication_failure, read_line, set_protocol_preference, Direction, + count_lines_per_msg_vec, find_proposal_and_patches_by_branch_name, get_all_proposals, + get_remote_name_by_url, get_short_git_server_name, get_write_protocols_to_try, + join_with_and, push_error_is_not_authentication_failure, read_line, + set_protocol_preference, Direction, }, }; @@ -580,10 +581,11 @@ impl<'a> PushReporter<'a> { } fn count_all_existing_lines(&self) -> usize { - self.remote_msgs.len() - + self.negotiation.len() - + self.transfer_progress_msgs.len() - + self.update_reference_errors.len() + let width = self.term.size().1; + count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len()) + + count_lines_per_msg_vec(width, &self.negotiation, 0) + + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0) + + count_lines_per_msg_vec(width, &self.update_reference_errors, 0) } fn process_remote_msg(&mut self, data: &[u8]) { if let Ok(data) = str::from_utf8(data) { diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 3ae1bab..7b5c2d2 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -384,6 +384,20 @@ pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool { false } +fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize { + if width == 0 { + return 1; + } + // ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic + ((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize +} + +pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize { + msgs.iter() + .map(|msg| count_lines_per_msg(width, msg, prefix_len)) + .sum() +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3