Skip to content

Commit

Permalink
refactor: clean up global task config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Jan 26, 2025
1 parent b8a5e01 commit 413c08f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
48 changes: 48 additions & 0 deletions internal/tui/task/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package task

import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/leg100/pug/internal/tui"
"github.com/leg100/pug/internal/tui/keys"
)

// Config is global task configuration
type Config struct {
// disableAutoscroll disables auto-scrolling of task output.
disableAutoscroll bool
// showInfo shows further info about the task.
showInfo bool
}

type (
// toggleAutoscrollMsg toggles whether task output is auto-scrolled.
toggleAutoscrollMsg struct{}

// toggleShowInfo toggles whether task info is shown.
toggleShowInfo struct{}
)

// Update updates global configuration of tasks.
func (c *Config) Update(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Global.Autoscroll):
c.disableAutoscroll = !c.disableAutoscroll

// Inform user, and send out message to all cached task models to
// toggle autoscroll.
return tea.Batch(
tui.CmdHandler(toggleAutoscrollMsg{}),
tui.ReportInfo("Toggled autoscroll %s", boolToOnOff(!c.disableAutoscroll)),
)
case key.Matches(msg, localKeys.ToggleInfo):
c.showInfo = !c.showInfo

// Send out message to all cached task models to toggle task info
return tui.CmdHandler(toggleShowInfo{})
}
}
return nil
}
7 changes: 0 additions & 7 deletions internal/tui/task/messages.go

This file was deleted.

62 changes: 17 additions & 45 deletions internal/tui/task/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ type Maker struct {
Helpers *tui.Helpers
Logger *logging.Logger
Program string

disableAutoscroll bool
showInfo bool
Config *Config
}

func (mm *Maker) Make(id resource.ID, width, height int) (tui.ChildModel, error) {
Expand All @@ -50,13 +48,11 @@ func (mm *Maker) make(id resource.ID, width, height int, border bool) (tui.Child
output: task.NewStreamer(),
spinner: mm.Spinner,
// read upto 1kb at a time
buf: make([]byte, 1024),
Helpers: mm.Helpers,
showInfo: mm.showInfo,
width: width,
program: mm.Program,
// Disable autoscroll if either task is finished or user has disabled it
disableAutoscroll: task.State.IsFinal() || mm.disableAutoscroll,
buf: make([]byte, 1024),
Helpers: mm.Helpers,
width: width,
program: mm.Program,
config: mm.Config,
}
m.setHeight(height)

Expand All @@ -74,29 +70,6 @@ func (mm *Maker) make(id resource.ID, width, height int, border bool) (tui.Child
return &m, nil
}

func (mm *Maker) Update(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Global.Autoscroll):
mm.disableAutoscroll = !mm.disableAutoscroll

// Inform user, and send out message to all cached task models to
// toggle autoscroll.
return tea.Batch(
tui.CmdHandler(toggleAutoscrollMsg{}),
tui.ReportInfo("Toggled autoscroll %s", boolToOnOff(!mm.disableAutoscroll)),
)
case key.Matches(msg, localKeys.ToggleInfo):
mm.showInfo = !mm.showInfo

// Send out message to all cached task models to toggle task info
return tui.CmdHandler(toggleTaskInfoMsg{})
}
}
return nil
}

type Model struct {
*tui.Helpers

Expand All @@ -110,12 +83,11 @@ type Model struct {
output <-chan []byte
buf []byte

program string
disableAutoscroll bool
showInfo bool
program string

viewport tui.Viewport
spinner *spinner.Model
config *Config

height int
width int
Expand Down Expand Up @@ -154,18 +126,18 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
cmd := m.common.Update(msg)
cmds = append(cmds, cmd)
}
case toggleAutoscrollMsg:
m.disableAutoscroll = !m.disableAutoscroll
case toggleTaskInfoMsg:
m.showInfo = !m.showInfo
// adjust width of viewport to accomodate info
case toggleShowInfo:
// adjust width of viewport to reflect presence/absence of task info
// side pane.
m.viewport.SetDimensions(m.viewportWidth(), m.height)
case outputMsg:
// Ensure output is for this model
if msg.modelID != m.id {
return nil
}
err := m.viewport.AppendContent(msg.output, msg.eof, !m.disableAutoscroll)
// Disable autoscroll if either task is finished or user has disabled it
disableAutoscroll := m.task.State.IsFinal() || m.config.disableAutoscroll
err := m.viewport.AppendContent(msg.output, msg.eof, !disableAutoscroll)
if err != nil {
return tui.ReportError(err)
}
Expand Down Expand Up @@ -193,7 +165,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
}

