upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/git_remote_nostr
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-09-18 11:11:41 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-09-18 11:11:41 +0100
commitc7838f952f9e32bb871ea6453595b8b14e8fdd3e (patch)
treec3fcb523ba9d64911bcf5abf9728a4e99b67eb32 /src/bin/git_remote_nostr
parent7718a56ab05038e743401ea01628d85edc50ed34 (diff)
fix(remote): reporter lines on narrow terminals
remove the correct number of lines when reporter prints to narrow terminals
Diffstat (limited to 'src/bin/git_remote_nostr')
-rw-r--r--src/bin/git_remote_nostr/fetch.rs13
-rw-r--r--src/bin/git_remote_nostr/push.rs16
-rw-r--r--src/bin/git_remote_nostr/utils.rs14
3 files changed, 31 insertions, 12 deletions
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::{
20}; 20};
21 21
22use crate::utils::{ 22use crate::utils::{
23 fetch_or_list_error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, 23 count_lines_per_msg_vec, fetch_or_list_error_is_not_authentication_failure,
24 get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and, 24 find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch, get_open_proposals,
25 set_protocol_preference, Direction, 25 get_read_protocols_to_try, join_with_and, set_protocol_preference, Direction,
26}; 26};
27 27
28pub async fn run_fetch( 28pub async fn run_fetch(
@@ -270,7 +270,9 @@ impl<'a> FetchReporter<'a> {
270 } 270 }
271 } 271 }
272 fn count_all_existing_lines(&self) -> usize { 272 fn count_all_existing_lines(&self) -> usize {
273 self.remote_msgs.len() + self.transfer_progress_msgs.len() 273 let width = self.term.size().1;
274 count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len())
275 + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0)
274 } 276 }
275 fn just_write_transfer_progress(&self, lines_to_clear: usize) { 277 fn just_write_transfer_progress(&self, lines_to_clear: usize) {
276 let _ = self.term.clear_last_lines(lines_to_clear); 278 let _ = self.term.clear_last_lines(lines_to_clear);
@@ -279,7 +281,8 @@ impl<'a> FetchReporter<'a> {
279 } 281 }
280 } 282 }
281 fn just_count_transfer_progress(&self) -> usize { 283 fn just_count_transfer_progress(&self) -> usize {
282 self.transfer_progress_msgs.len() 284 let width = self.term.size().1;
285 count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0)
283 } 286 }
284 fn process_remote_msg(&mut self, data: &[u8]) { 287 fn process_remote_msg(&mut self, data: &[u8]) {
285 if let Ok(data) = str::from_utf8(data) { 288 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::{
39 git::Repo, 39 git::Repo,
40 list::list_from_remotes, 40 list::list_from_remotes,
41 utils::{ 41 utils::{
42 find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, 42 count_lines_per_msg_vec, find_proposal_and_patches_by_branch_name, get_all_proposals,
43 get_short_git_server_name, get_write_protocols_to_try, join_with_and, 43 get_remote_name_by_url, get_short_git_server_name, get_write_protocols_to_try,
44 push_error_is_not_authentication_failure, read_line, set_protocol_preference, Direction, 44 join_with_and, push_error_is_not_authentication_failure, read_line,
45 set_protocol_preference, Direction,
45 }, 46 },
46}; 47};
47 48
@@ -580,10 +581,11 @@ impl<'a> PushReporter<'a> {
580 } 581 }
581 582
582 fn count_all_existing_lines(&self) -> usize { 583 fn count_all_existing_lines(&self) -> usize {
583 self.remote_msgs.len() 584 let width = self.term.size().1;
584 + self.negotiation.len() 585 count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len())
585 + self.transfer_progress_msgs.len() 586 + count_lines_per_msg_vec(width, &self.negotiation, 0)
586 + self.update_reference_errors.len() 587 + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0)
588 + count_lines_per_msg_vec(width, &self.update_reference_errors, 0)
587 } 589 }
588 fn process_remote_msg(&mut self, data: &[u8]) { 590 fn process_remote_msg(&mut self, data: &[u8]) {
589 if let Ok(data) = str::from_utf8(data) { 591 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 {
384 false 384 false
385} 385}
386 386
387fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize {
388 if width == 0 {
389 return 1;
390 }
391 // ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic
392 ((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize
393}
394
395pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize {
396 msgs.iter()
397 .map(|msg| count_lines_per_msg(width, msg, prefix_len))
398 .sum()
399}
400
387#[cfg(test)] 401#[cfg(test)]
388mod tests { 402mod tests {
389 use super::*; 403 use super::*;