-
Notifications
You must be signed in to change notification settings - Fork 9
/
keybinding.go
83 lines (67 loc) · 2.26 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
// 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/internal/console/common"
"namespacelabs.dev/foundation/schema/storage"
)
type NetworkPlanKeybinding struct {
name string
}
func NewNetworkPlanKeybinding(name string) *NetworkPlanKeybinding {
return &NetworkPlanKeybinding{
name: name,
}
}
func (k NetworkPlanKeybinding) Key() string { return "s" }
func (k NetworkPlanKeybinding) Label(enabled bool) string {
if !enabled {
return "show support servers"
}
return "hide support servers"
}
func (k NetworkPlanKeybinding) Handle(ctx context.Context, ch chan keyboard.Event, control chan<- keyboard.Control) {
showSupportServers := false
var networkPlan *storage.NetworkPlan
for event := range ch {
switch event.Operation {
case keyboard.OpSet:
showSupportServers := event.Enabled
k.renderStickyNetworkPlan(ctx, networkPlan, showSupportServers)
c := keyboard.Control{Operation: keyboard.ControlAck}
c.AckEvent.HandlerID = event.HandlerID
c.AckEvent.EventID = event.EventID
control <- c
case keyboard.OpStackUpdate:
networkPlan = event.StackUpdate.NetworkPlan
k.renderStickyNetworkPlan(ctx, networkPlan, showSupportServers)
}
}
}
func (k NetworkPlanKeybinding) renderStickyNetworkPlan(ctx context.Context, plan *storage.NetworkPlan, showSupportServers bool) {
content := ""
if plan != nil {
summary := render.NetworkPlanToSummary(plan)
var out bytes.Buffer
NetworkPlanToText(&out, summary, &NetworkPlanToTextOpts{
Style: colors.WithColors,
Checkmark: true,
IncludeSupportServers: showSupportServers,
})
content = out.String()
}
if plan != nil && plan.IsDeploymentFinished() {
fmt.Fprintln(console.TypedOutput(ctx, "network plan", common.CatOutputUs), content)
console.SetStickyContent(ctx, k.name, "")
} else {
console.SetStickyContent(ctx, k.name, content)
}
}