fine-grained stdout

This commit is contained in:
TD-Sky 2023-08-14 18:55:55 +08:00 committed by sxyazi
parent 789e955bd9
commit ec790dcad2
No known key found for this signature in database

View file

@ -1,4 +1,4 @@
use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, ffi::OsStr, io::{stdout, Write}, mem, os::unix::prelude::OsStrExt, path::{Path, PathBuf}}; use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, ffi::OsStr, io::{stdout, BufWriter, Write}, mem, os::unix::prelude::OsStrExt, path::{Path, PathBuf}};
use anyhow::{bail, Error, Result}; use anyhow::{bail, Error, Result};
use config::{open::Opener, BOOT, OPEN}; use config::{open::Opener, BOOT, OPEN};
@ -281,9 +281,15 @@ impl Manager {
} }
if todo.is_empty() { if todo.is_empty() {
return Ok(()); return Ok(());
} else { }
print!("Continue to rename? (y/N): "); // NOTE: it's worth using two iterations for finer-grained stdout control
stdout().flush()?; {
let mut stdout = BufWriter::new(stdout().lock());
for (o, n) in &todo {
writeln!(stdout, "{o:?} -> {n:?}")?;
}
write!(stdout, "Continue to rename? (y/N): ")?;
stdout.flush()?;
} }
let mut buf = [0]; let mut buf = [0];
@ -294,23 +300,26 @@ impl Manager {
let mut failed = Vec::new(); let mut failed = Vec::new();
for (o, n) in todo { for (o, n) in todo {
let (o, n) = root.as_ref().map(|root| (root.join(&o), root.join(&n))).unwrap_or((o, n));
if let Err(e) = fs::rename(&o, &n).await { if let Err(e) = fs::rename(&o, &n).await {
failed.push((o, n, e)); failed.push((o, n, e));
} }
} }
{
let mut stdout = BufWriter::new(stdout().lock());
if !failed.is_empty() { if !failed.is_empty() {
Term::clear()?; Term::clear()?;
println!("Failed to rename:"); writeln!(stdout, "Failed to rename:")?;
for (o, n, e) in failed { for (o, n, e) in failed {
stdout().write_all(o.as_os_str().as_bytes())?; writeln!(stdout, "{o:?} -> {n:?}: {e}")?;
stdout().write_all(b" -> ")?; }
stdout().write_all(n.as_os_str().as_bytes())?; write!(stdout, "\nPress ENTER to exit")?;
stdout().write_fmt(format_args!(": {e}\n"))?; stdout.flush()?;
}
} }
println!("\nPress ENTER to exit");
tokio::io::stdin().read_exact(&mut [0]).await?; tokio::io::stdin().read_exact(&mut [0]).await?;
}
Ok(()) Ok(())
} }