Skip to content

Commit

Permalink
main+lib: Allow for interrupting image sending
Browse files Browse the repository at this point in the history
This could probably be generalized to all messages, but apart from
this one, I think it would only make sense to also handle this for
cases where the user pastes 1.5GiB of plaintext into MsgConsole, but
then stdin is clogged up and there's no signals, so CTRL+A would wait
for its turn anyway..

Signed-off-by: Konrad Dybcio <[email protected]>
  • Loading branch information
konradybcio committed May 7, 2024
1 parent f1ab395 commit 98e0712
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,23 @@ pub async fn console_print(buf: &[u8]) {
}

#[allow(clippy::explicit_write)]
pub async fn send_image(write_sink: &mut Arc<Mutex<impl Write>>, buf: &[u8]) -> anyhow::Result<()> {
pub async fn send_image(
write_sink: &mut Arc<Mutex<impl Write>>,
buf: &[u8],
quit: &Arc<Mutex<bool>>,
) -> anyhow::Result<()> {
let mut last_percent_done: usize = 0;
let mut bytes_sent = 0;

for chunk in buf.chunks(2048) {
let percent_done = 100 * bytes_sent / buf.len();

if *quit.lock().await {
return Ok(());
}

if percent_done != last_percent_done {
let s = format!(" Sending image: {}%\r", percent_done);
let s = format!("Sending image: {}%\r", percent_done);
print!("{}", s.green());
stdout().flush()?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async fn main() -> anyhow::Result<()> {
Ok(Sk8brdMsgs::MsgPowerOff) => (),
Ok(Sk8brdMsgs::MsgFastbootPresent) => {
if !msgbuf.is_empty() && msgbuf[0] != 0 {
send_image(&mut chan, &fastboot_image).await?
send_image(&mut chan, &fastboot_image, &quit).await?
}
}
Ok(Sk8brdMsgs::MsgFastbootDownload) => (),
Expand Down Expand Up @@ -197,6 +197,6 @@ async fn main() -> anyhow::Result<()> {
)
.context("Couldn't disconnect cleanly")?;

println!("Goodbye");
println!("\nGoodbye");
Ok(())
}

0 comments on commit 98e0712

Please sign in to comment.