diff --git a/ui/components/branch/branch.go b/ui/components/branch/branch.go index 555336b8..3b6d476f 100644 --- a/ui/components/branch/branch.go +++ b/ui/components/branch/branch.go @@ -178,7 +178,7 @@ func (b *Branch) renderExtendedTitle(isSelected bool) string { baseStyle := b.getBaseStyle(isSelected) width := b.getMaxWidth() - title := b.renderPRTitleOrCommigMsg(isSelected, width) + title := b.renderLastCommigMsg(isSelected, width) branch := b.renderBranch(isSelected, width) return baseStyle.Render(lipgloss.JoinVertical(lipgloss.Left, branch, title)) } @@ -332,12 +332,10 @@ func (b *Branch) renderCommitsAheadBehind(isSelected bool) string { return lipgloss.JoinHorizontal(lipgloss.Top, commitsAhead, commitsBehind) } -func (b *Branch) renderPRTitleOrCommigMsg(isSelected bool, width int) string { +func (b *Branch) renderLastCommigMsg(isSelected bool, width int) string { baseStyle := b.getBaseStyle(isSelected) title := "-" - if b.PR != nil { - title = fmt.Sprintf("#%d %s", b.PR.Number, b.PR.Title) - } else if b.Data.LastCommitMsg != nil { + if b.Data.LastCommitMsg != nil { title = *b.Data.LastCommitMsg } return baseStyle.Foreground(b.Ctx.Theme.SecondaryText).Width(width).MaxWidth(width).Render(title) diff --git a/ui/components/branch/data.go b/ui/components/branch/data.go new file mode 100644 index 00000000..c5b7b34f --- /dev/null +++ b/ui/components/branch/data.go @@ -0,0 +1,39 @@ +package branch + +import ( + "time" + + "github.com/dlvhdr/gh-dash/v4/data" + "github.com/dlvhdr/gh-dash/v4/git" +) + +type BranchData struct { + Data git.Branch + PR *data.PullRequestData +} + +func (b BranchData) GetRepoNameWithOwner() string { + return b.Data.Remotes[0] +} + +func (b BranchData) GetTitle() string { + return b.Data.Name +} + +func (b BranchData) GetNumber() int { + if b.PR == nil { + return 0 + } + return b.PR.Number +} + +func (b BranchData) GetUrl() string { + if b.PR == nil { + return "" + } + return b.PR.Url +} + +func (b BranchData) GetUpdatedAt() time.Time { + return *b.Data.LastUpdatedAt +} diff --git a/ui/components/branchsidebar/branchsidebar.go b/ui/components/branchsidebar/branchsidebar.go new file mode 100644 index 00000000..7e230ffa --- /dev/null +++ b/ui/components/branchsidebar/branchsidebar.go @@ -0,0 +1,50 @@ +package branchsidebar + +import ( + "fmt" + "strings" + + tea "github.com/charmbracelet/bubbletea" + + "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 +} + +func NewModel(ctx context.ProgramContext) Model { + return Model{ + branch: nil, + } +} + +func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { + return m, nil +} + +func (m Model) View() string { + s := strings.Builder{} + + if m.branch == nil { + return "No branch selected" + } + + s.WriteString(m.branch.Data.Name) + if m.branch.PR != nil { + s.WriteString("\n") + s.WriteString(fmt.Sprintf("#%d %s", m.branch.PR.GetNumber(), m.branch.PR.Title)) + } + + return s.String() +} + +func (m *Model) SetRow(b *branch.BranchData) { + m.branch = b +} + +func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) { + m.ctx = ctx +} diff --git a/ui/components/reposection/reposection.go b/ui/components/reposection/reposection.go index bfeabeae..6b169c40 100644 --- a/ui/components/reposection/reposection.go +++ b/ui/components/reposection/reposection.go @@ -454,9 +454,12 @@ func (m *Model) GetCurrRow() data.RowData { if len(m.repo.Branches) == 0 { return nil } - branch := m.repo.Branches[m.Table.GetCurrItem()] - pr := findPRForRef(m.Prs, branch.Name) - return pr + b := m.repo.Branches[m.Table.GetCurrItem()] + pr := findPRForRef(m.Prs, b.Name) + return branch.BranchData{ + Data: b, + PR: pr, + } } func (m *Model) FetchNextPageSectionRows() []tea.Cmd { diff --git a/ui/ui.go b/ui/ui.go index 7ac2832b..8372d1a5 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -19,6 +19,8 @@ import ( "github.com/dlvhdr/gh-dash/v4/data" "github.com/dlvhdr/gh-dash/v4/git" "github.com/dlvhdr/gh-dash/v4/ui/common" + "github.com/dlvhdr/gh-dash/v4/ui/components/branch" + "github.com/dlvhdr/gh-dash/v4/ui/components/branchsidebar" "github.com/dlvhdr/gh-dash/v4/ui/components/footer" "github.com/dlvhdr/gh-dash/v4/ui/components/issuesidebar" "github.com/dlvhdr/gh-dash/v4/ui/components/issuessection" @@ -39,6 +41,7 @@ type Model struct { sidebar sidebar.Model prSidebar prsidebar.Model issueSidebar issuesidebar.Model + branchSidebar branchsidebar.Model currSectionId int footer footer.Model repo section.Section @@ -79,6 +82,7 @@ func NewModel(repoPath *string, configPath string) Model { m.footer = footer m.prSidebar = prsidebar.NewModel(m.ctx) m.issueSidebar = issuesidebar.NewModel(m.ctx) + m.branchSidebar = branchsidebar.NewModel(m.ctx) m.tabs = tabs.NewModel(&m.ctx) return m @@ -593,17 +597,13 @@ func (m Model) View() string { } s.WriteString("\n") content := "No sections defined" - if m.ctx.View == config.RepoView { - content = m.repo.View() - } else { - currSection := m.getCurrSection() - if currSection != nil { - content = lipgloss.JoinHorizontal( - lipgloss.Top, - m.getCurrSection().View(), - m.sidebar.View(), - ) - } + currSection := m.getCurrSection() + if currSection != nil { + content = lipgloss.JoinHorizontal( + lipgloss.Top, + m.getCurrSection().View(), + m.sidebar.View(), + ) } s.WriteString(content) s.WriteString("\n") @@ -660,6 +660,7 @@ func (m *Model) syncProgramContext() { m.sidebar.UpdateProgramContext(&m.ctx) m.prSidebar.UpdateProgramContext(&m.ctx) m.issueSidebar.UpdateProgramContext(&m.ctx) + m.branchSidebar.UpdateProgramContext(&m.ctx) } func (m *Model) updateSection(id int, sType string, msg tea.Msg) (cmd tea.Cmd) { @@ -700,9 +701,6 @@ func (m *Model) syncMainContentWidth() { } func (m *Model) syncSidebar() { - if m.ctx.View == config.RepoView { - return - } currRowData := m.getCurrRowData() width := m.sidebar.GetSidebarContentWidth() @@ -712,6 +710,9 @@ func (m *Model) syncSidebar() { } switch row := currRowData.(type) { + case branch.BranchData: + m.branchSidebar.SetRow(&row) + m.sidebar.SetContent(m.branchSidebar.View()) case *data.PullRequestData: m.prSidebar.SetSectionId(m.currSectionId) m.prSidebar.SetRow(row)