use crate::parser::{detect_dialect_from_file, DialectConfidence, SqlDialect};
use indicatif::{ProgressBar, ProgressStyle};
use std::path::{Path, PathBuf};
use std::time::Duration;
pub(crate) const INPUT_OUTPUT: &str = "Input/Output";
pub(crate) const FILTERING: &str = "Filtering";
pub(crate) const MODE: &str = "Mode";
pub(crate) const BEHAVIOR: &str = "Behavior";
pub(crate) const LIMITS: &str = "Limits";
pub(crate) const OUTPUT_FORMAT: &str = "Output";
pub(crate) fn dash_is_stdout(output: Option<PathBuf>) -> Option<PathBuf> {
output.filter(|p| p.as_os_str() != "-")
}
pub(crate) fn byte_progress_bar(len: u64) -> ProgressBar {
let pb = ProgressBar::new(len);
pb.set_style(
ProgressStyle::with_template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({percent}%) {msg}",
)
.expect("progress template is static and valid")
.progress_chars("█▓▒░ ")
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
);
pb.enable_steady_tick(Duration::from_millis(100));
pb
}
pub(crate) fn compact_progress_bar(len: u64) -> ProgressBar {
let pb = ProgressBar::new(len);
pb.set_style(
ProgressStyle::with_template(
" {spinner:.green} [{bar:30.cyan/blue}] {bytes}/{total_bytes} ({percent}%)",
)
.expect("progress template is static and valid")
.progress_chars("█▓▒░ "),
);
pb.enable_steady_tick(Duration::from_millis(100));
pb
}
pub(crate) fn resolve_dialect_with_confidence(
file: &Path,
explicit: Option<&str>,
quiet: bool,
) -> anyhow::Result<(SqlDialect, DialectConfidence)> {
match explicit {
Some(d) => {
let parsed: SqlDialect = d.parse().map_err(|e: String| anyhow::anyhow!(e))?;
Ok((parsed, DialectConfidence::High))
}
None => {
let result = detect_dialect_from_file(file)?;
if !quiet {
eprintln!(
"Auto-detected dialect: {} ({})",
result.dialect,
confidence_label(result.confidence)
);
}
Ok((result.dialect, result.confidence))
}
}
}
pub(crate) fn resolve_dialect(
file: &Path,
explicit: Option<&str>,
quiet: bool,
) -> anyhow::Result<SqlDialect> {
resolve_dialect_with_confidence(file, explicit, quiet).map(|(dialect, _)| dialect)
}
pub(crate) fn confidence_label(confidence: DialectConfidence) -> &'static str {
match confidence {
DialectConfidence::High => "high confidence",
DialectConfidence::Medium => "medium confidence",
DialectConfidence::Low => "low confidence",
}
}