Skip to content

Commit

Permalink
fix(remote): reporter lines on narrow terminals
Browse files Browse the repository at this point in the history
remove the correct number of lines when reporter prints to
narrow terminals
DanConwayDev committed Sep 18, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 7718a56 commit c7838f9
Showing 3 changed files with 31 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/bin/git_remote_nostr/fetch.rs
Original file line number Diff line number Diff line change
@@ -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) {
16 changes: 9 additions & 7 deletions src/bin/git_remote_nostr/push.rs
Original file line number Diff line number Diff line change
@@ -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) {
14 changes: 14 additions & 0 deletions src/bin/git_remote_nostr/utils.rs
Original file line number Diff line number Diff line change
@@ -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::*;

0 comments on commit c7838f9

Please sign in to comment.