Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Mar 22, 2024
1 parent d7c7c56 commit 23b3980
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 221 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ vet:
.PHONY: install-linter
install-linter:
go install honnef.co/go/tools/cmd/staticcheck@latest

.PHONY: debug
debug:
dlv debug --headless --api-version=2 --listen=127.0.0.1:4300 .
# Exiting delve neglects to restore the terminal, so we do so here.
reset

.PHONY: connect
connect:
dlv connect 127.0.0.1:4300 .
6 changes: 5 additions & 1 deletion internal/tui/kind.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package tui

//go:generate stringer -type=Kind

type Kind int

const (
ModuleListKind Kind = iota
ModuleKind
WorkspaceListKind
WorkspaceKind
RunListKind
TaskListKind
RunKind
TaskListKind
TaskKind
LogsKind
)
31 changes: 31 additions & 0 deletions internal/tui/kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions internal/tui/module/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/exp/maps"
)

// ListMaker makes module list models
type ListMaker struct {
ModuleService *module.Service
RunService *run.Service
Expand Down Expand Up @@ -75,7 +76,7 @@ func (m *ListMaker) Make(_ resource.Resource, width, height int) (tui.Model, err
}
return cells
}
table := table.New[*module.Module](columns, renderer, width, height)
table := table.New(columns, renderer, width, height)
table = table.WithSortFunc(module.ByPath)

return list{
Expand Down Expand Up @@ -126,6 +127,10 @@ func (m list) Update(msg tea.Msg) (tui.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Global.Enter):
if mod, ok := m.table.Highlighted(); ok {
return m, tui.NavigateTo(tui.ModuleKind, &mod.Resource)
}
case key.Matches(msg, localKeys.Reload):
return m, func() tea.Msg {
if err := m.ModuleService.Reload(); err != nil {
Expand All @@ -143,10 +148,6 @@ func (m list) Update(msg tea.Msg) (tui.Model, tea.Cmd) {
return tui.NewErrorMsg(err, "opening vim")
})
}
case key.Matches(msg, keys.Global.Enter):
if mod, ok := m.table.Highlighted(); ok {
return m, tui.NavigateTo(tui.WorkspaceListKind, &mod.Resource)
}
case key.Matches(msg, localKeys.Init):
return m, tasktui.TaskCmd(m.ModuleService.Init, maps.Keys(m.table.HighlightedOrSelected())...)
case key.Matches(msg, localKeys.Validate):
Expand Down
94 changes: 94 additions & 0 deletions internal/tui/module/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package module

import (
"fmt"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/leg100/pug/internal/module"
"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/run"
"github.com/leg100/pug/internal/tui"
"github.com/leg100/pug/internal/tui/keys"
runtui "github.com/leg100/pug/internal/tui/run"
tasktui "github.com/leg100/pug/internal/tui/task"
workspacetui "github.com/leg100/pug/internal/tui/workspace"
"github.com/leg100/pug/internal/workspace"
)

// Maker makes module models.
type Maker struct {
ModuleService *module.Service
WorkspaceService *workspace.Service
RunService *run.Service

WorkspaceListMaker *workspacetui.ListMaker
RunListMaker *runtui.ListMaker
TaskListMaker *tasktui.ListMaker
}

func (mm *Maker) Make(mr resource.Resource, width, height int) (tui.Model, error) {
mod, err := mm.ModuleService.Get(mr.ID())
if err != nil {
return model{}, err
}

tabs := tui.NewTabSet(width, height)
if _, err := tabs.AddTab(mm.WorkspaceListMaker, mr, "workspaces"); err != nil {
return nil, fmt.Errorf("adding workspaces tab: %w", err)
}
if _, err := tabs.AddTab(mm.RunListMaker, mr, "runs"); err != nil {
return nil, fmt.Errorf("adding runs tab: %w", err)
}
if _, err := tabs.AddTab(mm.TaskListMaker, mr, "tasks"); err != nil {
return nil, fmt.Errorf("adding tasks tab: %w", err)
}

m := model{
module: mod,
tabs: tabs,
}
return m, nil
}

type model struct {
module *module.Module
tabs tui.TabSet
}

func (m model) Init() tea.Cmd {
return m.tabs.Init()
}

func (m model) Update(msg tea.Msg) (tui.Model, tea.Cmd) {
var cmds []tea.Cmd

switch msg := msg.(type) {
case resource.Event[*module.Module]:
if msg.Payload.ID() == m.module.ID() {
m.module = msg.Payload
}
}
// Update tabs
updated, cmd := m.tabs.Update(msg)
cmds = append(cmds, cmd)
m.tabs = updated

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

func (m model) Title() string {
return tui.Breadcrumbs("Module", m.module.Resource)
}

func (m model) View() string {
return m.tabs.View()
}

func (m model) Pagination() string {
return ""
}

func (m model) HelpBindings() (bindings []key.Binding) {
return keys.KeyMapToSlice(workspacetui.Keys)
}
6 changes: 6 additions & 0 deletions internal/tui/run/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"errors"
"fmt"
"slices"
"time"

Expand Down Expand Up @@ -51,6 +52,7 @@ func (m *ListMaker) Make(parent resource.Resource, width, height int) (tui.Model
statusColumn,
changesColumn,
ageColumn,
table.IDColumn,
)

renderer := func(r *run.Run, style lipgloss.Style) table.RenderedRow {
Expand Down Expand Up @@ -154,6 +156,10 @@ func (m list) Pagination() string {
return ""
}

func (m list) TabStatus() string {
return fmt.Sprintf("(%d)", len(m.table.Items()))
}

func (m list) HelpBindings() (bindings []key.Binding) {
return []key.Binding{
keys.Common.Plan,
Expand Down
Loading

0 comments on commit 23b3980

Please sign in to comment.