use std::path::Path;
use std::process::Command;
use crate::error::Result;
pub fn complete_bookmarks(cwd: &Path) -> Result<Vec<String>> {
let out = Command::new("jj")
.args([
"bookmark",
"list",
"-T",
"name ++ \"\\n\"",
"--ignore-working-copy",
])
.current_dir(cwd)
.output()?;
if !out.status.success() {
return Ok(vec![]);
}
Ok(parse_lines(&out.stdout))
}
pub fn complete_remotes(cwd: &Path) -> Result<Vec<String>> {
let out = Command::new("jj")
.args(["git", "remote", "list", "--ignore-working-copy"])
.current_dir(cwd)
.output()?;
if !out.status.success() {
return Ok(vec![]);
}
let text = String::from_utf8_lossy(&out.stdout);
Ok(text
.lines()
.filter_map(|line| line.split_whitespace().next().map(|s| s.to_owned()))
.filter(|s| !s.is_empty())
.collect())
}
fn parse_lines(bytes: &[u8]) -> Vec<String> {
String::from_utf8_lossy(bytes)
.lines()
.map(|l| l.trim().to_owned())
.filter(|l| !l.is_empty())
.collect()
}
pub fn bookmark_value_completer(
current: &std::ffi::OsStr,
) -> Vec<clap_complete::CompletionCandidate> {
let prefix = current.to_string_lossy();
let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());
complete_bookmarks(&cwd)
.unwrap_or_default()
.into_iter()
.filter(|name| name.starts_with(prefix.as_ref()))
.map(clap_complete::CompletionCandidate::new)
.collect()
}
pub fn remote_value_completer(
current: &std::ffi::OsStr,
) -> Vec<clap_complete::CompletionCandidate> {
let prefix = current.to_string_lossy();
let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());
complete_remotes(&cwd)
.unwrap_or_default()
.into_iter()
.filter(|name| name.starts_with(prefix.as_ref()))
.map(clap_complete::CompletionCandidate::new)
.collect()
}