mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-16 15:46:36 +00:00
Add stdout/stderr printing support to UIThread
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
c33f195d5f
commit
ac492e6d8b
1 changed files with 40 additions and 24 deletions
|
@ -1,14 +1,26 @@
|
||||||
use std::io::{self, BufRead, Write};
|
use std::io::{self, BufRead, Write};
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
use crate::BinstallError;
|
use crate::BinstallError;
|
||||||
|
|
||||||
|
pub(super) enum UIRequest {
|
||||||
|
/// Request user confirmation
|
||||||
|
Confirm,
|
||||||
|
/// Print to stdout
|
||||||
|
PrintToStdout(Bytes),
|
||||||
|
/// Print to stderr
|
||||||
|
PrintToStderr(Bytes),
|
||||||
|
/// Flush stdout
|
||||||
|
FlushStdout,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UIThreadInner {
|
struct UIThreadInner {
|
||||||
/// Request for confirmation
|
/// Request for confirmation
|
||||||
request_tx: mpsc::Sender<()>,
|
request_tx: mpsc::Sender<UIRequest>,
|
||||||
|
|
||||||
/// Confirmation
|
/// Confirmation
|
||||||
confirm_rx: mpsc::Receiver<Result<(), BinstallError>>,
|
confirm_rx: mpsc::Receiver<Result<(), BinstallError>>,
|
||||||
|
@ -23,17 +35,13 @@ impl UIThreadInner {
|
||||||
// This task should be the only one able to
|
// This task should be the only one able to
|
||||||
// access stdin
|
// access stdin
|
||||||
let mut stdin = io::stdin().lock();
|
let mut stdin = io::stdin().lock();
|
||||||
|
let mut stdout = io::stdout().lock();
|
||||||
|
let mut stderr = io::stderr().lock();
|
||||||
let mut input = String::with_capacity(16);
|
let mut input = String::with_capacity(16);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if request_rx.blocking_recv().is_none() {
|
match request_rx.blocking_recv() {
|
||||||
break;
|
Some(UIRequest::Confirm) => {
|
||||||
}
|
|
||||||
|
|
||||||
// Lock stdout so that nobody can interfere
|
|
||||||
// with confirmation.
|
|
||||||
let mut stdout = io::stdout().lock();
|
|
||||||
|
|
||||||
let res = loop {
|
let res = loop {
|
||||||
writeln!(&mut stdout, "Do you wish to continue? yes/[no]").unwrap();
|
writeln!(&mut stdout, "Do you wish to continue? yes/[no]").unwrap();
|
||||||
write!(&mut stdout, "? ").unwrap();
|
write!(&mut stdout, "? ").unwrap();
|
||||||
|
@ -44,14 +52,22 @@ impl UIThreadInner {
|
||||||
|
|
||||||
match input.as_str().trim() {
|
match input.as_str().trim() {
|
||||||
"yes" | "y" | "YES" | "Y" => break Ok(()),
|
"yes" | "y" | "YES" | "Y" => break Ok(()),
|
||||||
"no" | "n" | "NO" | "N" | "" => break Err(BinstallError::UserAbort),
|
"no" | "n" | "NO" | "N" | "" => {
|
||||||
|
break Err(BinstallError::UserAbort)
|
||||||
|
}
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
confirm_tx
|
confirm_tx
|
||||||
.blocking_send(res)
|
.blocking_send(res)
|
||||||
.expect("entry exits when confirming request");
|
.expect("entry exits when confirming request")
|
||||||
|
}
|
||||||
|
Some(UIRequest::PrintToStdout(output)) => stdout.write_all(&output).unwrap(),
|
||||||
|
Some(UIRequest::PrintToStderr(output)) => stderr.write_all(&output).unwrap(),
|
||||||
|
Some(UIRequest::FlushStdout) => stdout.flush().unwrap(),
|
||||||
|
None => break,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -63,7 +79,7 @@ impl UIThreadInner {
|
||||||
|
|
||||||
async fn confirm(&mut self) -> Result<(), BinstallError> {
|
async fn confirm(&mut self) -> Result<(), BinstallError> {
|
||||||
self.request_tx
|
self.request_tx
|
||||||
.send(())
|
.send(UIRequest::Confirm)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| BinstallError::UserAbort)?;
|
.map_err(|_| BinstallError::UserAbort)?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue