bckplayer 0.1.0

A cross-platform CLI media player interface for VLC
Documentation
use anyhow::Result;
use std::path::PathBuf;
use std::process::Command;

pub fn find_vlc_executable() -> Result<PathBuf> {
    // Common VLC installation paths
    let possible_paths = if cfg!(target_os = "windows") {
        vec![
            PathBuf::from("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"),
            PathBuf::from("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"),
            PathBuf::from("vlc.exe"), // Check PATH
        ]
    } else if cfg!(target_os = "macos") {
        vec![
            PathBuf::from("/Applications/VLC.app/Contents/MacOS/VLC"),
            PathBuf::from("/usr/local/bin/vlc"),
            PathBuf::from("vlc"), // Check PATH
        ]
    } else {
        vec![
            PathBuf::from("/usr/bin/vlc"),
            PathBuf::from("/usr/local/bin/vlc"),
            PathBuf::from("/snap/bin/vlc"),
            PathBuf::from("vlc"), // Check PATH
        ]
    };

    for path in possible_paths {
        if path.file_name().is_some_and(|name| name == "vlc" || name == "vlc.exe") {
            // This is just "vlc" or "vlc.exe", check if it's in PATH
            if let Ok(output) = Command::new(&path).arg("--version").output() {
                if output.status.success() {
                    return Ok(path);
                }
            }
        } else if path.exists() {
            // This is a full path, check if it works
            if let Ok(output) = Command::new(&path).arg("--version").output() {
                if output.status.success() {
                    return Ok(path);
                }
            }
        }
    }

    Err(anyhow::anyhow!(
        "VLC not found. Please install VLC media player or ensure it's in your PATH."
    ))
}

pub fn is_url(input: &str) -> bool {
    input.starts_with("http://") 
        || input.starts_with("https://") 
        || input.starts_with("youtube.com/")
        || input.starts_with("youtu.be/")
        || input.starts_with("www.youtube.com/")
        || input.starts_with("m.youtube.com/")
        || input.starts_with("youtube-nocookie.com/")
}

pub fn is_youtube_url(input: &str) -> bool {
    is_url(input) && (
        input.contains("youtube.com") ||
        input.contains("youtu.be") ||
        input.contains("youtube-nocookie.com") ||
        input.contains("www.youtube.com") ||
        input.contains("m.youtube.com")
    )
}

pub fn format_duration(seconds: f64) -> String {
    let total_seconds = seconds as u64;
    let hours = total_seconds / 3600;
    let minutes = (total_seconds % 3600) / 60;
    let secs = total_seconds % 60;

    if hours > 0 {
        format!("{:02}:{:02}:{:02}", hours, minutes, secs)
    } else {
        format!("{:02}:{:02}", minutes, secs)
    }
}

pub fn format_volume(volume: f64, muted: bool) -> String {
    if muted {
        format!("Volume: MUTED ({}%)", volume as i32)
    } else {
        format!("Volume: {}%", volume as i32)
    }
}

pub fn format_speed(speed: f64) -> String {
    format!("Speed: {:.1}x", speed)
}

pub fn format_loop_status(loop_enabled: bool) -> String {
    if loop_enabled {
        "Loop: ON".to_string()
    } else {
        "Loop: OFF".to_string()
    }
}