-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegate.go
102 lines (83 loc) · 2.16 KB
/
delegate.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
93
94
95
96
97
98
99
100
101
102
package main
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
d := list.NewDefaultDelegate()
d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd {
var title, containerID, containerState string
if i, ok := m.SelectedItem().(item); ok {
title = i.Title()
containerID = i.containerID
containerState = i.containerState
} else {
return nil
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.choose):
if containerState == "exited" {
return tea.Batch(m.NewStatusMessage(statusMessageStyle("Starting "+title)), StartDockerContainerCmd(containerID))
}
if containerState == "running" {
return tea.Batch(m.NewStatusMessage(statusMessageStyle("Stopping "+title)), StopDockerContainerCmd(containerID))
}
}
}
return nil
}
help := []key.Binding{keys.choose}
d.ShortHelpFunc = func() []key.Binding {
return help
}
d.FullHelpFunc = func() [][]key.Binding {
return [][]key.Binding{help}
}
return d
}
type StartDockerContainerMsg struct {
ContainerID string
}
type StopDockerContainerMsg struct {
ContainerID string
}
func StartDockerContainerCmd(containerId string) tea.Cmd {
return func() tea.Msg {
return StartDockerContainerMsg{ContainerID: containerId}
}
}
func StopDockerContainerCmd(containerId string) tea.Cmd {
return func() tea.Msg {
return StopDockerContainerMsg{ContainerID: containerId}
}
}
type delegateKeyMap struct {
choose key.Binding
}
// Additional short help entries. This satisfies the help.KeyMap interface and
// is entirely optional.
func (d delegateKeyMap) ShortHelp() []key.Binding {
return []key.Binding{
d.choose,
}
}
// Additional full help entries. This satisfies the help.KeyMap interface and
// is entirely optional.
func (d delegateKeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{
d.choose,
},
}
}
func newDelegateKeyMap() *delegateKeyMap {
return &delegateKeyMap{
choose: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "Start/Stop container"),
),
}
}