Skip to content

Commit

Permalink
feat(branch): support switching to/from (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr authored Aug 23, 2024
1 parent 3c09b32 commit 0cd42da
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 345 deletions.
2 changes: 1 addition & 1 deletion ui/components/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (b *Branch) renderExtendedTitle(isSelected bool) string {
title = baseStyle.Foreground(b.Ctx.Theme.SecondaryText).Width(width).MaxWidth(width).Render(title)
name := b.Data.Name
if b.Data.IsCheckedOut {
name = baseStyle.Foreground(b.Ctx.Theme.SuccessText).Render(" " + name)
name = baseStyle.Foreground(b.Ctx.Theme.SuccessText).Render(name)
} else {
name = baseStyle.Foreground(b.Ctx.Theme.PrimaryText).Render(name)
}
Expand Down
17 changes: 10 additions & 7 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ func NewModel(
) Model {
m := Model{}
m.BaseModel = section.NewModel(
id,
ctx,
cfg.ToSectionConfig(),
SectionType,
GetSectionColumns(cfg, ctx),
m.GetItemSingularForm(),
m.GetItemPluralForm(),
lastUpdated,
section.NewSectionOptions{
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
IsSearchSupported: true,
},
)
m.Issues = []data.IssueData{}

Expand Down
3 changes: 3 additions & 0 deletions ui/components/listviewport/listviewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (m *Model) SyncViewPort(content string) {
}

func (m *Model) getNumPrsPerPage() int {
if m.ListItemHeight == 0 {
return 0
}
return m.viewport.Height / m.ListItemHeight
}

Expand Down
17 changes: 10 additions & 7 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ func NewModel(
) Model {
m := Model{}
m.BaseModel = section.NewModel(
id,
ctx,
cfg.ToSectionConfig(),
SectionType,
GetSectionColumns(cfg, ctx),
m.GetItemSingularForm(),
m.GetItemPluralForm(),
lastUpdated,
section.NewSectionOptions{
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
IsSearchSupported: true,
},
)
m.Prs = []data.PullRequestData{}

Expand Down
6 changes: 3 additions & 3 deletions ui/components/reposection/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (m *Model) checkout() (tea.Cmd, error) {
b := m.GetCurrBranch()
b := m.getCurrBranch()

taskId := fmt.Sprintf("checkout_%s_%d", b.Data.Name, time.Now().Unix())
task := context.Task{
Expand All @@ -35,8 +35,8 @@ func (m *Model) checkout() (tea.Cmd, error) {
}

return constants.TaskFinishedMsg{
SectionId: m.Id,
SectionType: m.Type,
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Err: err,
Expand Down
100 changes: 100 additions & 0 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package reposection

import (
"fmt"
"time"

tea "github.com/charmbracelet/bubbletea"

"github.com/dlvhdr/gh-dash/v4/data"
"github.com/dlvhdr/gh-dash/v4/git"
"github.com/dlvhdr/gh-dash/v4/ui/constants"
"github.com/dlvhdr/gh-dash/v4/ui/context"
)

type UpdatePRMsg struct {
PrNumber int
IsClosed *bool
NewComment *data.Comment
ReadyForReview *bool
IsMerged *bool
AddedAssignees *data.Assignees
RemovedAssignees *data.Assignees
}

type repoMsg struct {
repo *git.Repo
err error
}

func (m *Model) readRepoCmd() []tea.Cmd {
cmds := make([]tea.Cmd, 0)
branchesTaskId := fmt.Sprintf("fetching_branches_%d", time.Now().Unix())
if m.Ctx.RepoPath != nil {
branchesTask := context.Task{
Id: branchesTaskId,
StartText: "Reading local branches",
FinishedText: fmt.Sprintf(
`Read branches successfully for "%s"`,
*m.Ctx.RepoPath,
),
State: context.TaskStart,
Error: nil,
}
bCmd := m.Ctx.StartTask(branchesTask)
cmds = append(cmds, bCmd)
}
cmds = append(cmds, func() tea.Msg {
repo, err := git.GetRepo(*m.Ctx.RepoPath)
return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: branchesTaskId,
Msg: repoMsg{repo: repo},
Err: err,
}
})
return cmds
}

func (m *Model) fetchPRsCmd() []tea.Cmd {
prsTaskId := fmt.Sprintf("fetching_pr_branches_%d", time.Now().Unix())
cmds := make([]tea.Cmd, 0)
task := context.Task{
Id: prsTaskId,
StartText: "Fetching PRs for your branches",
FinishedText: "PRs for your branches have been fetched",
State: context.TaskStart,
Error: nil,
}
cmds = append(cmds, m.Ctx.StartTask(task))
cmds = append(cmds, func() tea.Msg {
limit := m.Config.Limit
if limit == nil {
limit = &m.Ctx.Config.Defaults.PrsLimit
}
res, err := data.FetchPullRequests("author:@me", *limit, nil)
// TODO: enrich with branches only for section with branches
if err != nil {
return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: prsTaskId,
Err: err,
}
}

return constants.TaskFinishedMsg{
SectionId: 0,
SectionType: SectionType,
TaskId: prsTaskId,
Msg: SectionPullRequestsFetchedMsg{
Prs: res.Prs,
TotalCount: res.TotalCount,
PageInfo: res.PageInfo,
TaskId: prsTaskId,
},
}
})
return cmds
}
6 changes: 3 additions & 3 deletions ui/components/reposection/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (m *Model) delete() (tea.Cmd, error) {
b := m.GetCurrBranch()
b := m.getCurrBranch()

taskId := fmt.Sprintf("delete_%s_%d", b.Data.Name, time.Now().Unix())
task := context.Task{
Expand All @@ -35,8 +35,8 @@ func (m *Model) delete() (tea.Cmd, error) {
}

return constants.TaskFinishedMsg{
SectionId: m.Id,
SectionType: m.Type,
SectionId: 0,
SectionType: SectionType,
TaskId: taskId,
Msg: repoMsg{repo: repo},
Err: err,
Expand Down
Loading

0 comments on commit 0cd42da

Please sign in to comment.