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};
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(), };
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
}
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(), };
let vec = branches
.into_iter()
.map(|branch| CompletionCandidate::new(branch))
.collect();
vec
}
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(), };
let vec = branches
.into_iter()
.map(|branch| CompletionCandidate::new(branch.as_str()[prefix.len()..].to_string()))
.collect();
vec
}
fn load_work_types() -> Option<Vec<String>> {
load_config().map(|config| config.work_types.iter().map(|wt| wt.type_name.clone()).collect())
}
fn load_config() -> Option<Config> {
let config_paths = get_config_dir().ok()?;
Config::load(&config_paths).ok()
}