use crate::config::load_default_config;
use crate::error::SpriteError;
use crate::utils::project;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct WorkspaceNavigator {
pub project_root: PathBuf,
pub current_dir: PathBuf,
pub agent_workspaces: HashMap<String, PathBuf>,
pub main_workspace: PathBuf,
pub discovered_workspaces: HashMap<String, PathBuf>,
}
impl WorkspaceNavigator {
pub fn new() -> Result<Self> {
let project_root = project::find_project_root().context(
"Failed to locate Sprite project root. Run 'sprite init' in this repository first.",
)?;
let current_dir = env::current_dir().context("Failed to get current working directory")?;
let mut navigator = Self {
main_workspace: project_root.clone(),
project_root,
current_dir,
agent_workspaces: HashMap::new(),
discovered_workspaces: HashMap::new(),
};
navigator.load_agent_workspaces()?;
navigator.discover_workspaces()?;
Ok(navigator)
}
fn load_agent_workspaces(&mut self) -> Result<()> {
match load_default_config() {
Ok(config) => {
for agent in &config.agents {
if let Some(workspace) = &agent.worktree_path {
let resolved_path = if workspace.is_absolute() {
workspace.clone()
} else {
self.project_root.join(workspace)
};
self.agent_workspaces
.insert(agent.id.clone(), resolved_path);
}
}
}
Err(err) => {
eprintln!("â ī¸ Warning: Failed to load Sprite configuration: {}", err);
}
}
Ok(())
}
fn discover_workspaces(&mut self) -> Result<()> {
let agents_dir = self.project_root.join("agents");
if agents_dir.exists() && agents_dir.is_dir() {
let entries = std::fs::read_dir(&agents_dir).with_context(|| {
format!("Failed to read agents directory: {}", agents_dir.display())
})?;
for entry in entries {
let entry = entry.with_context(|| "Failed to read directory entry")?;
let path = entry.path();
if path.is_dir() {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if !self.agent_workspaces.contains_key(name) {
self.discovered_workspaces.insert(name.to_string(), path);
}
}
}
}
}
Ok(())
}
pub fn get_all_workspaces(&self) -> Vec<WorkspaceInfo> {
let mut workspaces = Vec::new();
workspaces.push(WorkspaceInfo {
name: "main".to_string(),
path: self.main_workspace.clone(),
workspace_type: WorkspaceType::Main,
exists: true,
current: self.current_dir == self.main_workspace,
relative_path: self.get_relative_path(&self.main_workspace),
});
for (id, path) in &self.agent_workspaces {
workspaces.push(WorkspaceInfo {
name: id.clone(),
path: path.clone(),
workspace_type: WorkspaceType::Agent,
exists: path.exists(),
current: self.current_dir == *path,
relative_path: self.get_relative_path(path),
});
}
for (name, path) in &self.discovered_workspaces {
workspaces.push(WorkspaceInfo {
name: name.clone(),
path: path.clone(),
workspace_type: WorkspaceType::Discovered,
exists: true,
current: self.current_dir == *path,
relative_path: self.get_relative_path(path),
});
}
workspaces.sort_by(|a, b| a.name.cmp(&b.name));
workspaces
}
pub fn find_workspace(&self, name: &str) -> Option<WorkspaceInfo> {
if let Some(path) = self.agent_workspaces.get(name) {
return Some(WorkspaceInfo {
name: name.to_string(),
path: path.clone(),
workspace_type: WorkspaceType::Agent,
exists: path.exists(),
current: self.current_dir == *path,
relative_path: self.get_relative_path(path),
});
}
if let Some(path) = self.discovered_workspaces.get(name) {
return Some(WorkspaceInfo {
name: name.to_string(),
path: path.clone(),
workspace_type: WorkspaceType::Discovered,
exists: true,
current: self.current_dir == *path,
relative_path: self.get_relative_path(path),
});
}
if name == "main" || name == "root" {
return Some(WorkspaceInfo {
name: "main".to_string(),
path: self.main_workspace.clone(),
workspace_type: WorkspaceType::Main,
exists: true,
current: self.current_dir == self.main_workspace,
relative_path: self.get_relative_path(&self.main_workspace),
});
}
let relative_path = self.project_root.join(name);
if relative_path.exists() {
return Some(WorkspaceInfo {
name: name.to_string(),
path: relative_path.clone(),
workspace_type: WorkspaceType::Path,
exists: true,
current: self.current_dir == relative_path,
relative_path: self.get_relative_path(&relative_path),
});
}
None
}
pub fn get_current_workspace(&self) -> Option<WorkspaceInfo> {
if self.current_dir == self.main_workspace {
return Some(WorkspaceInfo {
name: "main".to_string(),
path: self.main_workspace.clone(),
workspace_type: WorkspaceType::Main,
exists: true,
current: true,
relative_path: ".".to_string(),
});
}
for (id, path) in &self.agent_workspaces {
if self.current_dir == *path {
return Some(WorkspaceInfo {
name: id.clone(),
path: path.clone(),
workspace_type: WorkspaceType::Agent,
exists: true,
current: true,
relative_path: self.get_relative_path(path),
});
}
}
for (name, path) in &self.discovered_workspaces {
if self.current_dir == *path {
return Some(WorkspaceInfo {
name: name.clone(),
path: path.clone(),
workspace_type: WorkspaceType::Discovered,
exists: true,
current: true,
relative_path: self.get_relative_path(path),
});
}
}
if let Some(workspace) = self.find_parent_workspace() {
return Some(workspace);
}
None
}
fn find_parent_workspace(&self) -> Option<WorkspaceInfo> {
if self.current_dir.starts_with(&self.main_workspace) {
return Some(WorkspaceInfo {
name: "main".to_string(),
path: self.main_workspace.clone(),
workspace_type: WorkspaceType::Main,
exists: true,
current: false,
relative_path: self.get_relative_path(&self.main_workspace),
});
}
for (id, path) in &self.agent_workspaces {
if self.current_dir.starts_with(path) {
return Some(WorkspaceInfo {
name: id.clone(),
path: path.clone(),
workspace_type: WorkspaceType::Agent,
exists: true,
current: false,
relative_path: self.get_relative_path(path),
});
}
}
None
}
pub fn get_relative_path(&self, target: &Path) -> String {
match pathdiff::diff_paths(target, &self.current_dir) {
Some(relative) => relative.to_string_lossy().to_string(),
None => target.display().to_string(),
}
}
#[allow(dead_code)]
pub fn workspace_exists(&self, name: &str) -> bool {
self.find_workspace(name).is_some_and(|w| w.exists)
}
#[allow(dead_code)]
pub fn get_navigation_command(&self, workspace_name: &str) -> Result<String> {
let workspace = self.find_workspace(workspace_name).ok_or_else(|| {
SpriteError::validation(
format!("Workspace '{}' not found", workspace_name),
Some("workspace".to_string()),
Some(workspace_name.to_string()),
)
})?;
if !workspace.exists {
return Err(SpriteError::filesystem(
format!(
"Workspace path does not exist: {}",
workspace.path.display()
),
workspace_name.to_string(),
)
.into());
}
Ok(format!("cd {}", workspace.path.display()))
}
pub fn navigate_to_workspace(&self, workspace_name: &str) -> Result<NavigationResult> {
let workspace = self.find_workspace(workspace_name).ok_or_else(|| {
SpriteError::validation(
format!("Workspace '{}' not found", workspace_name),
Some("workspace".to_string()),
Some(workspace_name.to_string()),
)
})?;
if !workspace.exists {
return Err(SpriteError::filesystem(
format!(
"Workspace path does not exist: {}",
workspace.path.display()
),
workspace_name.to_string(),
)
.into());
}
let current = self.get_current_workspace();
Ok(NavigationResult {
workspace: workspace.clone(),
current_workspace: current,
command: format!("cd {}", workspace.path.display()),
relative_command: format!("cd {}", workspace.relative_path),
})
}
#[allow(dead_code)]
pub fn generate_shell_integration(&self) -> String {
r#"# Sprite workspace navigation integration
# Add this to your shell configuration (.bashrc, .zshrc, etc.)
# Function to navigate to sprite workspaces
sprite_warp() {
local workspace="$1"
local cmd
cmd=$(sprite warp --print "$workspace" 2>/dev/null)
if [ $? -eq 0 ]; then
cd "$cmd"
else
echo "Error: $cmd"
return 1
fi
}
# Tab completion for sprite warp
_sprite_warp_completion() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local workspaces
workspaces=$(sprite warp --list 2>/dev/null | grep "đ" | awk '{print $3}' | sed 's/://')
COMPREPLY=($(compgen -W "$workspaces" -- "$cur"))
}
# Register completion
complete -F _sprite_warp_completion sprite_warp
complete -F _sprite_warp_completion sprite warp
# Alias for convenience
alias sw='sprite_warp'
"#
.to_string()
}
#[allow(dead_code)]
pub fn print_workspace_status(&self) -> Result<()> {
let current = self.get_current_workspace();
let all_workspaces = self.get_all_workspaces();
println!("đī¸ Workspace Status");
println!("====================");
if let Some(current) = ¤t {
println!("đ Current: {} ({})", current.name, current.path.display());
if current.workspace_type != WorkspaceType::Main {
println!(" Type: {:?}", current.workspace_type);
}
} else {
println!(
"đ Current: {} (outside any workspace)",
self.current_dir.display()
);
}
println!();
println!("đ Available Workspaces:");
for workspace in &all_workspaces {
let status = if workspace.current {
"đĸ current"
} else if workspace.exists {
"âĒ available"
} else {
"â missing"
};
let workspace_type = match workspace.workspace_type {
WorkspaceType::Main => "main",
WorkspaceType::Agent => "agent",
WorkspaceType::Discovered => "discovered",
WorkspaceType::Path => "path",
};
println!(
" {} {} ({}) - {}",
status, workspace.name, workspace_type, workspace.relative_path
);
}
println!();
println!("đĄ Navigation:");
println!(" sprite warp <name> # Navigate to workspace");
println!(" sprite warp --list # List all workspaces");
println!(" sprite warp --print <name> # Print path without changing");
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct WorkspaceInfo {
pub name: String,
pub path: PathBuf,
pub workspace_type: WorkspaceType,
pub exists: bool,
pub current: bool,
pub relative_path: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WorkspaceType {
Main,
Agent,
Discovered,
Path,
}
#[derive(Debug, Clone)]
pub struct NavigationResult {
pub workspace: WorkspaceInfo,
#[allow(dead_code)]
pub current_workspace: Option<WorkspaceInfo>,
pub command: String,
#[allow(dead_code)]
pub relative_command: String,
}
#[allow(dead_code)]
pub fn get_workspace_navigator() -> Result<WorkspaceNavigator> {
WorkspaceNavigator::new()
}
#[allow(dead_code)]
pub fn quick_warp(workspace_name: &str) -> Result<()> {
let navigator = WorkspaceNavigator::new()?;
let result = navigator.navigate_to_workspace(workspace_name)?;
println!("đ Navigating to workspace: {}", result.workspace.name);
println!("đ Target: {}", result.workspace.path.display());
println!("â¨ī¸ Command: {}", result.command);
if result.workspace.current {
println!("âšī¸ Already in this workspace");
} else {
println!("đĄ Execute: cd $(sprite warp --print {})", workspace_name);
}
Ok(())
}
#[allow(dead_code)]
pub fn list_workspaces_detailed() -> Result<()> {
let navigator = WorkspaceNavigator::new()?;
navigator.print_workspace_status()
}
#[allow(dead_code)]
pub fn generate_workspace_aliases() -> Result<String> {
let navigator = WorkspaceNavigator::new()?;
let workspaces = navigator.get_all_workspaces();
let mut aliases = String::new();
aliases.push_str("# Sprite workspace aliases\n");
aliases.push_str("# Generated automatically - add to your shell config\n\n");
for workspace in workspaces {
if workspace.exists {
let alias_name = workspace.name.replace('-', "_");
aliases.push_str(&format!(
"alias sw{}='cd {}'\n",
alias_name,
workspace.path.display()
));
}
}
Ok(aliases)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_workspace_navigator_creation() {
}
#[test]
fn test_workspace_info_creation() {
let workspace = WorkspaceInfo {
name: "test".to_string(),
path: PathBuf::from("/test/path"),
workspace_type: WorkspaceType::Agent,
exists: true,
current: false,
relative_path: "../test".to_string(),
};
assert_eq!(workspace.name, "test");
assert_eq!(workspace.workspace_type, WorkspaceType::Agent);
assert!(workspace.exists);
assert!(!workspace.current);
}
#[test]
fn test_navigation_result() {
let workspace = WorkspaceInfo {
name: "test".to_string(),
path: PathBuf::from("/test/path"),
workspace_type: WorkspaceType::Agent,
exists: true,
current: false,
relative_path: "../test".to_string(),
};
let result = NavigationResult {
workspace: workspace.clone(),
current_workspace: None,
command: "cd /test/path".to_string(),
relative_command: "cd ../test".to_string(),
};
assert_eq!(result.workspace.name, "test");
assert_eq!(result.command, "cd /test/path");
assert_eq!(result.relative_command, "cd ../test");
}
}