Skip to content

Commit

Permalink
feat(branches): sidebar skeleton (#451)
Browse files Browse the repository at this point in the history
* feat(branches): sidebar skeleton

* fix: spacing and always use last commit msg in ui
  • Loading branch information
dlvhdr committed Sep 6, 2024
1 parent ba8e0a6 commit 96a5959
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 22 deletions.
8 changes: 3 additions & 5 deletions ui/components/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions ui/components/branch/data.go
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions ui/components/branchsidebar/branchsidebar.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 6 additions & 3 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 15 additions & 14 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down

0 comments on commit 96a5959

Please sign in to comment.