Skip to content

Commit

Permalink
feat: support showing the number of results in tab bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Jun 27, 2024
1 parent da1221b commit 41880ec
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ If you choose to go this route, you need to specify _all_ of the following keys
```yaml
theme:
ui:
sectionsShowCount: true
table:
showSeparator: true
colors:
Expand Down
4 changes: 3 additions & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ type TableUIThemeConfig struct {
}

type UIThemeConfig struct {
Table TableUIThemeConfig `yaml:"table"`
SectionsShowCount bool `yaml:"sectionsShowCount" default:"true"`
Table TableUIThemeConfig `yaml:"table"`
}

type ThemeConfig struct {
Expand Down Expand Up @@ -280,6 +281,7 @@ func (parser ConfigParser) getDefaultConfig() Config {
RepoPaths: map[string]string{},
Theme: &ThemeConfig{
Ui: UIThemeConfig{
SectionsShowCount: true,
Table: TableUIThemeConfig{
ShowSeparator: true,
},
Expand Down
10 changes: 10 additions & 0 deletions docs/data/schemas/theme.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ properties:
skip_schema_render: true
format: yaml
properties:
sectionsShowCount:
title: Sections Show Count
description: >-
Whether the number of results show up next to each section's title in the tab bar.
type: boolean
default: true
schematize:
skip_schema_render: true
format: yaml
showSeparators:
title: Show Separators
description: >-
Expand Down Expand Up @@ -366,6 +375,7 @@ properties:
pattern: ^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
default:
ui:
sectionsShowCount: true
table:
showSeparators: true
colors:
Expand Down
11 changes: 11 additions & 0 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,14 @@ func (m Model) GetItemSingularForm() string {
func (m Model) GetItemPluralForm() string {
return "Issues"
}

func (m Model) GetTotalCount() *int {
if m.IsLoading() {
return nil
}
return &m.TotalCount
}

func (m Model) IsLoading() bool {
return m.Table.IsLoading()
}
27 changes: 19 additions & 8 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,6 @@ func FetchAllSections(
return sections, tea.Batch(fetchPRsCmds...)
}

func (m Model) GetItemSingularForm() string {
return "PR"
}

func (m Model) GetItemPluralForm() string {
return "PRs"
}

type UpdatePRMsg struct {
PrNumber int
IsClosed *bool
Expand Down Expand Up @@ -448,3 +440,22 @@ func assigneesContains(assignees []data.Assignee, assignee data.Assignee) bool {
}
return false
}

func (m Model) GetItemSingularForm() string {
return "PR"
}

func (m Model) GetItemPluralForm() string {
return "PRs"
}

func (m Model) GetTotalCount() *int {
if m.IsLoading() {
return nil
}
return &m.TotalCount
}

func (m Model) IsLoading() bool {
return m.Table.IsLoading()
}
2 changes: 2 additions & 0 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Section interface {
GetPagerContent() string
GetItemSingularForm() string
GetItemPluralForm() string
GetTotalCount() *int
}

type Identifier interface {
Expand All @@ -120,6 +121,7 @@ type Table interface {
FetchNextPageSectionRows() []tea.Cmd
BuildRows() []table.Row
ResetRows()
IsLoading() bool
}

type Search interface {
Expand Down
4 changes: 4 additions & 0 deletions ui/components/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,7 @@ func (m *Model) UpdateLastUpdated(t time.Time) {
func (m *Model) UpdateTotalItemsCount(count int) {
m.rowsViewport.SetTotalItems(count)
}

func (m *Model) IsLoading() bool {
return m.isLoading
}
33 changes: 27 additions & 6 deletions ui/components/tabs/tabs.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package tabs

import (
"fmt"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/dlvhdr/gh-dash/v4/config"
"github.com/dlvhdr/gh-dash/v4/ui/components/section"
"github.com/dlvhdr/gh-dash/v4/ui/context"
"github.com/dlvhdr/gh-dash/v4/utils"
)

type Model struct {
CurrSectionId int
sectionsConfigs []config.SectionConfig
sectionCounts []*int
CurrSectionId int
}

func NewModel() Model {
func NewModel(ctx *context.ProgramContext) Model {
return Model{
CurrSectionId: 1,
}
Expand All @@ -24,10 +30,14 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

func (m Model) View(ctx context.ProgramContext) string {
sectionsConfigs := ctx.GetViewSectionsConfig()
sectionTitles := make([]string, 0, len(sectionsConfigs))
for _, section := range sectionsConfigs {
sectionTitles = append(sectionTitles, section.Title)
sectionTitles := make([]string, 0, len(m.sectionsConfigs))
for i, section := range m.sectionsConfigs {
title := section.Title
// handle search section
if i > 0 && m.sectionCounts[i] != nil && ctx.Config.Theme.Ui.SectionsShowCount {
title = fmt.Sprintf("%s (%s)", title, utils.ShortNumber(*m.sectionCounts[i]))
}
sectionTitles = append(sectionTitles, title)
}

var tabs []string
Expand All @@ -53,3 +63,14 @@ func (m Model) View(ctx context.ProgramContext) string {
func (m *Model) SetCurrSectionId(id int) {
m.CurrSectionId = id
}

func (m *Model) UpdateSectionsConfigs(ctx *context.ProgramContext) {
m.sectionsConfigs = ctx.GetViewSectionsConfig()
}

func (m *Model) UpdateSectionCounts(sections []section.Section) {
m.sectionCounts = make([]*int, len(sections))
for i, s := range sections {
m.sectionCounts[i] = s.GetTotalCount()
}
}
10 changes: 8 additions & 2 deletions ui/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"

"github.com/dlvhdr/gh-dash/v4/config"
"github.com/dlvhdr/gh-dash/v4/ui/theme"
Expand Down Expand Up @@ -44,11 +45,16 @@ type ProgramContext struct {

func (ctx *ProgramContext) GetViewSectionsConfig() []config.SectionConfig {
var configs []config.SectionConfig
if ctx.View == config.PRsView {
log.Debug("View", "view", ctx.View)
switch ctx.View {
case config.PRsView:
log.Debug("sections", "prs", ctx.Config.PRSections)
for _, cfg := range ctx.Config.PRSections {
configs = append(configs, cfg.ToSectionConfig())
}
} else {
case config.IssuesView:
log.Debug("HelPPPPPPPPPPPPPPP", "config", ctx.Config, "view", ctx.View)
log.Debug("sections", "issues", ctx.Config.IssuesSections)
for _, cfg := range ctx.Config.IssuesSections {
configs = append(configs, cfg.ToSectionConfig())
}
Expand Down
13 changes: 11 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ type Model struct {
}

func NewModel(configPath string) Model {
tabsModel := tabs.NewModel()
taskSpinner := spinner.Model{Spinner: spinner.Dot}
m := Model{
keys: keys.Keys,
currSectionId: 1,
tabs: tabsModel,
sidebar: sidebar.NewModel(),
taskSpinner: taskSpinner,
tasks: map[string]context.Task{},
Expand All @@ -77,6 +75,7 @@ func NewModel(configPath string) Model {
m.footer = footer
m.prSidebar = prsidebar.NewModel(m.ctx)
m.issueSidebar = issuesidebar.NewModel(m.ctx)
m.tabs = tabs.NewModel(&m.ctx)

return m
}
Expand Down Expand Up @@ -131,6 +130,7 @@ func (m Model) Init() tea.Cmd {
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
tabsCmd tea.Cmd
sidebarCmd tea.Cmd
prSidebarCmd tea.Cmd
issueSidebarCmd tea.Cmd
Expand Down Expand Up @@ -328,6 +328,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.View = m.switchSelectedView()
m.syncMainContentWidth()
m.setCurrSectionId(1)
m.tabs.UpdateSectionsConfigs(&m.ctx)

currSections := m.getCurrentViewSections()
if len(currSections) == 0 {
Expand Down Expand Up @@ -381,6 +382,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.View = m.switchSelectedView()
m.syncMainContentWidth()
m.setCurrSectionId(1)
m.tabs.UpdateSectionsConfigs(&m.ctx)

currSections := m.getCurrentViewSections()
if len(currSections) == 0 {
Expand All @@ -396,8 +398,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ctx.Config = &msg.Config
m.ctx.Theme = theme.ParseTheme(m.ctx.Config)
m.ctx.Styles = context.InitStyles(m.ctx.Theme)
log.Debug("Config loaded", "default view", m.ctx.Config.Defaults.View)
log.Debug("ui.ui initMsg", "ctx", m.ctx)
m.ctx.View = m.ctx.Config.Defaults.View
log.Debug("View set", "view", m.ctx.View)
m.sidebar.IsOpen = msg.Config.Defaults.Preview.Open
m.tabs.UpdateSectionsConfigs(&m.ctx)
m.syncMainContentWidth()
newSections, fetchSectionsCmds := m.fetchAllViewSections()
m.setCurrentViewSections(newSections)
Expand Down Expand Up @@ -488,10 +494,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

m.tabs.UpdateSectionCounts(m.getCurrentViewSections())

sectionCmd := m.updateCurrentSection(msg)
cmds = append(
cmds,
cmd,
tabsCmd,
sidebarCmd,
footerCmd,
sectionCmd,
Expand Down
21 changes: 18 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,22 @@ func TimeElapsed(then time.Time) string {
return parts[0] + text
}

func BoolPtr(b bool) *bool { return &b }
func BoolPtr(b bool) *bool { return &b }

func StringPtr(s string) *string { return &s }
func UintPtr(u uint) *uint { return &u }
func IntPtr(u int) *int { return &u }

func UintPtr(u uint) *uint { return &u }

func IntPtr(u int) *int { return &u }

func ShortNumber(n int) string {
if n < 1000 {
return strconv.Itoa(n)
}

if n < 1000000 {
return strconv.Itoa(n/1000) + "k"
}

return strconv.Itoa(n/1000000) + "m"
}

0 comments on commit 41880ec

Please sign in to comment.