diff --git a/helpers.go b/helpers.go index 533a8ce..d6feed8 100644 --- a/helpers.go +++ b/helpers.go @@ -18,6 +18,7 @@ import ( "sync" "time" "unicode" + "unicode/utf8" "fiatjaf.com/nostr" "fiatjaf.com/nostr/nip05" @@ -621,11 +622,14 @@ func editWithDefaultEditor(filename string, initialContent string, wipe bool) (s return string(data), nil } +// clampWithEllipsis shortens s to at most size characters. it counts runes and +// not bytes, otherwise it would cut multibyte characters in half. func clampWithEllipsis(s string, size int) string { - if len(s) <= size { + if utf8.RuneCountInString(s) <= size { return s } - return s[0:size-1] + "…" + runes := []rune(s) + return string(runes[0:size-1]) + "…" } var ( diff --git a/podcast.go b/podcast.go index b9cf852..d61e79a 100644 --- a/podcast.go +++ b/podcast.go @@ -593,10 +593,7 @@ func printPodcastEpisodeList(ctx context.Context, p podcastInfo, limit int) erro title = ep.ID.Hex()[:16] } date := ep.CreatedAt.Time().Format("2006-01-02") - desc := ep.Content - if len(desc) > 100 { - desc = desc[:100] + "..." - } + desc := clampWithEllipsis(ep.Content, 100) stdout(fmt.Sprintf("%s %s %s", date, title, desc)) }