Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(branches): search #428

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ func NewModel(
m.BaseModel = section.NewModel(
ctx,
section.NewSectionOptions{
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
IsSearchSupported: true,
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
},
)
m.Issues = []data.IssueData{}
Expand Down
15 changes: 7 additions & 8 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ func NewModel(
m.BaseModel = section.NewModel(
ctx,
section.NewSectionOptions{
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
IsSearchSupported: true,
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(cfg, ctx),
Singular: m.GetItemSingularForm(),
Plural: m.GetItemPluralForm(),
LastUpdated: lastUpdated,
},
)
m.Prs = []data.PullRequestData{}
Expand Down
4 changes: 2 additions & 2 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func (m *Model) fetchPRsCmd() tea.Cmd {
prsTaskId := fmt.Sprintf("fetching_pr_branches_%d", time.Now().Unix())
task := context.Task{
Id: prsTaskId,
StartText: "Fetching PRs for your branches",
FinishedText: "PRs for your branches have been fetched",
StartText: "Fetching PRs",
FinishedText: "PRs fetched",
State: context.TaskStart,
Error: nil,
}
Expand Down
87 changes: 57 additions & 30 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/dlvhdr/gh-dash/v4/config"
"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/search"
"github.com/dlvhdr/gh-dash/v4/ui/components/section"
"github.com/dlvhdr/gh-dash/v4/ui/components/table"
"github.com/dlvhdr/gh-dash/v4/ui/constants"
Expand Down Expand Up @@ -41,16 +43,17 @@ func NewModel(
m.BaseModel = section.NewModel(
ctx,
section.NewSectionOptions{
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(ctx, cfg),
Singular: "branch",
Plural: "branches",
LastUpdated: lastUpdated,
IsSearchSupported: false,
Id: id,
Config: cfg.ToSectionConfig(),
Type: SectionType,
Columns: GetSectionColumns(ctx, cfg),
Singular: "branch",
Plural: "branches",
LastUpdated: lastUpdated,
},
)
m.SearchBar = search.NewModel(ctx, search.SearchOptions{Placeholder: "Search branches"})
m.SearchValue = ""
m.repo = &git.Repo{Branches: []git.Branch{}}
m.Branches = []branch.Branch{}
m.Prs = []data.PullRequestData{}
Expand All @@ -61,13 +64,32 @@ func NewModel(

func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
cmds := make([]tea.Cmd, 0)
var err error

switch msg := msg.(type) {

case tea.KeyMsg:

if m.IsSearchFocused() {
switch {

case msg.Type == tea.KeyCtrlC, msg.Type == tea.KeyEsc:
m.SearchBar.SetValue(m.SearchValue)
blinkCmd := m.SetIsSearching(false)
return m, blinkCmd

case msg.Type == tea.KeyEnter:
m.Table.ResetCurrItem()
m.SetIsSearching(false)
m.SearchValue = m.SearchBar.Value()
m.BuildRows()
return m, nil
}

break
}

switch {
case key.Matches(msg, keys.BranchKeys.Checkout):
cmd, err = m.checkout()
Expand All @@ -91,30 +113,26 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
case repoMsg:
m.repo = msg.repo
m.Table.SetIsLoading(false)
m.updateBranches()
m.Table.SetRows(m.BuildRows())

case SectionPullRequestsFetchedMsg:
m.Prs = msg.Prs
m.updateBranches()
m.TotalCount = msg.TotalCount
m.PageInfo = &msg.PageInfo
m.Table.SetIsLoading(false)
m.Table.SetRows(m.BuildRows())
m.Table.UpdateLastUpdated(time.Now())
m.UpdateTotalItemsCount(m.TotalCount)

case RefreshBranchesMsg:
cmds = append(cmds, m.onRefreshBranchesMsg()...)

case FetchMsg:
cmds = append(cmds, m.onFetchMsg()...)
}

m.updateBranchesWithPrs()

cmds = append(cmds, cmd)

search, searchCmd := m.SearchBar.Update(msg)
cmds = append(cmds, searchCmd)
m.SearchBar = search

m.Table.SetRows(m.BuildRows())

m.Table.SetRows(m.BuildRows())
table, tableCmd := m.Table.Update(msg)
cmds = append(cmds, tableCmd)
m.Table = table
cmds = append(cmds, tableCmd)

Expand All @@ -135,8 +153,9 @@ func (m *Model) View() string {
} else {
view = m.Table.View()
}

return m.Ctx.Styles.Section.ContainerStyle.Render(
view,
lipgloss.JoinVertical(lipgloss.Left, m.SearchBar.View(*m.Ctx), view),
)
}

Expand Down Expand Up @@ -268,7 +287,7 @@ func GetSectionColumns(
}
}

func (m *Model) updateBranches() {
func (m *Model) updateBranchesWithPrs() {
branches := make([]branch.Branch, 0)
for _, ref := range m.repo.Branches {
b := branch.Branch{Ctx: m.Ctx, Data: ref, Columns: m.Table.Columns}
Expand Down Expand Up @@ -300,12 +319,20 @@ func (m Model) BuildRows() []table.Row {
currItem := m.Table.GetCurrItem()

sorted := m.Branches
filtered := make([]branch.Branch, 0)
for _, b := range sorted {
if strings.Contains(b.Data.Name, m.SearchValue) {
filtered = append(filtered, b)
}
}

for i, b := range sorted {
rows = append(
rows,
b.ToTableRow(currItem == i),
)
for i, b := range filtered {
if strings.Contains(b.Data.Name, m.SearchValue) {
rows = append(
rows,
b.ToTableRow(currItem == i),
)
}
}

if rows == nil {
Expand Down Expand Up @@ -404,7 +431,7 @@ func (m Model) GetDimensions() constants.Dimensions {
}
return constants.Dimensions{
Width: m.Ctx.MainContentWidth - m.Ctx.Styles.Section.ContainerStyle.GetHorizontalPadding(),
Height: m.Ctx.MainContentHeight,
Height: m.Ctx.MainContentHeight - common.SearchHeight,
}
}

Expand Down
21 changes: 13 additions & 8 deletions ui/components/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,34 @@ import (

type Model struct {
ctx *context.ProgramContext
sectionType string
initialValue string
textInput textinput.Model
}

func NewModel(sectionType string, ctx *context.ProgramContext, initialValue string) Model {
prompt := fmt.Sprintf(" is:%s ", sectionType)
type SearchOptions struct {
Prefix string
InitialValue string
Placeholder string
}

func NewModel(ctx *context.ProgramContext, opts SearchOptions) Model {
prompt := fmt.Sprintf(" %s ", opts.Prefix)
ti := textinput.New()
ti.Placeholder = ""
ti.Focus()
ti.Placeholder = opts.Placeholder
ti.Width = getInputWidth(ctx, prompt)
ti.PromptStyle = ti.PromptStyle.Foreground(ctx.Theme.SecondaryText)
ti.Prompt = prompt
ti.TextStyle = ti.TextStyle.Faint(true)
ti.Cursor.Style = ti.Cursor.Style.Faint(true)
ti.Cursor.TextStyle = ti.Cursor.TextStyle.Faint(true)
ti.Blur()
ti.SetValue(initialValue)
ti.SetValue(opts.InitialValue)
ti.CursorStart()

return Model{
ctx: ctx,
textInput: ti,
initialValue: initialValue,
sectionType: sectionType,
initialValue: opts.InitialValue,
}
}

Expand Down
43 changes: 20 additions & 23 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,37 @@ type BaseModel struct {
}

type NewSectionOptions struct {
Id int
Config config.SectionConfig
IsSearchSupported bool
Type string
Columns []table.Column
Singular string
Plural string
LastUpdated time.Time
Id int
Config config.SectionConfig
Type string
Columns []table.Column
Singular string
Plural string
LastUpdated time.Time
}

func NewModel(
ctx *context.ProgramContext,
options NewSectionOptions,
) BaseModel {
m := BaseModel{
Ctx: ctx,
Id: options.Id,
Type: options.Type,
Config: options.Config,
Spinner: spinner.Model{Spinner: spinner.Dot},
Columns: options.Columns,
SingularForm: options.Singular,
PluralForm: options.Plural,
SearchBar: search.NewModel(options.Type, ctx, options.Config.Filters),
Ctx: ctx,
Id: options.Id,
Type: options.Type,
Config: options.Config,
Spinner: spinner.Model{Spinner: spinner.Dot},
Columns: options.Columns,
SingularForm: options.Singular,
PluralForm: options.Plural,
SearchBar: search.NewModel(ctx, search.SearchOptions{
Prefix: fmt.Sprintf("is:%s", options.Type),
InitialValue: options.Config.Filters,
}),
SearchValue: options.Config.Filters,
IsSearching: false,
TotalCount: 0,
PageInfo: nil,
PromptConfirmationBox: prompt.NewModel(ctx),
IsSearchSupported: options.IsSearchSupported,
}
m.Table = table.NewModel(
*ctx,
Expand Down Expand Up @@ -301,11 +302,7 @@ func (m *BaseModel) GetMainContent() string {
}

func (m *BaseModel) View() string {
search := ""
if m.IsSearchSupported {
search = m.SearchBar.View(*m.Ctx)
}

search := m.SearchBar.View(*m.Ctx)
return m.Ctx.Styles.Section.ContainerStyle.Render(
lipgloss.JoinVertical(
lipgloss.Left,
Expand Down
Loading