-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
147 lines (110 loc) · 3.85 KB
/
app.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func MakeInitialState() AppState {
return MakeEmptyState()
}
type UpdateCacheFn = func()
func run() {
app := tview.NewApplication()
client, err := GetClient()
if err != nil { panic(err) }
state := MakeInitialState()
conversations, conversationsUpdate := MakeConversations(client, state)
messages, messagesUpdate := MakeMessages(client, state)
preview, previewUpdate := MakePreview(state)
input, inputUpdate := MakeInput(state)
infobar, infobarUpdate := MakeInfobar(state)
inputContainer, _ := MakeContainerInput(state, input, infobar)
container := MakeContainer(conversations, messages, inputContainer, preview)
pages := tview.NewPages()
UIUpdate := func(newState AppState) {
conversationsUpdate(newState)
messagesUpdate(newState)
previewUpdate(newState)
infobarUpdate(newState)
showSearch := newState.search.open && !newState.search.opened
hideSearch := !newState.search.open
if showSearch { pages.ShowPage(PAGE_NAME_SEARCH); state.search.opened = true } else
if hideSearch { pages.HidePage(PAGE_NAME_SEARCH) }
if newState.focusInput { app.SetFocus(input) } else
{ inputUpdate(newState) }
}
cacheAndUIUpdate := MakeUpdateCacheFn(client, &state, UIUpdate)
onInputEscape := func(draft string) {
state.focusInput = false
state = UpdateStateDraft(state, draft)
messagesUpdate(state)
conversationsUpdate(state)
app.SetFocus(messages)
}
onInputEnter := MakeOnInputEnter(client, &state, cacheAndUIUpdate)
doneFn := MakeInputDoneFn(input, onInputEscape, onInputEnter)
input.SetDoneFunc(doneFn)
autoCompleteFn := MakeAutoCompleteFn()
input.SetAutocompleteFunc(autoCompleteFn)
onKeyDown := MakeOnKeyDown(app, &state, UIUpdate)
messages.SetInputCapture(onKeyDown)
search, _ := MakeSearch(app, client, &state, UIUpdate)
pages.
AddPage(PAGE_NAME_CONTAINER, container, true, true).
AddPage(PAGE_NAME_SEARCH, search, true, false)
app.SetRoot(pages, true)
cacheAndUIUpdate()
if err := app.Run(); err != nil {
panic(err)
}
}
func MakeUpdateCacheFn(client Client, state *AppState, updateUI func(state AppState)) UpdateCacheFn {
return func() {
convos, err := client.GetConversations()
if err != nil { panic(err) }
state.cache.conversations = convos
for i, convo := range convos {
msgs, _ := client.GetConversationMessages(convo)
state.cache.messages[i] = msgs
convoState := state.conversations[i]
messagePos := len(msgs) - 1
convoState.messagePos = messagePos
if messagePos != -1 {
lastMsg := msgs[messagePos]
lastMsgProvider := lastMsg.Provider
convoState.provider = lastMsgProvider
} else {
// this should be the default provider for the (app > contact) w that heiarchy
convoState.provider = "ADD DEFAULT"
}
state.conversations[i] = convoState
}
updateUI(*state)
}
}
type OnInputEnterFn = func(string)
func MakeOnInputEnter(client Client, state *AppState, updateCache UpdateCacheFn) OnInputEnterFn {
return func(message string) {
if message == "" { return }
convo := GetCacheConversation(*state)
provider := GetStateProvider(*state)
providerIds := []string{ provider }
err := client.SendMessage(convo, message, providerIds)
if err != nil { panic(err) }
msgs, _ := GetCacheMessages(*state)
newMsgPos := len(msgs)
*state = UpdateStateConversationPos(*state, 0)
*state = UpdateStateMessagePos(*state, newMsgPos)
updateCache()
}
}
type OnKeyDownFn = func(*tcell.EventKey) *tcell.EventKey
type UpdateFn = func(AppState)
func MakeOnKeyDown(app *tview.Application, state *AppState, update UpdateFn) OnKeyDownFn {
return func(event *tcell.EventKey) *tcell.EventKey {
newState := UpdateStateFromKeyBind(*state, event.Rune())
if newState.quit { app.Stop() }
update(newState)
*state = newState
return nil
}
}