Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitmux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ tmux:
swap_divergence: false
# Add a space between behind & ahead upstream counts.
divergence_space: false
# Show flags symbols without counts.
flags_without_count: false
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ 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, right or from the center 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` |
| `divergence_space` | Add a space between 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, right or from the center 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` |
| `divergence_space` | Add a space between behind & ahead upstream counts | `false` |
| `flags_without_count`| Show flags symbols without counts | `false` |

## Troubleshooting

Expand Down
39 changes: 21 additions & 18 deletions tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ 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"`
HideClean bool `yaml:"hide_clean"`
DivergenceSpace bool `yaml:"divergence_space"`
SwapDivergence bool `yaml:"swap_divergence"`
BranchMaxLen int `yaml:"branch_max_len"`
BranchTrim direction `yaml:"branch_trim"`
Ellipsis string `yaml:"ellipsis"`
HideClean bool `yaml:"hide_clean"`
DivergenceSpace bool `yaml:"divergence_space"`
SwapDivergence bool `yaml:"swap_divergence"`
FlagsWithoutCount bool `yaml:"flags_without_count"`
}

// A Formater formats git status to a tmux style string.
Expand Down Expand Up @@ -294,12 +295,19 @@ func (f *Formater) currentRef() string {
return fmt.Sprintf("%s%s%s", f.Styles.Clear, f.Styles.Branch, branch)
}

// formatFlag formats a flag with or without count based on the flags_without_count option
func (f *Formater) formatFlag(style, symbol string, count int) string {
if f.Options.FlagsWithoutCount {
return fmt.Sprintf("%s%s", style, symbol)
}
return fmt.Sprintf("%s%s%d", style, symbol, count)
}

func (f *Formater) flags() string {
var flags []string
if f.st.IsClean {
if f.st.NumStashed != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
}

if !f.Options.HideClean {
Expand All @@ -312,28 +320,23 @@ func (f *Formater) flags() string {
}

if f.st.NumStaged != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged))
flags = append(flags, f.formatFlag(f.Styles.Staged, f.Symbols.Staged, f.st.NumStaged))
}

if f.st.NumConflicts != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts))
flags = append(flags, f.formatFlag(f.Styles.Conflict, f.Symbols.Conflict, f.st.NumConflicts))
}

if f.st.NumModified != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Modified, f.Symbols.Modified, f.st.NumModified))
flags = append(flags, f.formatFlag(f.Styles.Modified, f.Symbols.Modified, f.st.NumModified))
}

if f.st.NumStashed != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
flags = append(flags, f.formatFlag(f.Styles.Stashed, f.Symbols.Stashed, f.st.NumStashed))
}

if f.st.NumUntracked != 0 {
flags = append(flags,
fmt.Sprintf("%s%s%d", f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked))
flags = append(flags, f.formatFlag(f.Styles.Untracked, f.Symbols.Untracked, f.st.NumUntracked))
}

if len(flags) > 0 {
Expand Down
123 changes: 123 additions & 0 deletions tmux/formater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,129 @@ func TestFlags(t *testing.T) {
}
}

func TestFlagsWithoutCount(t *testing.T) {
tests := []struct {
name string
styles styles
symbols symbols
options options
st *gitstatus.Status
want string
}{
{
name: "flags with counts (default)",
styles: styles{
Clear: "StyleClear",
Modified: "StyleMod",
Stashed: "StyleStash",
Staged: "StyleStaged",
},
symbols: symbols{
Modified: "SymbolMod",
Stashed: "SymbolStash",
Staged: "SymbolStaged",
},
options: options{
FlagsWithoutCount: false,
},
st: &gitstatus.Status{
NumStashed: 1,
Porcelain: gitstatus.Porcelain{
NumModified: 2,
NumStaged: 3,
},
},
want: "StyleClear" + "StyleStagedSymbolStaged3 StyleModSymbolMod2 StyleStashSymbolStash1",
},
{
name: "flags without counts",
styles: styles{
Clear: "StyleClear",
Modified: "StyleMod",
Stashed: "StyleStash",
Staged: "StyleStaged",
},
symbols: symbols{
Modified: "SymbolMod",
Stashed: "SymbolStash",
Staged: "SymbolStaged",
},
options: options{
FlagsWithoutCount: true,
},
st: &gitstatus.Status{
NumStashed: 1,
Porcelain: gitstatus.Porcelain{
NumModified: 2,
NumStaged: 3,
},
},
want: "StyleClear" + "StyleStagedSymbolStaged StyleModSymbolMod StyleStashSymbolStash",
},
{
name: "all flags without counts",
styles: styles{
Clear: "StyleClear",
Conflict: "StyleConflict",
Modified: "StyleMod",
Stashed: "StyleStash",
Staged: "StyleStaged",
Untracked: "StyleUntracked",
},
symbols: symbols{
Conflict: "SymbolConflict",
Modified: "SymbolMod",
Stashed: "SymbolStash",
Staged: "SymbolStaged",
Untracked: "SymbolUntracked",
},
options: options{
FlagsWithoutCount: true,
},
st: &gitstatus.Status{
NumStashed: 5,
Porcelain: gitstatus.Porcelain{
NumConflicts: 1,
NumModified: 10,
NumStaged: 3,
NumUntracked: 7,
},
},
want: "StyleClear" + "StyleStagedSymbolStaged StyleConflictSymbolConflict StyleModSymbolMod StyleStashSymbolStash StyleUntrackedSymbolUntracked",
},
{
name: "clean with stash without count",
styles: styles{
Clear: "StyleClear",
Clean: "StyleClean",
Stashed: "StyleStash",
},
symbols: symbols{
Clean: "SymbolClean",
Stashed: "SymbolStash",
},
options: options{
FlagsWithoutCount: true,
},
st: &gitstatus.Status{
IsClean: true,
NumStashed: 1,
},
want: "StyleClearStyleStashSymbolStash StyleCleanSymbolClean",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &Formater{
Config: Config{Styles: tt.styles, Symbols: tt.symbols, Options: tt.options},
st: tt.st,
}

compareStrings(t, tt.want, f.flags())
})
}
}

func TestDivergence(t *testing.T) {
tests := []struct {
name string
Expand Down