diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-17 10:23:06 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-21 22:07:14 +1100 |
commit | 1f3e1720a3a0bd31c0816a94d0b7c5bca1589f96 (patch) | |
tree | 5ad5c1f01b31cdf9e4fed9cdc0a4779a2bf442b8 /pkg/commands | |
parent | b7f2d0366b2c9b8ac034e426661db4ea72a7bb14 (diff) |
split RemoteBranch out from Branch
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/branch.go | 15 | ||||
-rw-r--r-- | pkg/commands/loading_remotes.go | 18 | ||||
-rw-r--r-- | pkg/commands/remote.go | 2 | ||||
-rw-r--r-- | pkg/commands/remote_branch.go | 18 |
4 files changed, 41 insertions, 12 deletions
diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go index d1cafdfb4..42d43507f 100644 --- a/pkg/commands/branch.go +++ b/pkg/commands/branch.go @@ -22,7 +22,7 @@ type Branch struct { // GetDisplayStrings returns the display string of branch func (b *Branch) GetDisplayStrings(isFocused bool) []string { - displayName := utils.ColoredString(b.Name, b.GetColor()) + displayName := utils.ColoredString(b.Name, GetBranchColor(b.Name)) if isFocused && b.Selected && b.Pushables != "" && b.Pullables != "" { displayName = fmt.Sprintf("%s ā%sā%s", displayName, b.Pushables, b.Pullables) } @@ -30,9 +30,11 @@ func (b *Branch) GetDisplayStrings(isFocused bool) []string { return []string{b.Recency, displayName} } -// GetColor branch color -func (b *Branch) GetColor() color.Attribute { - switch b.getType() { +// GetBranchColor branch color +func GetBranchColor(name string) color.Attribute { + branchType := strings.Split(name, "/")[0] + + switch branchType { case "feature": return color.FgGreen case "bugfix": @@ -43,8 +45,3 @@ func (b *Branch) GetColor() color.Attribute { return theme.DefaultTextColor } } - -// expected to return feature/bugfix/hotfix or blank string -func (b *Branch) getType() string { - return strings.Split(b.Name, "/")[0] -} diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go index 8b6b47d57..dc5dcac44 100644 --- a/pkg/commands/loading_remotes.go +++ b/pkg/commands/loading_remotes.go @@ -3,6 +3,8 @@ package commands import ( "fmt" "regexp" + "sort" + "strings" ) func (c *GitCommand) GetRemotes() ([]*Remote, error) { @@ -24,9 +26,9 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) { re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name)) matches := re.FindAllStringSubmatch(remoteBranchesStr, -1) - branches := make([]*Branch, len(matches)) + branches := make([]*RemoteBranch, len(matches)) for j, match := range matches { - branches[j] = &Branch{ + branches[j] = &RemoteBranch{ Name: match[1], } } @@ -38,5 +40,17 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) { } } + // now lets sort our remotes by name alphabetically + sort.Slice(remotes, func(i, j int) bool { + // we want origin at the top because we'll be most likely to want it + if remotes[i].Name == "origin" { + return true + } + if remotes[j].Name == "origin" { + return false + } + return strings.ToLower(remotes[i].Name) < strings.ToLower(remotes[j].Name) + }) + return remotes, nil } diff --git a/pkg/commands/remote.go b/pkg/commands/remote.go index 8f2941505..d61f81a53 100644 --- a/pkg/commands/remote.go +++ b/pkg/commands/remote.go @@ -5,7 +5,7 @@ type Remote struct { Name string Urls []string Selected bool - Branches []*Branch + Branches []*RemoteBranch } // GetDisplayStrings returns the display string of a remote diff --git a/pkg/commands/remote_branch.go b/pkg/commands/remote_branch.go new file mode 100644 index 000000000..18e58fa85 --- /dev/null +++ b/pkg/commands/remote_branch.go @@ -0,0 +1,18 @@ +package commands + +import ( + "github.com/jesseduffield/lazygit/pkg/utils" +) + +// Remote Branch : A git remote branch +type RemoteBranch struct { + Name string + Selected bool +} + +// GetDisplayStrings returns the display string of branch +func (b *RemoteBranch) GetDisplayStrings(isFocused bool) []string { + displayName := utils.ColoredString(b.Name, GetBranchColor(b.Name)) + + return []string{displayName} +} |