Skip to content

Commit bb31d30

Browse files
rtalexkarl
andauthored
feat: trim branch from center (#116)
Co-authored-by: Aurélien Rainone <[email protected]>
1 parent 0323aec commit bb31d30

File tree

4 files changed

+98
-14
lines changed

4 files changed

+98
-14
lines changed

.gitmux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ tmux:
7575
options:
7676
# Maximum displayed length for local and remote branch names.
7777
branch_max_len: 0
78-
# Trim left or right end of the branch (`right` or `left`).
78+
# Trim left, right or from the center of the branch (`right`, `left` or `center`).
7979
branch_trim: right
8080
# Character indicating whether and where a branch was truncated.
8181
ellipsis:

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,14 @@ layout: [branch, "|", flags, "|", stats]
265265

266266
This is the list of additional configuration `options`:
267267

268-
| Option | Description | Default |
269-
| :----------------- | :--------------------------------------------------------- | :----------------: |
270-
| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) |
271-
| `branch_trim` | Trim left or right end of the branch (`right` or `left`) | `right` (trailing) |
272-
| `ellipsis` | Character to show branch name has been truncated | `…` |
273-
| `hide_clean` | Hides the clean flag entirely | `false` |
274-
| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` |
275-
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
276-
268+
| Option | Description | Default |
269+
| :----------------- | :------------------------------------------------------------------------------ | :----------------: |
270+
| `branch_max_len` | Maximum displayed length for local and remote branch names | `0` (no limit) |
271+
| `branch_trim` | Trim left, right or from the center of the branch (`right`, `left` or `center`) | `right` (trailing) |
272+
| `ellipsis` | Character to show branch name has been truncated | `…` |
273+
| `hide_clean` | Hides the clean flag entirely | `false` |
274+
| `swap_divergence` | Swaps order of behind & ahead upstream counts | `false` |
275+
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
277276

278277
## Troubleshooting
279278

tmux/formater.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ type styles struct {
6262
}
6363

6464
const (
65-
dirLeft direction = "left"
66-
dirRight direction = "right"
65+
dirLeft direction = "left"
66+
dirRight direction = "right"
67+
dirCenter direction = "center"
6768
)
6869

6970
type 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
}
@@ -100,8 +103,8 @@ type Formater struct {
100103
}
101104

102105
// 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
105108
// 'ellipsis' string.
106109
//
107110
// 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 {
130133
case dirLeft:
131134
runes = runes[len(runes)+len(ell)-max:]
132135
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...)
133146
}
134147
return string(runes)
135148
}

tmux/formater_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,78 @@ func Test_truncate(t *testing.T) {
484484
dir: dirLeft,
485485
want: "super-long-branch",
486486
},
487+
488+
/* trim center */
489+
{
490+
s: "br",
491+
ellipsis: "...",
492+
max: 1,
493+
dir: dirCenter,
494+
want: "r",
495+
},
496+
{
497+
s: "br",
498+
ellipsis: "",
499+
max: 1,
500+
dir: dirCenter,
501+
want: "r",
502+
},
503+
{
504+
s: "br",
505+
ellipsis: "...",
506+
max: 3,
507+
dir: dirCenter,
508+
want: "br",
509+
},
510+
{
511+
s: "super-long-branch",
512+
ellipsis: "...",
513+
max: 3,
514+
dir: dirCenter,
515+
want: "...",
516+
},
517+
{
518+
s: "super-long-branch",
519+
ellipsis: "...",
520+
max: 15,
521+
dir: dirCenter,
522+
want: "super-...branch",
523+
},
524+
{
525+
s: "super-long-branch",
526+
ellipsis: "...",
527+
max: 17,
528+
dir: dirCenter,
529+
want: "super-long-branch",
530+
},
531+
{
532+
s: "长長的-树樹枝",
533+
ellipsis: "...",
534+
max: 6,
535+
dir: dirCenter,
536+
want: "长...樹枝",
537+
},
538+
{
539+
s: "super-long-branch",
540+
ellipsis: "...",
541+
max: 32,
542+
dir: dirCenter,
543+
want: "super-long-branch",
544+
},
545+
{
546+
s: "super-long-branch",
547+
ellipsis: "...",
548+
max: 0,
549+
dir: dirCenter,
550+
want: "super-long-branch",
551+
},
552+
{
553+
s: "super-long-branch",
554+
ellipsis: "...",
555+
max: -1,
556+
dir: dirCenter,
557+
want: "super-long-branch",
558+
},
487559
}
488560
for _, tt := range tests {
489561
t.Run("", func(t *testing.T) {

0 commit comments

Comments
 (0)