func (m Model) viewportWidth() int {
if m.showInfo {
if m.config.showInfo {
m.width -= infoWidth
}
return max(0, m.width)
Expand All @@ -216,7 +188,7 @@ const (
func (m *Model) View() string {
var components []string

if m.showInfo {
if m.config.showInfo {
var (
args = "-"
envs = "-"
Expand Down Expand Up @@ -245,7 +217,7 @@ func (m *Model) View() string {
tui.Bold.Render("Environment variables"),
envs,
"",
fmt.Sprintf("Autoscroll: %s", boolToOnOff(!m.disableAutoscroll)),
fmt.Sprintf("Autoscroll: %s", boolToOnOff(!m.config.disableAutoscroll)),
"",
fmt.Sprintf("Dependencies: %v", m.task.DependsOn),
)
Expand Down
8 changes: 2 additions & 6 deletions internal/tui/top/makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package top

import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/leg100/pug/internal/app"
"github.com/leg100/pug/internal/tui"
"github.com/leg100/pug/internal/tui/explorer"
Expand All @@ -11,17 +10,13 @@ import (
workspacetui "github.com/leg100/pug/internal/tui/workspace"
)

// updateableMaker is a dynamically configurable maker.
type updateableMaker interface {
Update(tea.Msg) tea.Cmd
}

// makeMakers makes model makers for making models
func makeMakers(
cfg app.Config,
app *app.App,
spinner *spinner.Model,
helpers *tui.Helpers,
taskConfig *tasktui.Config,
) map[tui.Kind]tui.Maker {
taskMaker := &tasktui.Maker{
Plans: app.Plans,
Expand All @@ -30,6 +25,7 @@ func makeMakers(
Helpers: helpers,
Logger: app.Logger,
Program: cfg.Program,
Config: taskConfig,
}
logMaker := &logs.Maker{
Logger: app.Logger,
Expand Down
20 changes: 9 additions & 11 deletions internal/tui/top/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/leg100/pug/internal/task"
"github.com/leg100/pug/internal/tui"
"github.com/leg100/pug/internal/tui/keys"
tuitask "github.com/leg100/pug/internal/tui/task"
"github.com/leg100/pug/internal/version"
)

Expand Down Expand Up @@ -49,6 +50,7 @@ type model struct {
lastTaskID *resource.MonotonicID
err error
info string
taskConfig *tuitask.Config
}

func newModel(cfg app.Config, app *app.App) (model, error) {
Expand All @@ -75,7 +77,8 @@ func newModel(cfg app.Config, app *app.App) (model, error) {
Workdir: cfg.Workdir,
}
spinner := spinner.New(spinner.WithSpinner(spinner.Line))
makers := makeMakers(cfg, app, &spinner, helpers)
taskConfig := &tuitask.Config{}
makers := makeMakers(cfg, app, &spinner, helpers, taskConfig)

m := model{
PaneManager: tui.NewPaneManager(makers),
Expand All @@ -85,6 +88,7 @@ func newModel(cfg app.Config, app *app.App) (model, error) {
tasks: app.Tasks,
dump: dump,
workdir: cfg.Workdir.PrettyString(),
taskConfig: taskConfig,
}
return m, nil
}
Expand Down Expand Up @@ -271,20 +275,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tui.NavigateTo(tui.TaskKind, tui.WithParent(*m.lastTaskID))
}
default:
// Send all others to global task config updater
if cmd := m.taskConfig.Update(msg); cmd != nil {
return m, cmd
}
// Send all other keys to panes.
if cmd := m.PaneManager.Update(msg); cmd != nil {
return m, cmd
}
// If pane manager doesn't respond with a command, then send key to
// any updateable model makers; first one to respond with a command
// wins.
for _, maker := range m.makers {
if updateable, ok := maker.(updateableMaker); ok {
if cmd := updateable.Update(msg); cmd != nil {
return m, cmd
}
}
}
return m, nil
}
case tui.ErrorMsg:
Expand Down

0 comments on commit 413c08f

Please sign in to comment.