@@ -62,8 +62,9 @@ type styles struct {
62
62
}
63
63
64
64
const (
65
- dirLeft direction = "left"
66
- dirRight direction = "right"
65
+ dirLeft direction = "left"
66
+ dirRight direction = "right"
67
+ dirCenter direction = "center"
67
68
)
68
69
69
70
type direction string
@@ -78,6 +79,8 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
78
79
* d = dirLeft
79
80
case dirRight :
80
81
* d = dirRight
82
+ case dirCenter :
83
+ * d = dirCenter
81
84
default :
82
85
return fmt .Errorf ("'direction': unexpected value %v" , s )
83
86
}
@@ -99,8 +102,8 @@ type Formater struct {
99
102
}
100
103
101
104
// truncate returns s, truncated so that it is no more than max runes long.
102
- // Depending on the provided direction, truncation is performed right or left.
103
- // If s is returned truncated, the truncated part is replaced with the
105
+ // Depending on the provided direction, truncation is performed right, left or
106
+ // center. If s is returned truncated, the truncated part is replaced with the
104
107
// 'ellipsis' string.
105
108
//
106
109
// If max is zero, negative or greater than the number of runes in s, truncate
@@ -129,6 +132,16 @@ func truncate(s, ellipsis string, max int, dir direction) string {
129
132
case dirLeft :
130
133
runes = runes [len (runes )+ len (ell )- max :]
131
134
runes = append (ell , runes ... )
135
+ case dirCenter :
136
+ // We want to keep the same number of runes on both sides of the ellipsis. If the
137
+ // number of runes on each side is odd, we add one more rune to the right side.
138
+ leftLength := (max - len (ell )) / 2
139
+ rightLength := max - len (ell ) - leftLength
140
+
141
+ rightPart := runes [len (runes )- rightLength :]
142
+
143
+ runes = append (runes [:leftLength ], ell ... )
144
+ runes = append (runes , rightPart ... )
132
145
}
133
146
return string (runes )
134
147
}
0 commit comments