Skip to content

Commit

Permalink
Add stack filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik committed Dec 11, 2024
1 parent fd93692 commit d938f97
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/andybalholm/brotli v0.0.0-20190621154722-5f990b63d2d6 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/input v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAU
github.com/andybalholm/brotli v0.0.0-20190621154722-5f990b63d2d6 h1:bZ28Hqta7TFAK3Q08CMvv8y3/8ATaEqv2nGoc6yff6c=
github.com/andybalholm/brotli v0.0.0-20190621154722-5f990b63d2d6/go.mod h1:+lx6/Aqd1kLJ1GQfkvOnaZ1WGmLpMpbprPuIOOZX30U=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/draw/data/workerpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (q *WorkerPool) Selected(row table.Row) error {
return browser.OpenURL(authenticated.Client.URL("/stack/%s/run/%s", row[1], row[2]))
}

func (q *WorkerPool) Filtered(string) error {
return nil
}

// Columns returns the columns of the worker pool table.
func (q *WorkerPool) Columns() []table.Column {
return []table.Column{
Expand Down
88 changes: 72 additions & 16 deletions internal/cmd/draw/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@ import (
"time"

"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)

// Table is a table that can be drawn.
type Table struct {
table table.Model
td TableData
table table.Model
textInput textinput.Model

td TableData

width int
height int
baseStyle lipgloss.Style

lastErr error
lastErr error
}

type view string

const (
viewTable view = "table"
viewInput view = "input"
)

// TableData is the data for a table.
type TableData interface {
// Columns returns the columns of the table.
Expand All @@ -34,6 +43,8 @@ type TableData interface {
// Selected is called when a row is selected.
// The entire row is passed to the function.
Selected(table.Row) error

Filtered(string) error
}

// NewTable creates a new table.
Expand Down Expand Up @@ -67,14 +78,20 @@ func NewTable(ctx context.Context, d TableData) (*Table, error) {
BorderStyle(lipgloss.ThickBorder()).
BorderForeground(lipgloss.Color("240"))

ti := textinput.New()
ti.Placeholder = "Enter text"
ti.CharLimit = 156
ti.Width = t.Width()

width, height, err := term.GetSize(0)
if err != nil {
return nil, err
}

return &Table{
table: t,
td: d,
table: t,
td: d,
textInput: ti,

width: width,
height: height,
Expand All @@ -96,26 +113,45 @@ func (t *Table) DrawTable() error {
// Init implements tea.Model.Init.
// Should not be called directly.
func (t Table) Init() tea.Cmd {
return tickCmd()
return tickCmd(time.Second * 5)
}

// Update implements tea.Model.Update.
// Should not be called directly.
func (t Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
cmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.WindowSizeMsg:
t.width = msg.Width
t.height = msg.Height
case tea.KeyMsg:
switch msg.String() {
case "esc", "ctrl+c", "q":
return t, tea.Quit
case "ctrl+f":
t.textInput.Focus()
t.table.Blur()
case "esc", "ctrl+c":
if t.table.Focused() {
return t, tea.Quit
} else {
t.table.Focus()

t.textInput.Blur()
t.textInput.Reset()
}
case "enter":
err := t.td.Selected(t.table.SelectedRow())
if err != nil {
return t, t.saveErrorAndExit(err)
if t.table.Focused() {
err := t.td.Selected(t.table.SelectedRow())
if err != nil {
return t, t.saveErrorAndExit(err)
}
}
if t.textInput.Focused() {
err := t.td.Filtered(t.textInput.Value())
if err != nil {
return t, t.saveErrorAndExit(err)
}
}
cmds = append(cmds, tickCmd(time.Microsecond))
}
case tickMsg:
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand All @@ -127,11 +163,17 @@ func (t Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

t.table.SetRows(rows)
return t, tickCmd()
return t, tickCmd(time.Second * 5)
}

var cmd tea.Cmd
t.textInput, cmd = t.textInput.Update(msg)
cmds = append(cmds, cmd)

t.table, cmd = t.table.Update(msg)
return t, cmd
cmds = append(cmds, cmd)

return t, tea.Batch(cmds...)
}

// View implements tea.Model.View.
Expand All @@ -141,10 +183,24 @@ func (t Table) View() string {
return fmt.Sprintln("Exited with an error:", t.lastErr)
}

tableView := t.baseStyle.Render(t.table.View()) + "\n"

filterText := "Filter stacks by name (CTRL+f to start)"
if t.textInput.Focused() {
filterText = "Filter stacks by name (CTRL+c to exit)"
}

filterView := fmt.Sprintf(
"%s\n%s\n",
filterText,
t.textInput.View(),
)

views := []string{tableView, filterView}
return lipgloss.Place(
t.width, t.height,
lipgloss.Center, lipgloss.Center,
t.baseStyle.Render(t.table.View())+"\n",
lipgloss.JoinVertical(lipgloss.Top, views...),
)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/draw/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

type tickMsg time.Time

func tickCmd() tea.Cmd {
return tea.Tick(time.Second*5, func(t time.Time) tea.Msg {
func tickCmd(d time.Duration) tea.Cmd {
return tea.Tick(d, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
18 changes: 16 additions & 2 deletions internal/cmd/stack/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package stack
import (
"context"
"fmt"
"strings"
"time"

"github.com/charmbracelet/bubbles/table"
"github.com/pkg/browser"
"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/client/structs"
"github.com/spacelift-io/spacectl/internal/cmd"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
"github.com/spacelift-io/spacectl/internal/cmd/draw"
"github.com/urfave/cli/v2"
"strings"
"time"
)

func watch(cliCtx *cli.Context) error {
Expand All @@ -36,6 +38,14 @@ type Stacks struct {
si structs.SearchInput
}

// Selected opens the selected worker pool in the browser.
func (q *Stacks) Filtered(s string) error {
fullTextSearch := graphql.NewString(graphql.String(s))
q.si.FullTextSearch = fullTextSearch

return nil
}

// Selected opens the selected worker pool in the browser.
func (q *Stacks) Selected(row table.Row) error {
ctx := context.Background()
Expand Down Expand Up @@ -87,6 +97,10 @@ type StackWatch struct {
id string
}

func (w *StackWatch) Filtered(s string) error {
return nil
}

func (s *StackWatch) Columns() []table.Column {
return []table.Column{
{Title: "Run ID", Width: 25},
Expand Down

0 comments on commit d938f97

Please sign in to comment.