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 (
|
2021-09-23 23:46:17 +02:00
|
|
|
Ellipsis = "…"
|
2021-11-12 05:09:17 +01:00
|
|
|
ClipCountryCode = 2
|
2020-04-26 16:22:29 +02:00
|
|
|
ClipKeyword = 40
|
2021-11-12 09:10:15 +01:00
|
|
|
ClipUsername = 64
|
2021-09-03 19:02:26 +02:00
|
|
|
ClipSlug = 80
|
2021-09-23 23:46:17 +02:00
|
|
|
ClipCategory = 100
|
2021-11-12 05:09:17 +01:00
|
|
|
ClipPlace = 128
|
2021-09-23 23:46:17 +02:00
|
|
|
ClipDefault = 160
|
|
|
|
ClipName = 160
|
|
|
|
ClipTitle = 200
|
2021-11-12 05:09:17 +01:00
|
|
|
ClipVarchar = 255
|
2022-03-24 21:46:25 +01:00
|
|
|
ClipPath = 500
|
2021-11-12 05:09:17 +01:00
|
|
|
ClipLabel = 500
|
2021-08-30 11:26:57 +02:00
|
|
|
ClipQuery = 1000
|
2020-04-26 16:22:29 +02:00
|
|
|
ClipDescription = 16000
|
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
|
|
|
}
|