Skip to content

Commit

Permalink
fix: remove full screen loading when refreshing (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr authored Oct 1, 2024
1 parent 8708484 commit a7291c9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
9 changes: 9 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func GetRepo(dir string) (*Repo, error) {
return &Repo{Repository: *repo, Origin: origin[0], Remotes: remotes, HeadBranchName: headBranch, Branches: branches, Status: status}, nil
}

func GetStatus(dir string) (gitm.NameStatus, error) {
repo, err := gitm.Open(dir)
if err != nil {
return gitm.NameStatus{}, err
}
return getUnstagedStatus(repo)
}

// test
func getUnstagedStatus(repo *gitm.Repository) (gitm.NameStatus, error) {
cmd := gitm.NewCommand("diff", "HEAD", "--name-status")
stdout, err := cmd.RunInDir(repo.Path())
Expand Down
52 changes: 51 additions & 1 deletion ui/components/branchsidebar/branchsidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"fmt"
"strings"

gitm "github.com/aymanbagabas/git-module"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/dlvhdr/gh-dash/v4/git"
"github.com/dlvhdr/gh-dash/v4/ui/components/branch"
"github.com/dlvhdr/gh-dash/v4/ui/context"
)

type Model struct {
ctx *context.ProgramContext
branch *branch.BranchData
status *gitm.NameStatus
}

func NewModel(ctx context.ProgramContext) Model {
Expand All @@ -22,12 +26,43 @@ func NewModel(ctx context.ProgramContext) Model {
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case updateBranchStatusMsg:
m.status = &msg.status
return m, nil
}
return m, nil
}

func (m Model) View() string {
s := strings.Builder{}

s.WriteString(lipgloss.NewStyle().Bold(true).Render("STATUS\n"))
if m.status == nil {
s.WriteString("\nLoading...")
} else {

if len(m.status.Added) == 0 && len(m.status.Removed) == 0 && len(m.status.Modified) == 0 {
s.WriteString("\nNo changes")
}

for _, file := range m.status.Added {
s.WriteString(fmt.Sprintf("\nA %s", file))
}
for _, file := range m.status.Removed {
s.WriteString(fmt.Sprintf("\nD %s", file))
}
for _, file := range m.status.Modified {
s.WriteString(fmt.Sprintf("\nM %s", file))
}
}

s.WriteString("\n\n")
s.WriteString(lipgloss.NewStyle().Foreground(m.ctx.Theme.FaintBorder).Render(
strings.Repeat(lipgloss.NormalBorder().Bottom, m.ctx.Config.Defaults.Preview.Width-5)),
)
s.WriteString("\n\n")

if m.branch == nil {
return "No branch selected"
}
Expand All @@ -41,8 +76,23 @@ func (m Model) View() string {
return s.String()
}

func (m *Model) SetRow(b *branch.BranchData) {
type updateBranchStatusMsg struct {
status gitm.NameStatus
}

func (m *Model) SetRow(b *branch.BranchData) tea.Cmd {
m.branch = b
return m.refreshBranchStatusCmd
}

func (m *Model) refreshBranchStatusCmd() tea.Msg {
status, err := git.GetStatus(*m.ctx.RepoPath)
if err != nil {
return nil
}
return updateBranchStatusMsg{
status: status,
}
}

func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
Expand Down
2 changes: 0 additions & 2 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
cmds = append(cmds, m.fetchPRsCmd())
}

m.Table.SetIsLoading(true)
cmds = append(cmds, m.Table.StartLoadingSpinner())
return cmds
}

Expand Down
31 changes: 19 additions & 12 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,22 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if prevRow != nextRow && nextRow == currSection.NumRows()-1 && m.ctx.View != config.RepoView {
cmds = append(cmds, currSection.FetchNextPageSectionRows()...)
}
m.onViewedRowChanged()
cmd = m.onViewedRowChanged()

case key.Matches(msg, m.keys.Up):
currSection.PrevRow()
m.onViewedRowChanged()

case key.Matches(msg, m.keys.FirstLine):
currSection.FirstItem()
m.onViewedRowChanged()
cmd = m.onViewedRowChanged()

case key.Matches(msg, m.keys.LastLine):
if currSection.CurrRow()+1 < currSection.NumRows() {
cmds = append(cmds, currSection.FetchNextPageSectionRows()...)
}
currSection.LastItem()
m.onViewedRowChanged()
cmd = m.onViewedRowChanged()

case key.Matches(msg, m.keys.TogglePreview):
m.sidebar.IsOpen = !m.sidebar.IsOpen
Expand Down Expand Up @@ -514,7 +514,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
scmd := m.updateSection(msg.SectionId, msg.SectionType, msg.Msg)
cmds = append(cmds, scmd)

m.syncSidebar()
syncCmd := m.syncSidebar()
cmds = append(cmds, syncCmd)
}

case spinner.TickMsg:
Expand All @@ -538,9 +539,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case execProcessFinishedMsg, tea.FocusMsg:
newSections, fetchSectionsCmds := m.fetchAllViewSections()
m.setCurrentViewSections(newSections)
cmds = append(cmds, fetchSectionsCmds)
cmds = append(cmds, currSection.FetchNextPageSectionRows()...)

case tea.WindowSizeMsg:
m.onWindowSizeChanged(msg)
Expand All @@ -551,6 +550,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.syncProgramContext()

var bsCmd tea.Cmd
m.branchSidebar, bsCmd = m.branchSidebar.Update(msg)
cmds = append(cmds, bsCmd)

m.sidebar, sidebarCmd = m.sidebar.Update(msg)

if m.prSidebar.IsTextInputBoxFocused() {
Expand Down Expand Up @@ -640,9 +643,10 @@ func (m *Model) setCurrSectionId(newSectionId int) {
m.tabs.SetCurrSectionId(newSectionId)
}

func (m *Model) onViewedRowChanged() {
m.syncSidebar()
func (m *Model) onViewedRowChanged() tea.Cmd {
cmd := m.syncSidebar()
m.sidebar.ScrollToTop()
return cmd
}

func (m *Model) onWindowSizeChanged(msg tea.WindowSizeMsg) {
Expand Down Expand Up @@ -705,18 +709,19 @@ func (m *Model) syncMainContentWidth() {
m.ctx.MainContentWidth = m.ctx.ScreenWidth - sideBarOffset
}

func (m *Model) syncSidebar() {
func (m *Model) syncSidebar() tea.Cmd {
currRowData := m.getCurrRowData()
width := m.sidebar.GetSidebarContentWidth()
var cmd tea.Cmd

if currRowData == nil {
m.sidebar.SetContent("")
return
return nil
}

switch row := currRowData.(type) {
case branch.BranchData:
m.branchSidebar.SetRow(&row)
cmd = m.branchSidebar.SetRow(&row)
m.sidebar.SetContent(m.branchSidebar.View())
case *data.PullRequestData:
m.prSidebar.SetSectionId(m.currSectionId)
Expand All @@ -729,6 +734,8 @@ func (m *Model) syncSidebar() {
m.issueSidebar.SetWidth(width)
m.sidebar.SetContent(m.issueSidebar.View())
}

return cmd
}

func (m *Model) fetchAllViewSections() ([]section.Section, tea.Cmd) {
Expand Down

0 comments on commit a7291c9

Please sign in to comment.