Skip to content

Commit

Permalink
feat: add minor filtering features
Browse files Browse the repository at this point in the history
- Stacks can be filtered
- Diff can be viewed from a filtered state
- Quitting is better
  • Loading branch information
dhth committed Mar 7, 2024
1 parent 789fc87 commit 87be9a1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
12 changes: 12 additions & 0 deletions model/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func newAppItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
Copy()

d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd {
fs := m.FilterState()
switch fs {
case list.FilterApplied:
switch msgType := msg.(type) {
case tea.KeyMsg:
if !key.Matches(msgType, keys.showDiff) {
return nil
}
}
case list.Filtering:
return nil
}
var stack Stack

var cmds []tea.Cmd
Expand Down
5 changes: 3 additions & 2 deletions model/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ func InitialModel(stacks []Stack) model {
appDelegate := newAppItemDelegate(appDelegateKeys)

m := model{
stacksList: list.New(stackItems, appDelegate, listPadding, 0),
stacksList: list.New(stackItems, appDelegate, listWidth, 0),
}
m.stacksList.Title = "Stacks"
m.stacksList.SetFilteringEnabled(false)
m.stacksList.SetStatusBarItemName("stack", "stacks")
m.stacksList.DisableQuitKeybindings()

return m
}
2 changes: 1 addition & 1 deletion model/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
PaddingRight(1).
Foreground(lipgloss.Color("#282828"))

baseListStyle = lipgloss.NewStyle().PaddingTop(1).PaddingRight(2).PaddingLeft(1).PaddingBottom(1).Width(listPadding + 10)
baseListStyle = lipgloss.NewStyle().PaddingTop(1).PaddingRight(2).PaddingLeft(1).PaddingBottom(1).Width(listWidth + 10)

stackListStyle = baseListStyle.Copy()

Expand Down
4 changes: 2 additions & 2 deletions model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Stack struct {
}

func (stack Stack) Title() string {
return fmt.Sprintf("%s", RightPadTrim(stack.Name, listPadding-20))
return fmt.Sprintf("%s", RightPadTrim(stack.Name, listWidth-20))
}
func (stack Stack) Description() string {
var status string
Expand All @@ -51,7 +51,7 @@ func (stack Stack) Description() string {
if stack.Err != nil {
desc = stack.Err.Error()
}
return fmt.Sprintf("@%s %s", RightPadTrim(desc, listPadding-20), status)
return fmt.Sprintf("@%s %s", RightPadTrim(desc, listWidth-20), status)
}
func (stack Stack) FilterValue() string { return stack.Name }

Expand Down
8 changes: 7 additions & 1 deletion model/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"fmt"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)

Expand All @@ -15,7 +16,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
fs := m.stacksList.FilterState()
if fs == list.Filtering || fs == list.FilterApplied {
m.stacksList.ResetFilter()
} else {
return m, tea.Quit
}
}
case tea.WindowSizeMsg:
_, h1 := stackListStyle.GetFrameSize()
Expand Down
2 changes: 1 addition & 1 deletion model/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
listPadding = 100
listWidth = 120
)

func (m model) View() string {
Expand Down

0 comments on commit 87be9a1

Please sign in to comment.