@@ -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
}
@@ -100,8 +103,8 @@ type Formater struct {
100
103
}
101
104
102
105
// truncate returns s, truncated so that it is no more than max runes long.
103
- // Depending on the provided direction, truncation is performed right or left.
104
- // If s is returned truncated, the truncated part is replaced with the
106
+ // Depending on the provided direction, truncation is performed right, left or
107
+ // center. If s is returned truncated, the truncated part is replaced with the
105
108
// 'ellipsis' string.
106
109
//
107
110
// If max is zero, negative or greater than the number of runes in s, truncate
@@ -130,6 +133,16 @@ func truncate(s, ellipsis string, max int, dir direction) string {
130
133
case dirLeft :
131
134
runes = runes [len (runes )+ len (ell )- max :]
132
135
runes = append (ell , runes ... )
136
+ case dirCenter :
137
+ // We want to keep the same number of runes on both sides of the ellipsis. If the
138
+ // number of runes on each side is odd, we add one more rune to the right side.
139
+ llen := (max - len (ell )) / 2
140
+ rlen := max - len (ell ) - llen
141
+
142
+ right := runes [len (runes )- rlen :]
143
+
144
+ runes = append (runes [:llen ], ell ... )
145
+ runes = append (runes , right ... )
133
146
}
134
147
return string (runes )
135
148
}
0 commit comments