-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
183 lines (164 loc) · 3.53 KB
/
window.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"strconv"
"strings"
bbt "github.com/charmbracelet/bubbletea"
)
type Window interface {
Update(bbt.Msg) (Window, bbt.Cmd)
View() string
}
type WindowView struct {
header *PaneHeader
view *PaneView
footer *PaneFooter
active Pane
}
func NewWindowView() *WindowView {
var window WindowView
window.view = NewPaneView()
window.header = NewPaneHeader(
PaneHeaderItem{
Name: "Back",
Func: func() bbt.Cmd {
return Activate("list")
},
},
)
window.footer = NewPaneFooter(
func() string {
return fmt.Sprintf("%3.f%%", window.view.model.ScrollPercent()*100)
},
func() string {
return ""
},
)
window.active = window.view
return &window
}
func (w *WindowView) Update(msg bbt.Msg) (Window, bbt.Cmd) {
switch msg := msg.(type) {
case ActivateMsg:
if msg == "toggle" {
switch w.active.(type) {
case *PaneHeader:
msg = "view"
case *PaneView:
msg = "header"
}
}
switch strings.ToLower(string(msg)) {
case "header":
w.active.Deactivate()
w.active = w.header.Activate()
case "view":
w.active.Deactivate()
w.active = w.view.Activate()
}
case bbt.KeyMsg:
switch msg.String() {
case "esc", "backspace":
return w, Activate("list")
}
case bbt.WindowSizeMsg:
for _, pane := range []Pane{w.header, w.footer, w.view} {
pane.SetSize(msg.Width, msg.Height)
width, height := pane.Size()
msg.Width -= width
msg.Height -= height
}
}
var cmd bbt.Cmd
w.active, cmd = w.active.Update(msg)
return w, cmd
}
func (w *WindowView) View() string {
var sb strings.Builder
sb.WriteString(w.header.View())
sb.WriteString(w.view.View())
sb.WriteString(w.footer.View())
return sb.String()
}
type WindowList struct {
header *PaneHeader
list *PaneList
footer *PaneFooter
active Pane
}
func NewWindowList() *WindowList {
var items []PaneHeaderItem
values := []string{"Top", "New", "Best", "Ask", "Show", "Job"}
for i := range values {
value := values[i]
items = append(items, PaneHeaderItem{
Name: value,
Func: func() bbt.Cmd {
return bbt.Sequence(
Activate("list"),
List("clear"),
List(value),
)
},
})
}
var window WindowList
window.header = NewPaneHeader(items...)
window.list = NewPaneList()
window.footer = NewPaneFooter(
func() string {
return fmt.Sprintf("%d of %d", window.list.model.Paginator.Page+1, window.list.model.Paginator.TotalPages)
}, func() string {
return ""
},
)
window.active = window.list
return &window
}
func (w *WindowList) Update(msg bbt.Msg) (Window, bbt.Cmd) {
switch msg := msg.(type) {
case ActivateMsg:
if msg == "toggle" {
switch w.active.(type) {
case *PaneHeader:
msg = "list"
case *PaneList:
msg = "header"
}
}
switch strings.ToLower(string(msg)) {
case "header":
w.active.Deactivate()
w.active = w.header.Activate()
case "list":
w.active.Deactivate()
w.active = w.list.Activate()
}
case bbt.KeyMsg:
switch msg.String() {
case "1", "2", "3", "4", "5", "6":
n, _ := strconv.Atoi(msg.String())
return w, bbt.Sequence(
Activate("header"),
Header(n-1),
)
}
case bbt.WindowSizeMsg:
for _, pane := range []Pane{w.header, w.footer, w.list} {
pane.SetSize(msg.Width, msg.Height)
width, height := pane.Size()
msg.Width -= width
msg.Height -= height
}
}
var cmd bbt.Cmd
w.active, cmd = w.active.Update(msg)
return w, cmd
}
func (w *WindowList) View() string {
var sb strings.Builder
sb.WriteString(w.header.View())
sb.WriteString(w.list.View())
sb.WriteString(w.footer.View())
return sb.String()
}