-
Notifications
You must be signed in to change notification settings - Fork 384
/
main.go
183 lines (156 loc) · 4.78 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
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 mini
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/terminfo"
"github.com/rivo/tview"
"go.uber.org/zap"
// Embeding assets.
_ "berty.tech/berty/v2/go/assets"
"berty.tech/berty/v2/go/internal/lifecycle"
"berty.tech/berty/v2/go/pkg/bertymessenger"
"berty.tech/berty/v2/go/pkg/bertyprotocol"
"berty.tech/berty/v2/go/pkg/bertytypes"
"berty.tech/berty/v2/go/pkg/errcode"
)
type Opts struct {
MessengerClient bertymessenger.MessengerServiceClient
ProtocolClient bertyprotocol.ProtocolServiceClient
Logger *zap.Logger
GroupInvitation string
DisplayName string
LifecycleManager *lifecycle.Manager
}
var globalLogger *zap.Logger
func Main(ctx context.Context, opts *Opts) error {
if opts.MessengerClient == nil {
return errcode.ErrMissingInput.Wrap(fmt.Errorf("missing messenger client"))
}
if opts.ProtocolClient == nil {
return errcode.ErrMissingInput.Wrap(fmt.Errorf("missing protocol client"))
}
_, err := terminfo.LookupTerminfo(os.Getenv("TERM"))
if err != nil {
return errcode.ErrCLINoTermcaps.Wrap(err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
config, err := opts.ProtocolClient.InstanceGetConfiguration(ctx, &bertytypes.InstanceGetConfiguration_Request{})
if err != nil {
return errcode.TODO.Wrap(err)
}
app := tview.NewApplication()
accountGroup, err := opts.ProtocolClient.GroupInfo(ctx, &bertytypes.GroupInfo_Request{
GroupPK: config.AccountGroupPK,
})
if err != nil {
return errcode.TODO.Wrap(err)
}
if opts.Logger != nil {
globalLogger = opts.Logger.Named(pkAsShortID(accountGroup.Group.PublicKey))
} else {
globalLogger = zap.NewNop()
}
tabbedView := newTabbedGroups(ctx, accountGroup, opts.ProtocolClient, opts.MessengerClient, app, opts.DisplayName)
if len(opts.GroupInvitation) > 0 {
req := &bertytypes.GroupMetadataList_Request{GroupPK: accountGroup.Group.PublicKey}
cl, err := tabbedView.protocol.GroupMetadataList(ctx, req)
if err != nil {
return errcode.ErrEventListMetadata.Wrap(err)
}
go func() {
for {
evt, err := cl.Recv()
switch err {
case io.EOF: // gracefully ended @TODO: log this
return
case nil: // ok
default:
panic(err)
}
if evt.Metadata.EventType != bertytypes.EventTypeAccountGroupJoined {
continue
}
tabbedView.NextGroup()
}
}()
for _, invit := range strings.Split(opts.GroupInvitation, ",") {
if err := groupJoinCommand(ctx, tabbedView.accountGroupView, invit); err != nil {
return errcode.TODO.Wrap(err)
}
}
}
input := tview.NewInputField().
SetFieldTextColor(tcell.ColorWhite).
SetFieldBackgroundColor(tcell.ColorBlack)
input.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
msg := input.GetText()
input.SetText("")
tabbedView.GetActiveViewGroup().OnSubmit(ctx, msg)
}
})
inputBox := tview.NewFlex().
AddItem(tview.NewTextView().SetText(">> "), 3, 0, false).
AddItem(input, 0, 1, true)
mainUI := tview.NewFlex().
AddItem(tabbedView.GetTabs(), 10, 0, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tabbedView.GetHistory(), 0, 1, false).
AddItem(inputBox, 1, 1, true), 0, 1, true)
const ShouldBecomeInactive = time.Second * 30
inactiveTimer := time.AfterFunc(ShouldBecomeInactive, func() {
opts.LifecycleManager.UpdateState(bertymessenger.StateInactive)
})
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
// reset timer
if !inactiveTimer.Stop() {
// AfterFunc timer should already have consume `inactiveTimer.C`
opts.LifecycleManager.UpdateState(bertymessenger.StateActive)
}
inactiveTimer.Reset(ShouldBecomeInactive)
// nolint:exhaustive
switch event.Key() {
case tcell.KeyCtrlC:
app.Stop()
case tcell.KeyEsc:
app.Stop()
case tcell.KeyHome:
tabbedView.GetActiveViewGroup().messages.historyScroll.ScrollToBeginning()
case tcell.KeyEnd:
tabbedView.GetActiveViewGroup().messages.historyScroll.ScrollToEnd()
case tcell.KeyPgUp:
tabbedView.GetActiveViewGroup().ScrollToOffset(-10)
case tcell.KeyPgDn:
tabbedView.GetActiveViewGroup().ScrollToOffset(+10)
case tcell.KeyCtrlP:
tabbedView.PrevGroup()
case tcell.KeyCtrlN:
tabbedView.NextGroup()
case tcell.KeyUp:
if event.Modifiers() == tcell.ModAlt || event.Modifiers() == tcell.ModCtrl {
tabbedView.PrevGroup()
} else {
input.SetText(tabbedView.GetActiveViewGroup().inputHistory.Prev())
}
case tcell.KeyDown:
if event.Modifiers() == tcell.ModAlt || event.Modifiers() == tcell.ModCtrl {
tabbedView.NextGroup()
} else {
input.SetText(tabbedView.GetActiveViewGroup().inputHistory.Next())
}
default:
return event
}
return nil
})
if err := app.SetRoot(mainUI, true).SetFocus(mainUI).Run(); err != nil {
return errcode.TODO.Wrap(err)
}
return nil
}