treeflow 0.2.1

CLI tool for simplified Git worktree management to speed up switching contexts when working collaboratively.
Documentation
use treeflow::core::config::{get_config_dir, Config, ConfigPaths, WorkType};
use clap_complete::{engine::ValueCandidates, CompletionCandidate};
use std::ffi::OsString;
use std::fs;
use clap::{arg, Parser};

/// Dynamic completer for work type names from configuration
pub fn worktypes(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
    let current_str = current.to_string_lossy();
    let work_types = match load_work_types() {
        Some(work_types) => work_types,
        None => return Vec::new(), // Return empty if config unavailable
    };

    // Filter work types that start with current input and convert to CompletionCandidate
    let vec = work_types
        .into_iter()
        .filter(|work_type| work_type.starts_with(current_str.as_ref()))
        .map(|work_type| CompletionCandidate::new(work_type))
        .collect();
    vec
}

/// Dynamic completer for Git branch names
pub fn branches(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
    let current_str = current.to_string_lossy();
    let branches = match treeflow::utils::git::list_branches(current_str.as_ref()) {
        Ok(branches) => branches,
        Err(_) => return Vec::new(), // Return empty if Git command fails
    };

    // Filter branches that start with current input and convert to CompletionCandidate
    let vec = branches
        .into_iter()
        .map(|branch| CompletionCandidate::new(branch))
        .collect();

    vec
}

/// Dynamic completer of work names
pub fn work_names(current: &std::ffi::OsStr, prefix: &str) -> Vec<CompletionCandidate> {
    let current_str = current.to_string_lossy();
    let branches = match treeflow::utils::git::list_branches(format!("{}{}", prefix, current_str).as_str()) {
        Ok(branches) => branches,
        Err(_) => return Vec::new(), // Return empty if Git command fails
    };

    let vec = branches
        .into_iter()
        .map(|branch| CompletionCandidate::new(branch.as_str()[prefix.len()..].to_string()))
        .collect();

    vec
}

/// Load work type names from configuration
fn load_work_types() -> Option<Vec<String>> {
    load_config().map(|config| config.work_types.iter().map(|wt| wt.type_name.clone()).collect())
}

/// Load configuration from file
fn load_config() -> Option<Config> {
    let config_paths = get_config_dir().ok()?;
    Config::load(&config_paths).ok()
}