2020-02-02 02:00:47 +01:00
|
|
|
package txt
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2020-02-02 02:00:47 +01:00
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
const (
|
2022-03-30 20:36:25 +02:00
|
|
|
Ellipsis = "…"
|
|
|
|
ClipKeyword = 40
|
|
|
|
ClipUsername = 64
|
|
|
|
ClipSlug = 80
|
|
|
|
ClipCategory = 100
|
|
|
|
ClipDefault = 160
|
|
|
|
ClipName = 160
|
|
|
|
ClipTitle = 200
|
|
|
|
ClipPath = 500
|
|
|
|
ClipShortText = 1024
|
|
|
|
ClipText = 2048
|
|
|
|
ClipLongText = 4096
|
2020-04-26 14:31:33 +02:00
|
|
|
)
|
|
|
|
|
2021-09-03 19:02:26 +02:00
|
|
|
// Clip shortens a string to the given number of runes, and removes all leading and trailing white space.
|
2020-02-02 02:00:47 +01:00
|
|
|
func Clip(s string, size int) string {
|
2020-04-26 14:31:33 +02:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
if s == "" || size <= 0 {
|
2020-02-02 02:00:47 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
runes := []rune(s)
|
|
|
|
|
|
|
|
if len(runes) > size {
|
2020-02-02 03:36:00 +01:00
|
|
|
s = string(runes[0 : size-1])
|
2020-02-02 02:00:47 +01:00
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
return strings.TrimSpace(s)
|
2020-02-02 02:00:47 +01:00
|
|
|
}
|
2020-10-05 07:40:11 +02:00
|
|
|
|
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 {
|
2020-10-05 07:40:11 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
return Clip(s, size-l) + suffix
|
2020-10-05 07:40:11 +02:00
|
|
|
}
|