photoprism/pkg/txt/clip.go

55 lines
942 B
Go
Raw Normal View History

package txt
2021-09-23 23:46:17 +02:00
import (
"strings"
)
const (
2021-09-23 23:46:17 +02:00
Ellipsis = "…"
ClipCountryCode = 2
ClipKeyword = 40
ClipUsername = 64
ClipSlug = 80
2021-09-23 23:46:17 +02:00
ClipCategory = 100
ClipPlace = 128
2021-09-23 23:46:17 +02:00
ClipDefault = 160
ClipName = 160
ClipTitle = 200
ClipVarchar = 255
ClipLabel = 500
ClipQuery = 1000
ClipDescription = 16000
)
// Clip shortens a string to the given number of runes, and removes all leading and trailing white space.
func Clip(s string, size int) string {
s = strings.TrimSpace(s)
if s == "" || size <= 0 {
return ""
}
runes := []rune(s)
if len(runes) > size {
s = string(runes[0 : size-1])
}
2021-09-23 23:46:17 +02:00
return strings.TrimSpace(s)
}
2021-09-23 23:46:17 +02:00
// Shorten shortens a string with suffix.
func Shorten(s string, size int, suffix string) string {
if suffix == "" {
suffix = Ellipsis
}
l := len(suffix)
if len(s) < size || size < l+1 {
return s
}
2021-09-23 23:46:17 +02:00
return Clip(s, size-l) + suffix
}