43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use console::Term;
|
|
|
|
pub fn yesno(prompt: &str) -> bool {
|
|
eprint!("{prompt} [y/N] ");
|
|
["y", "Y"].contains(
|
|
&std::io::stdin()
|
|
.lines()
|
|
.next()
|
|
.transpose()
|
|
.unwrap_or_default()
|
|
.unwrap_or_default()
|
|
.trim(),
|
|
)
|
|
}
|
|
|
|
pub fn print_list(indent: u32, list: impl Iterator<Item = impl AsRef<str>>) {
|
|
let (row, _) = Term::stdout().size();
|
|
let mut line_used: usize = 0;
|
|
for i in list {
|
|
let mut i = i.as_ref().to_string();
|
|
if line_used >= row as _ {
|
|
line_used = 0;
|
|
println!();
|
|
}
|
|
if i.contains(' ') {
|
|
i = format!("'{i}'");
|
|
}
|
|
let to_print = format!("{}{}", " ".repeat(indent as _), i);
|
|
print!("{to_print}");
|
|
line_used += to_print.len();
|
|
}
|
|
println!();
|
|
}
|
|
|
|
pub fn format_size(bytes: u64) -> String {
|
|
match bytes {
|
|
..1000 => format!("{bytes} B"),
|
|
..1_000_000 => format!("{} kB", bytes / 1000),
|
|
..1_000_000_000 => format!("{:.2} MB", (bytes as f64) / 1000000.),
|
|
_ => format!("{:.3} GB", (bytes as f64) / 1000000000.),
|
|
}
|
|
}
|