Skip to content

Commit 8564741

Browse files
committed
progress: Fix clippy warnings and clean up
1 parent e5c6cbb commit 8564741

1 file changed

Lines changed: 22 additions & 32 deletions

File tree

src/progress.rs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,30 @@ pub async fn monitor_stderr(
7575

7676
// We loop over the stream reading byte by byte
7777
// and process lines as they are completed.
78-
loop {
79-
match reader.read_u8().await {
80-
Ok(byte) => {
81-
raw_log.push(byte);
82-
83-
// Git output lines end with either \n or \r
84-
// Every time we encounter one, we process the line.
85-
// Note: \r is used for progress updates, meaning the line
86-
// is overwritten in place.
87-
if byte == b'\r' || byte == b'\n' {
88-
if !buffer.is_empty() {
89-
if let Ok(line) = std::str::from_utf8(&buffer) {
90-
// Update UI if we have a handler
91-
if let Some(h) = &handler {
92-
h.update_pb(line, &mut state.as_mut().unwrap());
93-
}
94-
}
95-
// Clear the buffer for the next line
96-
buffer.clear();
97-
}
98-
} else {
99-
buffer.push(byte);
100-
}
101-
}
102-
// We break the loop on EOF or error
103-
Err(_) => break,
78+
while let Ok(byte) = reader.read_u8().await {
79+
raw_log.push(byte);
80+
81+
// We push bytes into the buffer until we hit a delimiter
82+
if byte != b'\r' && byte != b'\n' {
83+
buffer.push(byte);
84+
continue;
10485
}
86+
87+
// Process the line, if we can parse it and have a handler
88+
if let (Ok(line), Some(h)) = (std::str::from_utf8(&buffer), &handler) {
89+
// Parse the line and update the progress bar accordingly
90+
let progress = parse_git_line(line);
91+
h.update_pb(progress, state.as_mut().unwrap());
92+
}
93+
94+
// Always clear buffer after a delimiter
95+
buffer.clear();
10596
}
10697

10798
// Finalize the progress bar if we have a handler
108-
handler.map(|h| h.finish(&mut state.unwrap()));
99+
if let Some(handler) = handler {
100+
handler.finish(&mut state.unwrap());
101+
}
109102

110103
// Return the full raw log as a string
111104
String::from_utf8_lossy(&raw_log).to_string()
@@ -159,10 +152,7 @@ impl ProgressHandler {
159152
}
160153

161154
/// Update the progress bar(s) based on a parsed git progress line.
162-
pub fn update_pb(&self, line: &str, state: &mut ProgressState) {
163-
// Parse the line to determine the type of progress update
164-
let progress = parse_git_line(line);
165-
155+
fn update_pb(&self, progress: GitProgress, state: &mut ProgressState) {
166156
// Target the active submodule if one exists, otherwise the main bar
167157
let target_pb = if let Some(name) = &state.active_sub {
168158
state.sub_bars.get(name).unwrap_or(&state.pb)
@@ -365,7 +355,7 @@ pub fn parse_git_line(line: &str) -> GitProgress {
365355
fn path_to_name(path: &str) -> String {
366356
path.trim_end_matches('/')
367357
.split('/')
368-
.last()
358+
.next_back()
369359
.unwrap_or(path)
370360
.to_string()
371361
}

0 commit comments

Comments
 (0)