146 lines
3.5 KiB
Go
146 lines
3.5 KiB
Go
package widget
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"git.ohea.xyz/cursorius/tui/queries"
|
|
"github.com/Khan/genqlient/graphql"
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type pipelineWidgetList struct {
|
|
list list.Model
|
|
client graphql.Client
|
|
}
|
|
|
|
func (m pipelineWidgetList) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m pipelineWidgetList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
m.list.SetWidth(msg.Width - 10)
|
|
m.list.SetHeight(msg.Height - 10)
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "down":
|
|
if m.list.Index() < len(m.list.Items())-1 {
|
|
m.list.Select(m.list.Index() + 1)
|
|
} else if m.list.Index() == len(m.list.Items())-1 {
|
|
m.list.Select(0)
|
|
}
|
|
case "up":
|
|
if m.list.Index() > 0 {
|
|
m.list.Select(m.list.Index() - 1)
|
|
} else if m.list.Index() == 0 {
|
|
m.list.Select(len(m.list.Items()) - 1)
|
|
}
|
|
|
|
case "e":
|
|
if m.list.Index() < len(m.list.Items())-1 {
|
|
//item := m.list.SelectedItem()
|
|
//if item, ok := item.(pipelineListItem); ok {
|
|
// return m, func() tea.Cmd {
|
|
// view, err := createPipelineEditForm(client, item.id)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// return pipelineWidgetScreenSwitch(view)
|
|
// }
|
|
//}
|
|
} else {
|
|
return m, func() tea.Msg {
|
|
view, err := createPipelineCreateForm(m.client, m.list.Width(), m.list.Height())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return pipelineWidgetScreenSwitch(view)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m pipelineWidgetList) View() string {
|
|
return m.list.View()
|
|
}
|
|
|
|
type pipelineListItem struct {
|
|
name string
|
|
id string
|
|
}
|
|
|
|
type pipelineCreateListItem string
|
|
|
|
func (i pipelineListItem) FilterValue() string { return "" }
|
|
func (i pipelineCreateListItem) FilterValue() string { return "" }
|
|
|
|
type pipelineListItemDelegate struct{}
|
|
|
|
func (d pipelineListItemDelegate) Height() int { return 1 }
|
|
func (d pipelineListItemDelegate) Spacing() int { return 0 }
|
|
func (d pipelineListItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
|
|
func (d pipelineListItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
|
|
switch i := listItem.(type) {
|
|
case pipelineListItem:
|
|
fn := itemStyle.Render
|
|
faintFn := faintItemStyle.Render
|
|
if index == m.Index() {
|
|
fn = func(s string) string {
|
|
return selectedItemStyle.Render("> " + s)
|
|
}
|
|
faintFn = faintSelectedItemStyle.Render
|
|
}
|
|
|
|
str := fn(i.name) + faintFn(" ("+i.id+")")
|
|
fmt.Fprint(w, str)
|
|
case pipelineCreateListItem:
|
|
fn := itemStyle.Render
|
|
if index == m.Index() {
|
|
fn = func(s string) string {
|
|
return selectedItemStyle.Render("> " + s)
|
|
}
|
|
}
|
|
|
|
str := string(i)
|
|
fmt.Fprint(w, fn(str))
|
|
}
|
|
}
|
|
|
|
func createPipelineWidgetList(client graphql.Client) (tea.Model, error) {
|
|
getPipelineResp, err := queries.GetPipelines(context.Background(), client)
|
|
if err != nil {
|
|
return pipelineWidget{}, fmt.Errorf("Could not connect to server: %w", err)
|
|
}
|
|
|
|
var content []list.Item
|
|
|
|
if getPipelineResp.Pipelines != nil {
|
|
for _, pipeline := range getPipelineResp.Pipelines {
|
|
if pipeline != nil {
|
|
content = append(content, pipelineListItem{
|
|
name: pipeline.Name,
|
|
id: pipeline.Id,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
content = append(content, pipelineCreateListItem("[Create Pipeline]"))
|
|
|
|
pipelineList := list.New(content, pipelineListItemDelegate{}, 0, 0)
|
|
pipelineList.SetShowStatusBar(false)
|
|
pipelineList.Title = "Pipelines"
|
|
|
|
return pipelineWidgetList{
|
|
list: pipelineList,
|
|
client: client,
|
|
}, nil
|
|
}
|