Skip to content

Commit

Permalink
feat(branches): footer to show git status (#437)
Browse files Browse the repository at this point in the history
* feat(branches): footer to show git status

* fix: use diff HEAD
  • Loading branch information
dlvhdr committed Sep 4, 2024
1 parent 04fff7f commit f22047c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
35 changes: 34 additions & 1 deletion git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package git

import (
"bufio"
"bytes"
"errors"
"fmt"
"sort"
Expand All @@ -18,6 +20,7 @@ type Repo struct {
Origin string
Remotes []string
Branches []Branch
Status gitm.NameStatus
}

type Branch struct {
Expand Down Expand Up @@ -72,6 +75,10 @@ func GetRepo(dir string) (*Repo, error) {
if err != nil {
return nil, err
}
status, err := getUnstagedStatus(repo)
if err != nil {
return nil, err
}

branches := make([]Branch, len(bNames))
for i, b := range bNames {
Expand Down Expand Up @@ -119,7 +126,33 @@ func GetRepo(dir string) (*Repo, error) {
return nil, err
}

return &Repo{Repository: *repo, Origin: origin[0], Remotes: remotes, Branches: branches}, nil
return &Repo{Repository: *repo, Origin: origin[0], Remotes: remotes, Branches: branches, Status: status}, nil
}

func getUnstagedStatus(repo *gitm.Repository) (gitm.NameStatus, error) {
cmd := gitm.NewCommand("diff", "HEAD", "--name-status")
stdout, err := cmd.RunInDir(repo.Path())
if err != nil {
return gitm.NameStatus{}, err
}
status := gitm.NameStatus{}
scanner := bufio.NewScanner(bytes.NewReader(stdout))
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 2 {
continue
}

switch fields[0][0] {
case 'A':
status.Added = append(status.Added, fields[1])
case 'D':
status.Removed = append(status.Removed, fields[1])
case 'M':
status.Modified = append(status.Modified, fields[1])
}
}
return status, err
}

func FetchRepo(dir string) (*Repo, error) {
Expand Down
17 changes: 17 additions & 0 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,20 @@ func (m Model) GetTotalCount() *int {
func (m Model) IsLoading() bool {
return m.Table.IsLoading()
}

func (m Model) GetPagerContent() string {
pagerContent := ""
if m.TotalCount > 0 {
pagerContent = fmt.Sprintf(
"%v %v • %v %v/%v • Fetched %v",
constants.WaitingIcon,
m.LastUpdated().Format("01/02 15:04:05"),
m.SingularForm,
m.Table.GetCurrItem()+1,
m.TotalCount,
len(m.Table.Rows),
)
}
pager := m.Ctx.Styles.ListViewPort.PagerStyle.Render(pagerContent)
return pager
}
17 changes: 17 additions & 0 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,20 @@ func (m Model) GetTotalCount() *int {
func (m Model) IsLoading() bool {
return m.Table.IsLoading()
}

func (m Model) GetPagerContent() string {
pagerContent := ""
if m.TotalCount > 0 {
pagerContent = fmt.Sprintf(
"%v %v • %v %v/%v • Fetched %v",
constants.WaitingIcon,
m.LastUpdated().Format("01/02 15:04:05"),
m.SingularForm,
m.Table.GetCurrItem()+1,
m.TotalCount,
len(m.Table.Rows),
)
}
pager := m.Ctx.Styles.ListViewPort.PagerStyle.Render(pagerContent)
return pager
}
10 changes: 10 additions & 0 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reposection

import (
"fmt"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -537,3 +538,12 @@ func (m *Model) GetTotalCount() *int {

return utils.IntPtr(len(m.Branches))
}

func (m *Model) GetPagerContent() string {
s := lipgloss.NewStyle().Background(m.Ctx.Styles.ListViewPort.PagerStyle.GetBackground())
mod := s.Foreground(lipgloss.Color("#e0af68")).Render(fmt.Sprintf(" %d", len(m.repo.Status.Modified)))
plus := s.Foreground(m.Ctx.Theme.SuccessText).Render(fmt.Sprintf(" %d", len(m.repo.Status.Added)))
minus := s.Foreground(m.Ctx.Theme.WarningText).Render(fmt.Sprintf(" %d", len(m.repo.Status.Removed)))
spacer := s.Render(" ")
return m.Ctx.Styles.ListViewPort.PagerStyle.Render(lipgloss.JoinHorizontal(lipgloss.Top, plus, spacer, minus, spacer, mod))
}
17 changes: 0 additions & 17 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,23 +330,6 @@ func (m *BaseModel) IsLoading() bool {
return m.Table.IsLoading()
}

func (m *BaseModel) GetPagerContent() string {
pagerContent := ""
if m.TotalCount > 0 {
pagerContent = fmt.Sprintf(
"%v %v • %v %v/%v • Fetched %v",
constants.WaitingIcon,
m.LastUpdated().Format("01/02 15:04:05"),
m.SingularForm,
m.Table.GetCurrItem()+1,
m.TotalCount,
len(m.Table.Rows),
)
}
pager := m.Ctx.Styles.ListViewPort.PagerStyle.Render(pagerContent)
return pager
}

func (m *BaseModel) GetPromptConfirmation() string {
if m.IsPromptConfirmationShown {
var prompt string
Expand Down

0 comments on commit f22047c

Please sign in to comment.