Skip to content

Commit

Permalink
Configurable limit (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr authored Jan 14, 2022
1 parent 4a82c44 commit b471bb8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 25 deletions.
5 changes: 4 additions & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type PRSectionConfig struct {
Title string
Filters string
Limit *int `yaml:"limit,omitempty"`
}

type PreviewConfig struct {
Expand All @@ -20,7 +21,8 @@ type PreviewConfig struct {
}

type Defaults struct {
Preview PreviewConfig
Preview PreviewConfig
PrsLimit int `yaml:"prsLimit"`
}

type Config struct {
Expand All @@ -46,6 +48,7 @@ func (parser ConfigParser) getDefaultConfig() Config {
Open: true,
Width: 50,
},
PrsLimit: 20,
},
PRSections: []PRSectionConfig{
{
Expand Down
8 changes: 2 additions & 6 deletions data/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
graphql "github.com/cli/shurcooL-graphql"
)

const (
Limit = 20
)

type PullRequestData struct {
Number int
Title string
Expand Down Expand Up @@ -114,7 +110,7 @@ func makeQuery(query string) string {
return fmt.Sprintf("is:pr %s", query)
}

func FetchRepoPullRequests(query string) ([]PullRequestData, error) {
func FetchRepoPullRequests(query string, limit int) ([]PullRequestData, error) {
var err error
client, err := gh.GQLClient(nil)
if err != nil {
Expand All @@ -131,7 +127,7 @@ func FetchRepoPullRequests(query string) ([]PullRequestData, error) {
}
variables := map[string]interface{}{
"query": graphql.String(makeQuery(query)),
"limit": graphql.Int(Limit),
"limit": graphql.Int(limit),
}
err = client.Query("SearchPullRequests", &queryResult, variables)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions ui/mainViewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (m *Model) onLineDown() {
m.mainViewport.bottomBoundId += 1
m.mainViewport.model.LineDown(prRowHeight)
}

m.sidebarViewport.YOffset = 0
}

func (m *Model) onLineUp() {
Expand All @@ -58,6 +60,7 @@ func (m *Model) onLineUp() {
m.mainViewport.bottomBoundId -= 1
m.mainViewport.model.LineUp(prRowHeight)
}
m.sidebarViewport.YOffset = 0
}

func (m *Model) RenderMainViewPort() string {
Expand Down
11 changes: 9 additions & 2 deletions ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ func NewModel(logFile *os.File) Model {

func (m *Model) updateOnConfigFetched(config config.Config) {
m.config = &config
var data []section
var data []Section
for i, sectionConfig := range m.config.PRSections {
s := spinner.Model{Spinner: spinner.Dot}
data = append(data, section{
data = append(data, Section{
Id: i,
Config: sectionConfig,
Spinner: s,
IsLoading: true,
Limit: func() int {
if sectionConfig.Limit != nil {
return *sectionConfig.Limit
}

return m.config.Defaults.PrsLimit
}(),
})
}
m.data = &data
Expand Down
4 changes: 2 additions & 2 deletions ui/modelUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ui

import "github.com/dlvhdr/gh-prs/utils"

func (m Model) getCurrSection() *section {
func (m Model) getCurrSection() *Section {
if m.data == nil || len(*m.data) == 0 {
return nil
}
Expand Down Expand Up @@ -43,7 +43,7 @@ func (m *Model) nextPr() {
m.cursor.currPrId = newPrId
}

func (m Model) getSectionAt(id int) *section {
func (m Model) getSectionAt(id int) *Section {
return &(*m.data)[id]
}

Expand Down
19 changes: 10 additions & 9 deletions ui/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ package ui
import (
"fmt"

"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dlvhdr/gh-prs/config"
"github.com/dlvhdr/gh-prs/data"
)

type section struct {
type Section struct {
Id int
Config config.PRSectionConfig
Prs []PullRequest
Spinner spinner.Model
IsLoading bool
Paginator paginator.Model
Limit int
}

type tickMsg struct {
SectionId int
InternalTickMsg tea.Msg
}

func (section *section) Tick(spinnerTickCmd tea.Cmd) func() tea.Msg {
func (section *Section) Tick(spinnerTickCmd tea.Cmd) func() tea.Msg {
return func() tea.Msg {
return tickMsg{
SectionId: section.Id,
Expand All @@ -34,9 +33,9 @@ func (section *section) Tick(spinnerTickCmd tea.Cmd) func() tea.Msg {
}
}

func (section *section) fetchSectionPullRequests() tea.Cmd {
func (section *Section) fetchSectionPullRequests() tea.Cmd {
return func() tea.Msg {
fetched, err := data.FetchRepoPullRequests(section.Config.Filters)
fetched, err := data.FetchRepoPullRequests(section.Config.Filters, section.Limit)
if err != nil {
return sectionPullRequestsFetchedMsg{
SectionId: section.Id,
Expand Down Expand Up @@ -67,14 +66,14 @@ func (m Model) makeRenderPullRequestCmd(sectionId int) tea.Cmd {
}
}

func (section *section) renderLoadingState() string {
func (section *Section) renderLoadingState() string {
if !section.IsLoading {
return ""
}
return spinnerStyle.Render(fmt.Sprintf("%s Fetching Pull Requests...", section.Spinner.View()))
}

func (section *section) renderEmptyState() string {
func (section *Section) renderEmptyState() string {
emptyState := emptyStateStyle.Render(fmt.Sprintf(
"No PRs were found that match the given filters: %s",
lipgloss.NewStyle().Italic(true).Render(section.Config.Filters),
Expand Down Expand Up @@ -102,6 +101,8 @@ func (m *Model) renderTableHeader() string {
Render("Title")

return headerStyle.
PaddingLeft(mainContentPadding).
PaddingRight(mainContentPadding).
Width(m.mainViewport.model.Width).
MaxWidth(m.mainViewport.model.Width).
Render(
Expand Down Expand Up @@ -155,6 +156,6 @@ func (m *Model) renderCurrentSection() string {
Render(m.RenderMainViewPort())
}

func (section section) numPrs() int {
func (section Section) numPrs() int {
return len(section.Prs)
}
9 changes: 7 additions & 2 deletions ui/sidebar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

import (
"fmt"
"regexp"
"strings"

Expand All @@ -18,7 +19,7 @@ func (m *Model) renderSidebar() string {
return ""
}

height := m.sidebarViewport.Height
height := m.sidebarViewport.Height + pagerHeight
style := sideBarStyle.Copy().
Height(height).
MaxHeight(height).
Expand All @@ -32,7 +33,11 @@ func (m *Model) renderSidebar() string {
)
}

return style.Copy().Render(m.sidebarViewport.View())
return style.Copy().Render(lipgloss.JoinVertical(
lipgloss.Left,
m.sidebarViewport.View(),
pagerStyle.Copy().Render(fmt.Sprintf("%d%%", int(m.sidebarViewport.ScrollPercent()*100))),
))
}

func (m *Model) setSidebarViewportContent() {
Expand Down
2 changes: 1 addition & 1 deletion ui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var (
BorderLeft(true).
BorderStyle(lipgloss.Border{
Top: "",
Bottom: " ",
Bottom: "",
Left: "│",
Right: "",
TopLeft: "",
Expand Down
4 changes: 2 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Model struct {
keys utils.KeyMap
err error
config *config.Config
data *[]section
data *[]Section
mainViewport MainViewport
sidebarViewport viewport.Model
cursor cursor
Expand Down Expand Up @@ -123,7 +123,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
utils.OpenBrowser(currPR.Data.Url)
return m, nil
case key.Matches(msg, m.keys.Refresh):
var newData []section
var newData []Section
for _, section := range *m.data {
if section.IsLoading {
return m, nil
Expand Down

0 comments on commit b471bb8

Please sign in to comment.