use anyhow::Result;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeOrigin {
Cwd,
ExplicitWorkspace,
ExplicitProject(String),
}
pub fn resolve(workspace: Option<&Path>, all_workspaces: bool) -> Result<Vec<PathBuf>> {
let primary = crate::core::workspace::resolve(workspace)?;
if all_workspaces {
return crate::core::workspace::machine_workspaces(Some(&primary));
}
Ok(vec![primary])
}
pub fn label(roots: &[PathBuf]) -> String {
if roots.len() == 1 {
return roots[0].to_string_lossy().to_string();
}
format!("machine:{} workspaces", roots.len())
}
pub fn decorate_path(workspace: &Path, path: &str) -> String {
format!("{}:{}", workspace.to_string_lossy(), path)
}
pub fn scope_header(ws: &Path, origin: &ScopeOrigin) -> Option<String> {
match origin {
ScopeOrigin::ExplicitWorkspace | ScopeOrigin::ExplicitProject(_) => {
let name = ws.file_name().and_then(|n| n.to_str()).unwrap_or("?");
Some(format!("scope: {} ({})", name, ws.display()))
}
ScopeOrigin::Cwd => {
let registered = crate::core::machine_registry::list_paths()
.unwrap_or_default()
.into_iter()
.any(|p| p == ws);
if registered {
None
} else {
Some(format!("scope: {} ({})", ws.display(), "unregistered cwd"))
}
}
}
}