Skip to content

Commit cc84284

Browse files
committed
feat: trim branch from center
1 parent b821624 commit cc84284

File tree

4 files changed

+97
-12
lines changed

4 files changed

+97
-12
lines changed

.gitmux.yml

+1-1
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

+7-7
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ layout: [branch, "|", flags, "|", stats]
264264

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

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

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 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` |
275275

276276
## Troubleshooting
277277

tmux/formater.go

+17-4
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
}
@@ -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,16 @@ 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+
llen := (max - len(ell)) / 2
139+
rlen := max - len(ell) - llen
140+
141+
right := runes[len(runes)-rlen:]
142+
143+
runes = append(runes[:llen], ell...)
144+
runes = append(runes, right...)
132145
}
133146
return string(runes)
134147
}

tmux/formater_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,78 @@ func Test_truncate(t *testing.T) {
412412
dir: dirLeft,
413413
want: "super-long-branch",
414414
},
415+
416+
/* trim center */
417+
{
418+
s: "br",
419+
ellipsis: "...",
420+
max: 1,
421+
dir: dirCenter,
422+
want: "r",
423+
},
424+
{
425+
s: "br",
426+
ellipsis: "",
427+
max: 1,
428+
dir: dirCenter,
429+
want: "r",
430+
},
431+
{
432+
s: "br",
433+
ellipsis: "...",
434+
max: 3,
435+
dir: dirCenter,
436+
want: "br",
437+
},
438+
{
439+
s: "super-long-branch",
440+
ellipsis: "...",
441+
max: 3,
442+
dir: dirCenter,
443+
want: "...",
444+
},
445+
{
446+
s: "super-long-branch",
447+
ellipsis: "...",
448+
max: 15,
449+
dir: dirCenter,
450+
want: "super-...branch",
451+
},
452+
{
453+
s: "super-long-branch",
454+
ellipsis: "...",
455+
max: 17,
456+
dir: dirCenter,
457+
want: "super-long-branch",
458+
},
459+
{
460+
s: "长長的-树樹枝",
461+
ellipsis: "...",
462+
max: 6,
463+
dir: dirCenter,
464+
want: "长...樹枝",
465+
},
466+
{
467+
s: "super-long-branch",
468+
ellipsis: "...",
469+
max: 32,
470+
dir: dirCenter,
471+
want: "super-long-branch",
472+
},
473+
{
474+
s: "super-long-branch",
475+
ellipsis: "...",
476+
max: 0,
477+
dir: dirCenter,
478+
want: "super-long-branch",
479+
},
480+
{
481+
s: "super-long-branch",
482+
ellipsis: "...",
483+
max: -1,
484+
dir: dirCenter,
485+
want: "super-long-branch",
486+
},
415487
}
416488
for _, tt := range tests {
417489
t.Run("", func(t *testing.T) {

0 commit comments

Comments
 (0)