count runes and not bytes when clamping text, so multibyte characters don't get cut in half.

This commit is contained in:
Yasuhiro Matsumoto
2026-09-07 10:55:58 +09:00
parent c72ff66c0c
commit 996dade3ec
2 changed files with 7 additions and 6 deletions

View File

@@ -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 (

View File

@@ -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))
}