mirror of
https://github.com/fiatjaf/nak.git
synced 2026-09-11 21:22:07 +02:00
count runes and not bytes when clamping text, so multibyte characters don't get cut in half.
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user