-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
101 lines (83 loc) · 1.8 KB
/
view.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
package peco
import (
"time"
"github.com/peco/peco/hub"
"golang.org/x/net/context"
)
type statusMsgReq interface {
Message() string
Delay() time.Duration
}
func (prt PagingRequestType) Type() PagingRequestType {
return prt
}
func (jlr JumpToLineRequest) Type() PagingRequestType {
return ToLineInPage
}
func (jlr JumpToLineRequest) Line() int {
return int(jlr)
}
func NewView(state *Peco) *View {
var layout Layout
switch state.LayoutType() {
case LayoutTypeBottomUp:
layout = NewBottomUpLayout(state)
default:
layout = NewDefaultLayout(state)
}
return &View{
state: state,
layout: layout,
}
}
func (v *View) Loop(ctx context.Context, cancel func()) error {
defer cancel()
h := v.state.Hub()
for {
select {
case <-ctx.Done():
return nil
case r := <-h.StatusMsgCh():
v.printStatus(r, r.Data().(statusMsgReq))
case r := <-h.PagingCh():
v.movePage(r, r.Data().(PagingRequest))
case r := <-h.DrawCh():
tmp := r.Data()
switch tmp.(type) {
case string:
switch tmp.(string) {
case "prompt":
v.drawPrompt(r)
case "purgeCache":
v.purgeDisplayCache(r)
}
case *DrawOptions:
v.drawScreen(r, tmp.(*DrawOptions))
default:
v.drawScreen(r, nil)
}
}
}
}
func (v *View) printStatus(p hub.Payload, r statusMsgReq) {
defer p.Done()
v.layout.PrintStatus(r.Message(), r.Delay())
}
func (v *View) purgeDisplayCache(p hub.Payload) {
defer p.Done()
v.layout.PurgeDisplayCache()
}
func (v *View) drawScreen(p hub.Payload, options *DrawOptions) {
defer p.Done()
v.layout.DrawScreen(v.state, options)
}
func (v *View) drawPrompt(p hub.Payload) {
defer p.Done()
v.layout.DrawPrompt(v.state)
}
func (v *View) movePage(p hub.Payload, r PagingRequest) {
defer p.Done()
if v.layout.MovePage(v.state, r) {
v.layout.DrawScreen(v.state, nil)
}
}