fix: Refactor string truncation logic into reusable utility function to avoid panic (#2818) (#2819)

Signed-off-by: toyamagu2021 <tomoki-yamaguchi@c-fo.com>
This commit is contained in:
toyamagu-2021
2025-07-04 02:22:45 +09:00
committed by GitHub
parent fd0f3bc81d
commit ff4c7d575a
4 changed files with 56 additions and 15 deletions
+2 -5
View File
@@ -4,6 +4,7 @@ use cliclack::{self, intro, outro};
use std::path::Path;
use crate::project_tracker::ProjectTracker;
use crate::utils::safe_truncate;
/// Format a DateTime for display
fn format_date(date: DateTime<chrono::Utc>) -> String {
@@ -199,11 +200,7 @@ pub fn handle_projects_interactive() -> Result<()> {
.last_instruction
.as_ref()
.map_or(String::new(), |instr| {
let truncated = if instr.len() > 40 {
format!("{}...", &instr[0..37])
} else {
instr.clone()
};
let truncated = safe_truncate(instr, 40);
format!(" [{}]", truncated)
});
+3 -10
View File
@@ -1,4 +1,5 @@
use crate::session::message_to_markdown;
use crate::utils::safe_truncate;
use anyhow::{Context, Result};
use cliclack::{confirm, multiselect, select};
use goose::session::info::{get_valid_sorted_sessions, SessionInfo, SortOrder};
@@ -50,11 +51,7 @@ fn prompt_interactive_session_removal(sessions: &[SessionInfo]) -> Result<Vec<Se
} else {
&s.metadata.description
};
let truncated_desc = if desc.len() > TRUNCATED_DESC_LENGTH {
format!("{}...", &desc[..TRUNCATED_DESC_LENGTH - 3])
} else {
desc.to_string()
};
let truncated_desc = safe_truncate(desc, TRUNCATED_DESC_LENGTH);
let display_text = format!("{} - {} ({})", s.modified, truncated_desc, s.id);
(display_text, s.clone())
})
@@ -320,11 +317,7 @@ pub fn prompt_interactive_session_selection() -> Result<session::Identifier> {
};
// Truncate description if too long
let truncated_desc = if desc.len() > 40 {
format!("{}...", &desc[..37])
} else {
desc.to_string()
};
let truncated_desc = safe_truncate(desc, 40);
let display_text = format!("{} - {} ({})", s.modified, truncated_desc, s.id);
(display_text, s.clone())