pub(super) fn truncate_title(s: &str, max: usize) -> &str {
if s.len() <= max {
return s;
}
let boundary = s[..=max].rfind(|c: char| c.is_whitespace()).unwrap_or(max);
&s[..boundary]
}
pub(super) fn build_title(prompt: Option<&str>, fallback_name: &str) -> String {
prompt
.map(|p| truncate_title(p.trim(), 72).to_string())
.filter(|t| !t.is_empty())
.unwrap_or_else(|| format!("codetether: {fallback_name}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn truncate_title_short_input_unchanged() {
assert_eq!(truncate_title("fix bug", 72), "fix bug");
}
#[test]
fn truncate_title_long_input_on_word() {
let long = "fix the login bug that causes users to see a blank screen after submitting";
let result = truncate_title(long, 40);
assert!(result.len() <= 40);
assert!(!result.ends_with(' '));
}
}