-
Notifications
You must be signed in to change notification settings - Fork 9
/
keybinding.go
92 lines (75 loc) · 2.58 KB
/
keybinding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package view
import (
"bytes"
"context"
"fmt"
"namespacelabs.dev/foundation/framework/planning/render"
"namespacelabs.dev/foundation/internal/cli/keyboard"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/tasks/idtypes"
)
type NetworkPlanKeybinding struct {
Name string
}
func (k NetworkPlanKeybinding) Key() string { return "s" }
func (k NetworkPlanKeybinding) States() []keyboard.HandlerState {
return []keyboard.HandlerState{
{State: "hidden", Label: "show main services"},
{State: "show", Label: "show all services"},
{State: "show-all", Label: "hide services"},
}
}
func (k NetworkPlanKeybinding) Handle(ctx context.Context, ch chan keyboard.Event, control chan<- keyboard.Control) {
defer close(control)
currentState := "hidden"
var networkPlan *storage.NetworkPlan
var deploymentRevision uint64
for event := range ch {
switch event.Operation {
case keyboard.OpSet:
currentState = event.CurrentState
case keyboard.OpStackUpdate:
networkPlan = event.StackUpdate.NetworkPlan
}
if currentState != "hidden" {
console.SetStickyContent(ctx, k.Name, renderStickyNetworkPlan(networkPlan, currentState == "show-all"))
} else {
console.SetStickyContent(ctx, k.Name, "")
if event.Operation == keyboard.OpStackUpdate && event.StackUpdate.Deployed && event.StackUpdate.DeployedRevision > deploymentRevision && networkPlan != nil && !networkPlan.Incomplete {
deploymentRevision = event.StackUpdate.DeployedRevision
if out := renderStickyNetworkPlan(networkPlan, false); out != "" {
fmt.Fprintln(console.TypedOutput(ctx, "network plan", idtypes.CatOutputUs), out)
}
}
}
switch event.Operation {
case keyboard.OpSet:
c := keyboard.Control{}
c.AckEvent.EventID = event.EventID
control <- c
case keyboard.OpStackUpdate:
set := event.StackUpdate.Deployed
control <- keyboard.Control{
SetEnabled: &set,
}
}
}
}
func renderStickyNetworkPlan(plan *storage.NetworkPlan, showSupportServers bool) string {
if plan == nil {
return ""
}
summary := render.NetworkPlanToSummary(plan)
var out bytes.Buffer
NetworkPlanToText(&out, summary, &NetworkPlanToTextOpts{
Style: colors.WithColors,
Checkmark: true,
IncludeSupportServers: showSupportServers,
})
return out.String()
}