Skip to content
This repository was archived by the owner on Nov 3, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 65 additions & 104 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,49 @@ pub fn add_aliases_to_global_looprc() -> Result<()> {
Ok(())
}

/// How a directory is named in progress and result lines: `.` for the root,
/// `./open-source/meta/loop_cli` for a directory under it, so repos that share
/// a basename stay distinguishable, and the full path for one outside it.
/// Without a root, the basename.
fn display_name(dir: &Path, root: Option<&Path>) -> String {
let Some(root) = root else {
return dir
.file_name()
.and_then(|n| n.to_str())
.filter(|s| !s.is_empty())
.unwrap_or(".")
.to_string();
};
match dir.strip_prefix(root) {
Ok(rel) if rel.as_os_str().is_empty() => ".".to_string(),
Ok(rel) => {
let parts: Vec<_> = rel
.components()
.map(|c| c.as_os_str().to_string_lossy())
.collect();
format!("./{}", parts.join("/"))
}
Err(_) => dir.display().to_string(),
}
}

/// [`display_name`], with the root's folder name added: `. (harmony)`.
fn result_name(dir: &Path, root: Option<&Path>) -> String {
let name = display_name(dir, root);
if name != "." {
return name;
}
let base = if dir == Path::new(".") {
env::current_dir().ok()
} else {
Some(dir.to_path_buf())
};
match base.as_deref().and_then(Path::file_name) {
Some(base) => format!(". ({})", base.to_string_lossy()),
None => name,
}
}

