blob: 791600e8664d37530c6f98b4c18f38a836bb173a [file] [log] [blame]
package main
func truncateStringWithEllipsis(s string, maxChars int) string {
if len(s) <= maxChars {
return s
}
// Find the last space within the maximum character limit
i := maxChars - 1
for ; i >= 0; i-- {
if s[i] == ' ' {
break
}
}
// If no space found, truncate to the maximum length minus the ellipsis length
if i < 0 {
return s[:maxChars-1] + "…"
}
// Truncate at the last space found
return s[:i] + "…"
}