Add pipeline creation UI

This commit is contained in:
2023-02-24 18:30:39 -07:00
parent 5c1c1478ee
commit 0710306800
3 changed files with 100 additions and 47 deletions
+4
View File
@@ -24,6 +24,10 @@ func (m pipelineWidget) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.height = msg.Height
case pipelineWidgetScreenSwitch:
m.currentView = msg
m.currentView, _ = m.currentView.Update(tea.WindowSizeMsg{
Width: m.width,
Height: m.height,
})
}
var cmd tea.Cmd
+51 -45
View File
@@ -1,9 +1,11 @@
package widget
import (
"context"
"fmt"
"io"
"git.ohea.xyz/cursorius/tui/queries"
"github.com/Khan/genqlient/graphql"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
@@ -11,7 +13,8 @@ import (
)
type pipelineCreateForm struct {
list list.Model
formList list.Model
client graphql.Client
}
func (m pipelineCreateForm) Init() tea.Cmd {
@@ -22,25 +25,64 @@ func (m pipelineCreateForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
return m, func() tea.Msg {
view, err := createPipelineWidgetList(m.client)
if err != nil {
return err
}
return pipelineWidgetScreenSwitch(view)
}
case "enter":
// TODO: enable submitting
if m.formList.Index() == len(m.formList.Items())-1 {
return m, func() tea.Msg {
name := ""
url := ""
pollIntervalStr := ""
if e, ok := m.formList.Items()[0].(createPipelineItem); ok {
name = e.field.Value()
}
if e, ok := m.formList.Items()[1].(createPipelineItem); ok {
url = e.field.Value()
}
if e, ok := m.formList.Items()[2].(createPipelineItem); ok {
pollIntervalStr = e.field.Value()
}
var pollInterval *int
if pollIntervalStr == "" {
pollInterval = nil
}
_, err := queries.CreatePipeline(context.Background(), m.client, name, url, pollInterval, nil)
if err != nil {
return err
}
view, err := createPipelineWidgetList(m.client)
if err != nil {
return err
}
return pipelineWidgetScreenSwitch(view)
}
}
}
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
m.formList, cmd = m.formList.Update(msg)
items := m.list.Items()
items := m.formList.Items()
for i := 0; i < len(items); i++ {
if e, ok := items[i].(createPipelineItem); ok {
if i == m.list.Index() {
if i == m.formList.Index() {
e.field.Focus()
e.field, _ = e.field.Update(msg)
} else {
e.field.Blur()
}
m.list.SetItem(i, e)
m.formList.SetItem(i, e)
}
}
@@ -48,44 +90,7 @@ func (m pipelineCreateForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m pipelineCreateForm) View() string {
return m.list.View()
}
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))
}
return m.formList.View()
}
type createPipelineItem struct {
@@ -148,6 +153,7 @@ func createPipelineCreateForm(client graphql.Client, width int, height int) (tea
l.KeyMap.Filter.SetEnabled(false)
l.KeyMap.Quit.SetEnabled(false)
return pipelineCreateForm{
list: l,
formList: l,
client: client,
}, nil
}
+45 -2
View File
@@ -3,6 +3,7 @@ package widget
import (
"context"
"fmt"
"io"
"git.ohea.xyz/cursorius/tui/queries"
"github.com/Khan/genqlient/graphql"
@@ -27,14 +28,19 @@ func (m pipelineWidgetList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "down":
if m.list.Index() < len(m.list.Items()) {
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 "enter":
case "e":
if m.list.Index() < len(m.list.Items())-1 {
//item := m.list.SelectedItem()
//if item, ok := item.(pipelineListItem); ok {
@@ -70,6 +76,43 @@ type pipelineListItem struct {
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 {