pub fn execute_command_in_directory(
dir: &Path,
command: &str,
Expand All @@ -232,10 +275,11 @@ pub fn execute_command_in_directory(
) -> CommandResult {
if !dir.exists() {
println!("\nNo directory found for {}", dir.display());
let dir_name = dir.file_name().unwrap_or_default().to_str().unwrap();
println!(
"\x1b[31m\n✗ {}: No directory found. Command: {} (Exit code: {})\x1b[0m",
dir_name, command, 1
display_name(dir, config.root_dir.as_deref()),
command,
1
);
return CommandResult {
success: false,
Expand Down Expand Up @@ -331,43 +375,11 @@ pub fn execute_command_in_directory(
let success = status.success();

if !config.silent {
// Check if this directory is the root_dir (should display as ".")
let is_root = config
.root_dir
.as_ref()
.is_some_and(|root| dir == root.as_path());
let dir_name = if is_root {
"."
} else {
dir.file_name()
.and_then(|name| name.to_str())
.filter(|&s| !s.is_empty())
.unwrap_or(".")
};
let name = result_name(dir, config.root_dir.as_deref());
if success {
if is_root {
// Display root as ". (basename)"
if let Some(base) = dir.file_name().and_then(|s| s.to_str()) {
println!("\x1b[32m\n✓ . ({base})\x1b[0m");
} else {
println!("\x1b[32m\n✓ .\x1b[0m");
}
} else if dir_name == "." {
// Fallback for literal "." paths
if let Ok(cwd) = std::env::current_dir() {
if let Some(base) = cwd.file_name().and_then(|s| s.to_str()) {
println!("\x1b[32m\n✓ . ({base})\x1b[0m");
} else {
println!("\x1b[32m\n✓ .\x1b[0m");
}
} else {
println!("\x1b[32m\n✓ .\x1b[0m");
}
} else {
println!("\x1b[32m\n✓ {dir_name}\x1b[0m");
}
println!("\x1b[32m\n✓ {name}\x1b[0m");
} else {
println!("\x1b[31m\n✗ {dir_name}: exited code {exit_code}\x1b[0m");
println!("\x1b[31m\n✗ {name}: exited code {exit_code}\x1b[0m");
}
io::stdout().flush().unwrap();
}
Expand Down Expand Up @@ -624,17 +636,8 @@ fn execute_commands_internal(config: &LoopConfig, commands: &[DirCommand]) -> Re
let pb = mp.add(ProgressBar::new_spinner());
pb.set_style(spinner_style.clone());
pb.set_prefix(format!("[{}/{}]", i + 1, total));
let dir_path = PathBuf::from(&dir_cmd.dir);
let is_root = config.root_dir.as_ref().is_some_and(|r| dir_path == *r);
let dir_name = if is_root {
".".to_string()
} else {
dir_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(".")
.to_string()
};
let dir_name =
display_name(Path::new(&dir_cmd.dir), config.root_dir.as_deref());
pb.set_message(format!("{dir_name}: pending..."));
pb.enable_steady_tick(Duration::from_millis(100));
Some(pb)
Expand Down Expand Up @@ -663,15 +666,7 @@ fn execute_commands_internal(config: &LoopConfig, commands: &[DirCommand]) -> Re
}

let dir = PathBuf::from(&dir_cmd.dir);
let is_root = config.root_dir.as_ref().is_some_and(|r| dir == *r);
let dir_name = if is_root {
".".to_string()
} else {
dir.file_name()
.and_then(|n| n.to_str())
.unwrap_or(".")
.to_string()
};
let dir_name = display_name(&dir, config.root_dir.as_deref());

// Update spinner to show running
if let Some(ref pb) = spinners[i] {
Expand Down Expand Up @@ -713,33 +708,13 @@ fn execute_commands_internal(config: &LoopConfig, commands: &[DirCommand]) -> Re
execute_parallel()
};

// Store results (already collected from rayon), sorted for deterministic output
// Sort by directory name: root_dir first (displayed as "."), then alphabetical
// Store results (already collected from rayon), sorted for deterministic output:
// the root (".") first, then by displayed path, component by component so
// nested repos sit under their parent (`a/b` before `a-b`)
let mut sorted_results = parallel_results;
sorted_results.sort_by(|a, b| {
let a_is_root = config.root_dir.as_ref().is_some_and(|r| a.directory == *r);
let b_is_root = config.root_dir.as_ref().is_some_and(|r| b.directory == *r);
let a_name = a
.directory
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(".");
let b_name = b
.directory
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(".");
match (a_is_root, b_is_root) {
(true, true) => std::cmp::Ordering::Equal,
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => match (a_name, b_name) {
(".", ".") => std::cmp::Ordering::Equal,
(".", _) => std::cmp::Ordering::Less,
(_, ".") => std::cmp::Ordering::Greater,
_ => a_name.cmp(b_name),
},
}
sorted_results.sort_by_cached_key(|r| {
let name = display_name(&r.directory, config.root_dir.as_deref());
(name != ".", PathBuf::from(name))
});
results
.lock()
Expand All @@ -763,26 +738,7 @@ fn execute_commands_internal(config: &LoopConfig, commands: &[DirCommand]) -> Re
}

for result in results.iter() {
// Check if this directory is the root_dir (should display as ".")
let is_root = config
.root_dir
.as_ref()
.is_some_and(|r| result.directory == *r);
let dir_name = if is_root {
// Display root as ". (basename)"
if let Some(base) = result.directory.file_name().and_then(|s| s.to_str()) {
format!(". ({base})")
} else {
".".to_string()
}
} else {
result
.directory
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(".")
.to_string()
};
let dir_name = result_name(&result.directory, config.root_dir.as_deref());

let has_output =
!result.stdout.trim().is_empty() || !result.stderr.trim().is_empty();
Expand Down Expand Up @@ -883,10 +839,15 @@ fn execute_commands_internal(config: &LoopConfig, commands: &[DirCommand]) -> Re
total
);
for result in &failed {
// Without a root the basename is ambiguous, so keep the full path
let name = match config.root_dir.as_deref() {
Some(root) => result_name(&result.directory, Some(root)),
None => result.directory.display().to_string(),
};
println!(
"\n{} {}: {} (Exit code {}) ",
"✗".red(),
result.directory.display(),
name,
result.command,
result.exit_code
);
Expand Down
25 changes: 25 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,28 @@ fn test_dir_command_serialization() {
assert_eq!(parsed.dir, "/path/to/dir");
assert_eq!(parsed.cmd, "git status");
}

#[test]
fn test_display_name_is_path_from_root() {
let root = Path::new("/ws/harmony");
assert_eq!(display_name(root, Some(root)), ".");
assert_eq!(
display_name(
Path::new("/ws/harmony/open-source/meta/loop_cli"),
Some(root)
),
"./open-source/meta/loop_cli"
);
// Two repos with the same basename stay distinguishable
assert_ne!(
display_name(Path::new("/ws/harmony/agent"), Some(root)),
display_name(Path::new("/ws/harmony/open-source/meta/agent"), Some(root))
);
// Outside the root, the full path; without a root, the basename
assert_eq!(
display_name(Path::new("/ws/other/repo"), Some(root)),
Path::new("/ws/other/repo").display().to_string()
);
assert_eq!(display_name(Path::new("./repo"), None), "repo");
assert_eq!(result_name(root, Some(root)), ". (harmony)");
}
Loading