refactor(git): add branch status to template

This commit is contained in:
Jan De Dobbeleer 2021-10-31 20:59:35 +01:00 committed by Jan De Dobbeleer
parent 4dbed1176c
commit 3f8400e8f1
3 changed files with 76 additions and 46 deletions

View file

@ -15,6 +15,7 @@ type Repo struct {
Ahead int
Behind int
HEAD string
BranchStatus string
Upstream string
UpstreamIcon string
StashCount int
@ -191,7 +192,9 @@ func (g *git) shouldIgnoreRootRepository(rootDir string) bool {
func (g *git) string() string {
statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false)
displayStatus := g.props.getBool(DisplayStatus, false)
if !displayStatus {
g.repo.HEAD = g.getPrettyHEADName()
}
if displayStatus || statusColorsEnabled {
g.setGitStatus()
}
@ -201,9 +204,20 @@ func (g *git) string() string {
if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) {
g.repo.UpstreamIcon = g.getUpstreamIcon()
}
if g.props.getBool(DisplayBranchStatus, true) {
g.repo.BranchStatus = g.getBranchStatus()
}
// use template if available
segmentTemplate := g.props.getString(SegmentTemplate, "")
if len(segmentTemplate) > 0 {
return g.templateString(segmentTemplate)
}
// legacy render string if no template
// remove this for 6.0
return g.renderDeprecatedString()
}
func (g *git) templateString(segmentTemplate string) string {
template := &textTemplate{
Template: segmentTemplate,
Context: g.repo,
@ -215,10 +229,6 @@ func (g *git) string() string {
}
return text
}
// legacy render string if no template
// remove this for 6.0
return g.renderDeprecatedString(displayStatus)
}
func (g *git) init(props *properties, env environmentInfo) {
g.props = props

View file

@ -32,10 +32,7 @@ const (
WorktreeCountIcon Property = "worktree_count_icon"
)
func (g *git) renderDeprecatedString(displayStatus bool) string {
if !displayStatus {
return g.getPrettyHEADName()
}
func (g *git) renderDeprecatedString() string {
buffer := new(bytes.Buffer)
// remote (if available)
if len(g.repo.UpstreamIcon) != 0 {
@ -43,8 +40,8 @@ func (g *git) renderDeprecatedString(displayStatus bool) string {
}
// branchName
fmt.Fprintf(buffer, "%s", g.repo.HEAD)
if g.props.getBool(DisplayBranchStatus, true) {
buffer.WriteString(g.getBranchStatus())
if len(g.repo.BranchStatus) != 0 {
buffer.WriteString(g.repo.BranchStatus)
}
if g.repo.Staging.Changed {
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Staging, StagingColor, LocalStagingIcon, " \uF046"))

View file

@ -993,7 +993,7 @@ func TestGetGitCommand(t *testing.T) {
}
}
func TestGitTemplate(t *testing.T) {
func TestGitTemplateString(t *testing.T) {
cases := []struct {
Case string
Expected string
@ -1061,17 +1061,40 @@ func TestGitTemplate(t *testing.T) {
Working: &GitStatus{},
},
},
{
Case: "Upstream Icon",
Expected: "from GitHub on main",
Template: "from {{ .UpstreamIcon }} on {{ .HEAD }}",
Repo: &Repo{
HEAD: "main",
Staging: &GitStatus{},
Working: &GitStatus{},
UpstreamIcon: "GitHub",
},
},
{
Case: "Branch status",
Expected: "from GitHub on main \u21912",
Template: "from {{ .UpstreamIcon }} on {{ .HEAD }} {{ .BranchStatus }}",
Repo: &Repo{
HEAD: "main",
Staging: &GitStatus{},
Working: &GitStatus{},
UpstreamIcon: "GitHub",
BranchStatus: "\u21912",
},
},
}
for _, tc := range cases {
g := &git{
props: &properties{
values: map[Property]interface{}{
SegmentTemplate: tc.Template,
DisplayStatus: true,
},
},
repo: tc.Repo,
}
assert.Equal(t, tc.Expected, g.string(), tc.Case)
assert.Equal(t, tc.Expected, g.templateString(tc.Template), tc.Case)
}
}