Skip to content

Commit

Permalink
Issue 60/ellipsis (#85)
Browse files Browse the repository at this point in the history
* Add Ellipsis option, defaults to …

* Modify Test_truncate

* Change default layout (remove .. between branch and remote)

* README.md format

* README.md: document ellipsis

Fixes #60

* Update golden file and fix tests

Since the default config has changed the golden file must be
updated.
  • Loading branch information
arl committed Feb 16, 2023
1 parent 1606e24 commit 2f7b93a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 102 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- [Prerequisites](#prerequisites)
- [Installing](#installing)
- [Binary release](#binary-release)
- [Homebrew (tap) macOS and linux, amd64 and arm64](#homebrew-tap-macos-and-linux-amd64-and-arm64)
- [Homebrew tap (macOS and linux) (amd64 and arm64)](#homebrew-tap-macos-and-linux-amd64-and-arm64)
- [AUR](#aur)
- [From source](#from-source)
- [Getting started](#getting-started)
Expand Down Expand Up @@ -132,6 +132,7 @@ tmux:
options:
branch_max_len: 0
branch_trim: right
ellipsis:
```

First, save the default configuration to a new file:
Expand Down Expand Up @@ -261,10 +262,11 @@ 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) |
| 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 | `` |


## Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion testdata/default.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ with some different content

-- empty_file --
-- output.golden --
#[fg=default]#[fg=default]#[fg=white,bold]⎇ #[fg=default]#[fg=white,bold]main#[fg=default]..#[fg=default] - #[fg=default]#[fg=green,bold]● 1 #[fg=red,bold]✚ 1 #[fg=cyan,bold]⚑ 1 #[fg=magenta,bold]… 2
#[fg=default]#[fg=default]#[fg=white,bold]⎇ #[fg=default]#[fg=white,bold]main#[fg=default] #[fg=default] - #[fg=default]#[fg=green,bold]● 1 #[fg=red,bold]✚ 1 #[fg=cyan,bold]⚑ 1 #[fg=magenta,bold]… 2
33 changes: 18 additions & 15 deletions tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (d *direction) UnmarshalYAML(value *yaml.Node) error {
type options struct {
BranchMaxLen int `yaml:"branch_max_len"`
BranchTrim direction `yaml:"branch_trim"`
Ellipsis string `yaml:"ellipsis"`
}

// DefaultCfg is the default tmux configuration.
Expand All @@ -103,7 +104,6 @@ var DefaultCfg = Config{
Ahead: "↑·",
Behind: "↓·",
HashPrefix: ":",

Insertions: "Σ",
Deletions: "Δ",
},
Expand All @@ -122,10 +122,11 @@ var DefaultCfg = Config{
Insertions: "#[fg=green]",
Deletions: "#[fg=red]",
},
Layout: []string{"branch", "..", "remote-branch", "divergence", " - ", "flags"},
Layout: []string{"branch", " ", "remote-branch", "divergence", " - ", "flags"},
Options: options{
BranchMaxLen: 0,
BranchTrim: dirRight,
Ellipsis: "…",
},
}

Expand All @@ -136,25 +137,27 @@ type Formater struct {
st *gitstatus.Status
}

// truncate returns s, truncated so that it is no more than max characters long.
// 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 max is zero, negative or greater than the number of rnues in s, truncate
// just returns s. However, if truncation is applied, then the last 3 chars (or
// 3 first, depending on provided direction) are replaced with "...".
// 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
// just returns s.
//
// NOTE: If max is lower than 3, in other words if we can't even have ellispis,
// then truncate just truncates the maximum number of characters, without
// bothering with ellipsis.
func truncate(s string, max int, dir direction) string {
// NOTE: If max is lower than len(ellipsis), in other words it we're not even
// allowed to just return the ellipsis string, then we just return the maximum
// number of runes we can, without inserting ellpisis.
func truncate(s, ellipsis string, max int, dir direction) string {
slen := utf8.RuneCountInString(s)
if max <= 0 || slen <= max {
return s
}

runes := []rune(s)
ell := []rune("...")
ell := []rune(ellipsis)

if max < 3 {
if max < len(ellipsis) {
ell = nil // Just truncate s since even ellipsis don't fit.
}

Expand All @@ -176,7 +179,7 @@ func (f *Formater) Format(w io.Writer, st *gitstatus.Status) error {

// overall working tree state
if f.st.IsInitial {
branch := truncate(f.st.LocalBranch, f.Options.BranchMaxLen, f.Options.BranchTrim)
branch := truncate(f.st.LocalBranch, f.Options.Ellipsis, f.Options.BranchMaxLen, f.Options.BranchTrim)
fmt.Fprintf(w, "%s%s [no commits yet]", f.Styles.Branch, branch)
f.flags()
_, err := f.b.WriteTo(w)
Expand Down Expand Up @@ -245,7 +248,7 @@ func (f *Formater) remoteBranch() {

f.clear()

branch := truncate(f.st.RemoteBranch, f.Options.BranchMaxLen, f.Options.BranchTrim)
branch := truncate(f.st.RemoteBranch, f.Options.Ellipsis, f.Options.BranchMaxLen, f.Options.BranchTrim)
fmt.Fprintf(&f.b, "%s%s", f.Styles.Remote, branch)
}

Expand Down Expand Up @@ -280,7 +283,7 @@ func (f *Formater) currentRef() {
return
}

branch := truncate(f.st.LocalBranch, f.Options.BranchMaxLen, f.Options.BranchTrim)
branch := truncate(f.st.LocalBranch, f.Options.Ellipsis, f.Options.BranchMaxLen, f.Options.BranchTrim)
fmt.Fprintf(&f.b, "%s%s", f.Styles.Branch, branch)
}

Expand Down
Loading

0 comments on commit 2f7b93a

Please sign in to comment.