feat: prompts, dir structure, etc.
This commit is contained in:
parent
fc3ae1e71a
commit
89e62ad9c7
7 changed files with 94 additions and 20 deletions
19
pkgr/src/util/mod.rs
Normal file
19
pkgr/src/util/mod.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use std::fs::DirEntry;
|
||||
use std::path::Path;
|
||||
|
||||
pub mod prompts;
|
||||
|
||||
pub fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> std::io::Result<()> {
|
||||
if dir.is_dir() {
|
||||
for entry in std::fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
visit_dirs(&path, cb)?;
|
||||
} else {
|
||||
cb(&entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
32
pkgr/src/util/prompts.rs
Normal file
32
pkgr/src/util/prompts.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
use std::io::Write;
|
||||
use log::trace;
|
||||
|
||||
pub fn is_noninteractive() -> bool {
|
||||
if let Ok(v) = std::env::var("PKGR_NON_INTERACTIVE") {
|
||||
trace!("PKGR_NON_INTERACTIVE={}", v);
|
||||
match v.as_str() {
|
||||
"1" => true,
|
||||
"true" => true,
|
||||
_ => false
|
||||
}
|
||||
} else { false }
|
||||
}
|
||||
|
||||
pub fn prompt_bool<S: Into<String>>(prompt: S, default: bool) -> bool {
|
||||
if is_noninteractive() { return default; }
|
||||
print!("{} [{}]: ", prompt.into(), if default { "Y/n" } else { "y/N" });
|
||||
match std::io::stdout().flush() {
|
||||
Ok(_) => (),
|
||||
_ => println!()
|
||||
};
|
||||
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).expect("Failed to read input.");
|
||||
let answer = input.trim().to_lowercase();
|
||||
|
||||
if answer.is_empty() {
|
||||
default
|
||||
} else {
|
||||
answer == "y" || answer == "yes"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue