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 = "…"
|
2022-09-02 18:39:19 +02:00
|
|
|
ClipRole = 32
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipKeyword = 40
|
2022-09-28 09:01:17 +02:00
|
|
|
ClipIP = 48
|
2022-09-02 18:39:19 +02:00
|
|
|
ClipUserName = 64
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipSlug = 80
|
|
|
|
ClipCategory = 100
|
2022-09-28 09:01:17 +02:00
|
|
|
ClipTokenName = 128
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipDefault = 160
|
|
|
|
ClipName = 160
|
2022-09-28 09:01:17 +02:00
|
|
|
ClipLongName = 200
|
2022-09-02 18:39:19 +02:00
|
|
|
ClipEmail = 255
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipPath = 500
|
2022-09-28 09:01:17 +02:00
|
|
|
ClipURL = 512
|
|
|
|
ClipLog = 512
|
|
|
|
ClipFlags = 767
|
2022-03-30 20:36:25 +02:00
|
|
|
ClipShortText = 1024
|
|
|
|
ClipText = 2048
|
|
|
|
ClipLongText = 4096
|
2022-09-02 18:39:19 +02:00
|
|
|
ClipPassword = 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 {
|
2022-09-28 09:01:17 +02:00
|
|
|
s = string(runes[0:size])
|
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
|
|
|
}
|