use super::{BackendKind, DaemonConfig};
use mineral_config::ConfigWarning;
use mineral_model::BitRate;
use std::path::Path;
pub fn render_check(
config: &DaemonConfig,
warnings: &[ConfigWarning],
default_download_dir: &Path,
color: bool,
) -> String {
let mut lines = Vec::<String>::new();
lines.push(paint("Mineral daemon configuration check", "1;36", color));
lines.push(format!(" volume: {}", config.audio().volume()));
lines.push(format!(
" audio backend: {}",
backend_name(config.audio().backend())
));
lines.push(format!(
" audio cache (bytes): {}",
config.cache().audio_capacity()
));
lines.push(format!(
" download quality: {}",
quality_name(config.download().quality())
));
lines.push(format!(
" download dir: {}",
dir_label(config, default_download_dir)
));
lines.push(format!(
" netease timeout (s): {}",
config.sources().netease().timeout_secs()
));
lines.push(format!(
" gapless prefetch (ms): {}",
config.gapless_prefetch_ms()
));
append_warnings(&mut lines, warnings, color);
lines.join("\n")
}
fn dir_label(config: &DaemonConfig, default_download_dir: &Path) -> String {
match config.download().dir() {
Some(path) => path.display().to_string(),
None => format!("{} (default)", default_download_dir.display()),
}
}
fn backend_name(backend: &BackendKind) -> &'static str {
match backend {
BackendKind::Auto => "auto",
BackendKind::Null => "null",
}
}
fn quality_name(quality: &BitRate) -> &'static str {
match quality {
BitRate::Standard => "standard",
BitRate::Higher => "higher",
BitRate::Exhigh => "exhigh",
BitRate::Lossless => "lossless",
BitRate::Hires => "hires",
}
}
fn append_warnings(lines: &mut Vec<String>, warnings: &[ConfigWarning], color: bool) {
if warnings.is_empty() {
lines.push(paint("Config OK, no warnings.", "32", color));
} else {
lines.push(paint(
&format!("{} warning(s):", warnings.len()),
"1;31",
color,
));
for warning in warnings {
lines.push(paint(&format!(" - {warning}"), "31", color));
}
}
}
fn paint(text: &str, code: &str, color: bool) -> String {
if color {
format!("\x1b[{code}m{text}\x1b[0m")
} else {
text.to_owned()
}
}