@@ -62,8 +62,9 @@ type styles struct {
6262}
6363
6464const (
65- dirLeft direction = "left"
66- dirRight direction = "right"
65+ dirLeft direction = "left"
66+ dirRight direction = "right"
67+ dirCenter direction = "center"
6768)
6869
6970type direction string
@@ -78,6 +79,8 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
7879 * d = dirLeft
7980 case dirRight :
8081 * d = dirRight
82+ case dirCenter :
83+ * d = dirCenter
8184 default :
8285 return fmt .Errorf ("'direction': unexpected value %v" , s )
8386 }
@@ -99,8 +102,8 @@ type Formater struct {
99102}
100103
101104// 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
104107// 'ellipsis' string.
105108//
106109// If max is zero, negative or greater than the number of runes in s, truncate
@@ -129,6 +132,19 @@ func truncate(s, ellipsis string, max int, dir direction) string {
129132 case dirLeft :
130133 runes = runes [len (runes )+ len (ell )- max :]
131134 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+ rightStartIndex := len (runes ) - rightLength
142+
143+ leftPart := runes [:leftLength ]
144+ rightPart := runes [rightStartIndex :]
145+
146+ runes = append (leftPart , ell ... )
147+ runes = append (runes , rightPart ... )
132148 }
133149 return string (runes )
134150}
0 commit comments