-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
140 lines (124 loc) · 2.91 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Focus int64
const (
Left Focus = iota
Right
)
func (s Focus) String() string {
switch s {
case Left:
return "left focus"
case Right:
return "right focus"
}
return "unknown"
}
var (
AppStyle = lipgloss.NewStyle().
BorderForeground(lipgloss.Color("63")).
Padding(1, 2)
TitleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#25A065")).
Padding(0, 1)
StatusMessageStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#04B575", Dark: "#04B575"}).
Render
)
type AppModel struct {
Target *TargetModel
Action *ActionModel
Keys *AppKeyMap
Focus
}
func (m *AppModel) Init() tea.Cmd {
return tea.EnterAltScreen
}
func (m *AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmds []tea.Cmd
cmd tea.Cmd
)
switch msg := msg.(type) {
case tea.KeyMsg:
// quit even during filtering
if key.Matches(msg, m.Keys.forceQuit) {
return m, tea.Quit
}
// Don't match any of the keys below if we're actively filtering.
if m.Target.List.FilterState() == list.Filtering {
break
}
switch {
case key.Matches(msg, m.Keys.toggleFocus):
if m.Focus == Left {
m.Focus = Right
cmd = m.Target.List.ToggleSpinner()
cmds = append(cmds, cmd)
cmd = m.Action.List.ToggleSpinner()
cmds = append(cmds, cmd)
} else {
m.Focus = Left
cmd = m.Target.List.ToggleSpinner()
cmds = append(cmds, cmd)
cmd = m.Action.List.ToggleSpinner()
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
case tea.WindowSizeMsg:
}
// This will also call our delegate's update function.
if m.Focus == Left {
m.Target, cmd = m.Target.Update(msg)
if m.Target.List.SelectedItem() != nil {
m.Action = NewAction(m.Target.List.SelectedItem().(item))
} else {
m.Action = &ActionModel{}
}
cmds = append(cmds, cmd)
} else {
m.Action, cmd = m.Action.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m *AppModel) View() string {
var help string
if m.Focus == Left {
var l = m.Target.List
help = l.Styles.HelpStyle.Render(l.Help.View(l))
} else {
var l = m.Action.List
help = l.Styles.HelpStyle.Render(l.Help.View(l))
}
return AppStyle.Render(
lipgloss.JoinVertical(
lipgloss.Center,
lipgloss.JoinHorizontal(lipgloss.Left, m.Target.View(), m.Action.View()),
help,
))
}
func InitialPage() *AppModel {
targets := InitialTarget()
action := NewAction(targets.List.SelectedItem().(item))
self := &AppModel{targets, action, NewAppKeyMap(), Left}
return self
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if err := tea.NewProgram(InitialPage()).Start(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}