Skip to content

Commit

Permalink
feat: trim branch from center
Browse files Browse the repository at this point in the history
  • Loading branch information
rtalexk committed Jun 9, 2024
1 parent b821624 commit a882b42
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ layout: [branch, "|", flags, "|", stats]

This is the list of additional configuration `options`:

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

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

## Troubleshooting

Expand Down
21 changes: 17 additions & 4 deletions tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ type styles struct {
}

const (
dirLeft direction = "left"
dirRight direction = "right"
dirLeft direction = "left"
dirRight direction = "right"
dirCenter direction = "center"
)

type direction string
Expand All @@ -78,6 +79,8 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
*d = dirLeft
case dirRight:
*d = dirRight
case dirCenter:
*d = dirCenter
default:
return fmt.Errorf("'direction': unexpected value %v", s)
}
Expand All @@ -99,8 +102,8 @@ type Formater struct {
}

// truncate returns s, truncated so that it is no more than max runes long.
// Depending on the provided direction, truncation is performed right or left.
// If s is returned truncated, the truncated part is replaced with the
// Depending on the provided direction, truncation is performed right, left or
// center. If s is returned truncated, the truncated part is replaced with the
// 'ellipsis' string.
//
// If max is zero, negative or greater than the number of runes in s, truncate
Expand Down Expand Up @@ -129,6 +132,16 @@ func truncate(s, ellipsis string, max int, dir direction) string {
case dirLeft:
runes = runes[len(runes)+len(ell)-max:]
runes = append(ell, runes...)
case dirCenter:
// We want to keep the same number of runes on both sides of the ellipsis. If the
// number of runes on each side is odd, we add one more rune to the right side.
leftLength := (max - len(ell)) / 2
rightLength := max - len(ell) - leftLength

rightPart := runes[len(runes)-rightLength:]

runes = append(runes[:leftLength], ell...)
runes = append(runes, rightPart...)
}
return string(runes)
}
Expand Down
72 changes: 72 additions & 0 deletions tmux/formater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,78 @@ func Test_truncate(t *testing.T) {
dir: dirLeft,
want: "super-long-branch",
},

/* trim center */
{
s: "br",
ellipsis: "...",
max: 1,
dir: dirCenter,
want: "r",
},
{
s: "br",
ellipsis: "",
max: 1,
dir: dirCenter,
want: "r",
},
{
s: "br",
ellipsis: "...",
max: 3,
dir: dirCenter,
want: "br",
},
{
s: "super-long-branch",
ellipsis: "...",
max: 3,
dir: dirCenter,
want: "...",
},
{
s: "super-long-branch",
ellipsis: "...",
max: 15,
dir: dirCenter,
want: "super-...branch",
},
{
s: "super-long-branch",
ellipsis: "...",
max: 17,
dir: dirCenter,
want: "super-long-branch",
},
{
s: "长長的-树樹枝",
ellipsis: "...",
max: 6,
dir: dirCenter,
want: "长...樹枝",
},
{
s: "super-long-branch",
ellipsis: "...",
max: 32,
dir: dirCenter,
want: "super-long-branch",
},
{
s: "super-long-branch",
ellipsis: "...",
max: 0,
dir: dirCenter,
want: "super-long-branch",
},
{
s: "super-long-branch",
ellipsis: "...",
max: -1,
dir: dirCenter,
want: "super-long-branch",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
Expand Down

0 comments on commit a882b42

Please sign in to comment.