Skip to content

Commit

Permalink
chore: rename task changes column to summary
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Aug 21, 2024
1 parent f7cd452 commit 75affa9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 120 deletions.
38 changes: 38 additions & 0 deletions internal/state/reload_summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package state

const (
Empty ReloadSummary = iota
Unchanged
Updated
)

type ReloadSummary int

func (s ReloadSummary) String() string {
switch s {
case Empty:
return "empty"
case Unchanged:
return "unchanged"
case Updated:
return "updated"
default:
return ""
}
}

func newReloadSummary(before, after *State) ReloadSummary {
if after == nil {
return Empty
}
if after.Serial == -1 {
return Empty
}
if before == nil {
return Updated
}
if before.Serial == after.Serial {
return Unchanged
}
return Updated
}
36 changes: 2 additions & 34 deletions internal/state/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package state

import (
"fmt"
"log/slog"

"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/task"
Expand All @@ -12,37 +11,6 @@ type reloader struct {
*Service
}

type ReloadSummary struct {
Old, New *State
}

func (s ReloadSummary) OldSerial() int {
oldSerial := -1
if s.Old != nil {
oldSerial = int(s.Old.Serial)
}
return oldSerial
}

func (s ReloadSummary) NewSerial() int {
newSerial := -1
if s.New != nil {
newSerial = int(s.New.Serial)
}
return newSerial
}

func (s ReloadSummary) String() string {
return fmt.Sprintf("#%d->#%d", s.OldSerial(), s.NewSerial())
}

func (s ReloadSummary) LogValue() slog.Value {
return slog.GroupValue(
slog.Any("old.state.serial", s.OldSerial()),
slog.Any("new.state.serial", s.NewSerial()),
)
}

// Reload creates a task to repopulate the local cache of the state of the given
// workspace.
func (r *reloader) Reload(workspaceID resource.ID) (task.Spec, error) {
Expand All @@ -63,11 +31,11 @@ func (r *reloader) Reload(workspaceID resource.ID) (task.Spec, error) {
// updates will be made before checking for content.
old, err := r.cache.Get(workspaceID)
if err == nil && old.Serial == state.Serial {
return ReloadSummary{Old: old, New: state}, nil
return newReloadSummary(old, state), nil
}
// Add/replace state in cache.
r.cache.Add(workspaceID, state)
return ReloadSummary{Old: old, New: state}, nil
return newReloadSummary(old, state), nil
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ var (
Light: string(Grey),
}

RunReportBackgroundColor = EvenLighterGrey
GroupReportBackgroundColor = EvenLighterGrey
TaskSummaryBackgroundColor = EvenLighterGrey

ScrollPercentageBackground = lipgloss.AdaptiveColor{
Dark: string(DarkGrey),
Expand Down
127 changes: 52 additions & 75 deletions internal/tui/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,116 +177,93 @@ func (h *Helpers) TaskStatus(t *task.Task, background bool) string {
}
}

// TaskSummary renders a summary of the task's outcome. Set table to true if the
// outcome is to be rendered within a table row.
// TaskSummary renders a summary of the task's outcome.
func (h *Helpers) TaskSummary(t *task.Task, table bool) string {
if t.Summary == nil {
return ""
}
var style lipgloss.Style
if !table {
style = lipgloss.NewStyle().Background(TaskSummaryBackgroundColor)
}
// Render special resource report
var content string
switch summary := t.Summary.(type) {
case plan.Report:
return h.ResourceReport(summary, table)
content = h.ResourceReport(summary, style)
case workspace.ReloadSummary:
return h.WorkspaceReloadReport(summary, table)
content = h.WorkspaceReloadReport(summary, style)
case workspace.CostSummary:
return h.CostSummary(summary, table)
content = h.CostSummary(summary, style)
case state.ReloadSummary:
return h.StateReloadReport(summary, table)
content = h.StateReloadReport(summary, style)
default:
content = t.Summary.String()
}
if table {
return content
}
return t.Summary.String()
return Padded.Background(TaskSummaryBackgroundColor).Render(content)
}

// ResourceReport renders a colored summary of resource changes as a result of a
// plan or apply. Set table to true if the report is rendered within a table
// row.
func (h *Helpers) ResourceReport(report plan.Report, table bool) string {
var background lipgloss.TerminalColor = lipgloss.NoColor{}
if !table {
background = RunReportBackgroundColor
}
additions := Regular.Background(background).Foreground(Green).Render(fmt.Sprintf("+%d", report.Additions))
changes := Regular.Background(background).Foreground(Blue).Render(fmt.Sprintf("~%d", report.Changes))
destructions := Regular.Background(background).Foreground(Red).Render(fmt.Sprintf("-%d", report.Destructions))
// plan or apply.
func (h *Helpers) ResourceReport(report plan.Report, inherit lipgloss.Style) string {
additions := Regular.Foreground(Green).Inherit(inherit).Render(fmt.Sprintf("+%d", report.Additions))
changes := Regular.Foreground(Blue).Inherit(inherit).Render(fmt.Sprintf("~%d", report.Changes))
destructions := Regular.Foreground(Red).Inherit(inherit).Render(fmt.Sprintf("-%d", report.Destructions))

s := fmt.Sprintf("%s%s%s", additions, changes, destructions)
if !table {
s = Padded.Background(background).Render(s)
}
return s
return fmt.Sprintf("%s%s%s", additions, changes, destructions)
}

// WorkspaceReloadReport renders a colored summary of workspaces added or
// removed as a result of a workspace reload. Set table to true if the report is
// rendered within a table row.
func (h *Helpers) WorkspaceReloadReport(report workspace.ReloadSummary, table bool) string {
var background lipgloss.TerminalColor = lipgloss.NoColor{}
if !table {
background = RunReportBackgroundColor
}
added := Regular.Background(background).Foreground(Green).Render(fmt.Sprintf("+%d", len(report.Added)))
removed := Regular.Background(background).Foreground(Red).Render(fmt.Sprintf("-%d", len(report.Removed)))
// removed as a result of a workspace reload.
func (h *Helpers) WorkspaceReloadReport(report workspace.ReloadSummary, inherit lipgloss.Style) string {
added := Regular.Foreground(Green).Inherit(inherit).Render(fmt.Sprintf("+%d", len(report.Added)))
removed := Regular.Foreground(Red).Inherit(inherit).Render(fmt.Sprintf("-%d", len(report.Removed)))

s := fmt.Sprintf("%s%s", added, removed)
if !table {
s = Padded.Background(background).Render(s)
}
return s
return fmt.Sprintf("%s%s", added, removed)
}

// StateReloadReport renders a colored summary of changes resulting from a
// workspace reload. Set table to true if the report is rendered within a table
// row.
func (h *Helpers) StateReloadReport(report state.ReloadSummary, table bool) string {
var background lipgloss.TerminalColor = lipgloss.NoColor{}
if !table {
background = RunReportBackgroundColor
}
oldSerial := Regular.Background(background).Foreground(Red).Render(fmt.Sprintf("#%d", report.OldSerial()))
newSerial := Regular.Background(background).Foreground(Green).Render(fmt.Sprintf("#%d", report.NewSerial()))

s := fmt.Sprintf("%s<-%s", newSerial, oldSerial)
if !table {
s = Padded.Background(background).Render(s)
}
return s
// workspace reload.
func (h *Helpers) StateReloadReport(report state.ReloadSummary, inherit lipgloss.Style) string {
var foreground lipgloss.TerminalColor
switch report {
case state.Empty:
foreground = Red
case state.Unchanged:
foreground = Grey
case state.Updated:
foreground = Green
}
return Regular.Foreground(foreground).Inherit(inherit).Render(report.String())
}

// CostSummary renders a summary of the costs for a workspace. Set table to true
// if the report is rendered within a table row.
func (h *Helpers) CostSummary(report workspace.CostSummary, table bool) string {
var background lipgloss.TerminalColor = lipgloss.NoColor{}
if !table {
background = RunReportBackgroundColor
}
s := Regular.Background(background).Foreground(Green).Render(report.String())
if !table {
s = Padded.Background(background).Render(s)
}
return s
// CostSummary renders a summary of the costs for a workspace.
func (h *Helpers) CostSummary(report workspace.CostSummary, inherit lipgloss.Style) string {
return Regular.Foreground(Green).Inherit(inherit).Render(report.String())
}

// GroupReport renders a colored summary of a task group's task statuses. Set table to true if
// the report is rendered within a table row.
// GroupReport renders a colored summary of a task group's task statuses.
func (h *Helpers) GroupReport(group *task.Group, table bool) string {
var background lipgloss.TerminalColor = lipgloss.NoColor{}
var inherit lipgloss.Style
if !table {
background = GroupReportBackgroundColor
inherit = Padded.Background(GroupReportBackgroundColor)
}
slash := Regular.Background(background).Foreground(Grey).Render("/")
exited := Regular.Background(background).Foreground(Green).Render(fmt.Sprintf("%d", group.Exited()))
total := Regular.Background(background).Foreground(Blue).Render(fmt.Sprintf("%d", len(group.Tasks)))
slash := Regular.Inherit(inherit).Foreground(Grey).Render("/")
exited := Regular.Inherit(inherit).Foreground(Green).Render(fmt.Sprintf("%d", group.Exited()))
total := Regular.Inherit(inherit).Foreground(Blue).Render(fmt.Sprintf("%d", len(group.Tasks)))

s := fmt.Sprintf("%s%s%s", exited, slash, total)
if errored := group.Errored(); errored > 0 {
erroredString := Regular.Background(background).Foreground(Red).Render(fmt.Sprintf("%d", errored))
erroredString := Regular.Foreground(Red).Render(fmt.Sprintf("%d", errored))
s = fmt.Sprintf("%s%s%s", erroredString, slash, s)
}

if !table {
s = Padded.Background(background).Render(s)
if table {
return s
}
return s
return Padded.Background(GroupReportBackgroundColor).Render(s)
}

// CreateTasks repeatedly invokes fn with each id in ids, creating a task for
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/table/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var (
Title: "WORKSPACE",
FlexFactor: 1,
}
RunChangesColumn = Column{
Key: "run_changes",
Title: "CHANGES",
SummaryColumn = Column{
Key: "summary",
Title: "SUMMARY",
FlexFactor: 1,
}
ResourceCountColumn = Column{
Expand Down
9 changes: 2 additions & 7 deletions internal/tui/task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ var (
Title: "AGE",
Width: 7,
}
runChangesColumn = table.Column{
Key: "run_changes",
Title: "CHANGES",
FlexFactor: 1,
}
)

// ListTaskMaker makes task models belonging to a task list model
Expand Down Expand Up @@ -79,7 +74,7 @@ func (mm *ListMaker) Make(_ resource.ID, width, height int) (tea.Model, error) {
table.WorkspaceColumn,
commandColumn,
statusColumn,
runChangesColumn,
table.SummaryColumn,
ageColumn,
}

Expand All @@ -91,7 +86,7 @@ func (mm *ListMaker) Make(_ resource.ID, width, height int) (tea.Model, error) {
commandColumn.Key: t.String(),
ageColumn.Key: tui.Ago(time.Now(), t.Updated),
statusColumn.Key: mm.Helpers.TaskStatus(t, false),
runChangesColumn.Key: mm.Helpers.TaskSummary(t, true),
table.SummaryColumn.Key: mm.Helpers.TaskSummary(t, true),
}
}

Expand Down

0 comments on commit 75affa9

Please sign in to comment.