-
Notifications
You must be signed in to change notification settings - Fork 187
/
ellipsis.go
79 lines (68 loc) · 2.16 KB
/
ellipsis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2022 The Inspektor Gadget authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ellipsis
type EllipsisType int
const (
None EllipsisType = iota // None simply cuts the text if it is too long
End // End cuts an overflowing string one character before reaching the maximum width and adds an ellipsis ("…").
Start // Start lets the overflowing string start with an ellipsis ("…") followed by the last X characters, where X is the maximum length - 1.
Middle // Middle uses the first and last characters of an overflowing string, merging them in the middle with an ellipsis ("…").
)
const ellipsisRune = rune('…')
func (et EllipsisType) String() string {
switch et {
default:
fallthrough
case None:
return "None"
case End:
return "End"
case Start:
return "Start"
case Middle:
return "Middle"
}
}
func ShortenString(str string, maxLength int, ellipsisType EllipsisType) string {
return string(Shorten([]rune(str), maxLength, ellipsisType))
}
func Shorten(rs []rune, maxLength int, ellipsisType EllipsisType) []rune {
if maxLength <= 0 {
return []rune{}
}
slen := len(rs)
if slen <= maxLength {
return rs
}
if maxLength <= 1 && ellipsisType != None {
return []rune{ellipsisRune}
}
switch ellipsisType {
default:
fallthrough
case None:
return rs[:maxLength]
case Start:
return append([]rune{ellipsisRune}, rs[slen-maxLength+1:]...)
case End:
return append(rs[:maxLength-1], ellipsisRune)
case Middle:
mid := maxLength / 2
end := mid
if maxLength%2 == 0 {
end -= 1
}
return append(append(rs[:mid], ellipsisRune), rs[slen-end:]...)
}
}